root/lib/strutil/strverscmp.c

/* [previous][next][first][last][top][bottom][index][help]  */

DEFINITIONS

This source file includes following definitions.
  1. str_verscmp

   1 /*
   2    Compare strings while treating digits characters numerically.
   3 
   4    Copyright (C) 1997-2024
   5    Free Software Foundation, Inc.
   6 
   7    This file is part of the GNU C Library.
   8    Contributed by Jean-François Bignolles <bignolle@ecoledoc.ibp.fr>, 1997.
   9 
  10    The GNU C Library is free software; you can redistribute it and/or
  11    modify it under the terms of the GNU Lesser General Public
  12    License as published by the Free Software Foundation; either
  13    version 2.1 of the License, or (at your option) any later version.
  14 
  15    The GNU C Library is distributed in the hope that it will be useful,
  16    but WITHOUT ANY WARRANTY; without even the implied warranty of
  17    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  18    Lesser General Public License for more details.
  19 
  20    You should have received a copy of the GNU Lesser General Public
  21    License along with the GNU C Library; if not, see
  22    <https://www.gnu.org/licenses/>.
  23 
  24    This file is part of the Midnight Commander.
  25 
  26    The Midnight Commander is free software: you can redistribute it
  27    and/or modify it under the terms of the GNU General Public License as
  28    published by the Free Software Foundation, either version 3 of the License,
  29    or (at your option) any later version.
  30 
  31    The Midnight Commander is distributed in the hope that it will be useful,
  32    but WITHOUT ANY WARRANTY; without even the implied warranty of
  33    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  34    GNU General Public License for more details.
  35 
  36    You should have received a copy of the GNU General Public License
  37    along with this program.  If not, see <http://www.gnu.org/licenses/>.
  38  */
  39 
  40 #include <config.h>
  41 
  42 #ifdef HAVE_STRVERSCMP
  43 #include <string.h>
  44 #endif /* HAVE_STRVERSCMP */
  45 
  46 #include "lib/strutil.h"
  47 
  48 /*** global variables ****************************************************************************/
  49 
  50 /*** file scope macro definitions ****************************************************************/
  51 
  52 #ifndef HAVE_STRVERSCMP
  53 
  54 /* states: S_N: normal, S_I: comparing integral part, S_F: comparing
  55    fractional parts, S_Z: idem but with leading Zeroes only */
  56 #define  S_N    0x0
  57 #define  S_I    0x3
  58 #define  S_F    0x6
  59 #define  S_Z    0x9
  60 
  61 /* result_type: CMP: return diff; LEN: compare using len_diff/diff */
  62 #define  CMP    2
  63 #define  LEN    3
  64 
  65 #endif /* HAVE_STRVERSCMP */
  66 
  67 /*** file scope type declarations ****************************************************************/
  68 
  69 /*** file scope variables ************************************************************************/
  70 
  71 /* --------------------------------------------------------------------------------------------- */
  72 /*** file scope functions ************************************************************************/
  73 /* --------------------------------------------------------------------------------------------- */
  74 
  75 /* --------------------------------------------------------------------------------------------- */
  76 /*** public functions ****************************************************************************/
  77 /* --------------------------------------------------------------------------------------------- */
  78 /* Compare S1 and S2 as strings holding indices/version numbers,
  79    returning less than, equal to or greater than zero if S1 is less than,
  80    equal to or greater than S2 (for more info, see the texinfo doc).
  81  */
  82 int
  83 str_verscmp (const char *s1, const char *s2)
     /* [previous][next][first][last][top][bottom][index][help]  */
  84 {
  85 #ifdef HAVE_STRVERSCMP
  86     return strverscmp (s1, s2);
  87 
  88 #else /* HAVE_STRVERSCMP */
  89     const unsigned char *p1 = (const unsigned char *) s1;
  90     const unsigned char *p2 = (const unsigned char *) s2;
  91     unsigned char c1, c2;
  92     int state;
  93     int diff;
  94 
  95     /* *INDENT-OFF* */
  96     /* Symbol(s)    0       [1-9]   others
  97        Transition   (10) 0  (01) d  (00) x   */
  98     static const unsigned char next_state[] =
  99     {
 100         /* state    x    d    0  */
 101         /* S_N */ S_N, S_I, S_Z,
 102         /* S_I */ S_N, S_I, S_I,
 103         /* S_F */ S_N, S_F, S_F,
 104         /* S_Z */ S_N, S_F, S_Z
 105     };
 106 
 107     static const signed char result_type[] =
 108     {
 109         /* state  x/x  x/d  x/0  d/x  d/d  d/0  0/x  0/d  0/0  */
 110 
 111         /* S_N */ CMP, CMP, CMP, CMP, LEN, CMP, CMP, CMP, CMP,
 112         /* S_I */ CMP,  -1,  -1,  +1, LEN, LEN,  +1, LEN, LEN,
 113         /* S_F */ CMP, CMP, CMP, CMP, CMP, CMP, CMP, CMP, CMP,
 114         /* S_Z */ CMP,  +1,  +1,  -1, CMP, CMP,  -1, CMP, CMP
 115     };
 116     /* *INDENT-ON* */
 117 
 118     if (p1 == p2)
 119         return 0;
 120 
 121     c1 = *p1++;
 122     c2 = *p2++;
 123     /* Hint: '0' is a digit too.  */
 124     state = S_N + ((c1 == '0') + (g_ascii_isdigit (c1) ? 1 : 0));
 125 
 126     while ((diff = c1 - c2) == 0)
 127     {
 128         if (c1 == '\0')
 129             return diff;
 130 
 131         state = next_state[state];
 132         c1 = *p1++;
 133         c2 = *p2++;
 134         state += (c1 == '0') + (g_ascii_isdigit (c1) ? 1 : 0);
 135     }
 136 
 137     state = result_type[state * 3 + (((c2 == '0') + (g_ascii_isdigit (c2) ? 1 : 0)))];
 138 
 139     switch (state)
 140     {
 141     case CMP:
 142         return diff;
 143 
 144     case LEN:
 145         while (g_ascii_isdigit (*p1++))
 146             if (!g_ascii_isdigit (*p2++))
 147                 return 1;
 148 
 149         return g_ascii_isdigit (*p2) ? -1 : diff;
 150 
 151     default:
 152         return state;
 153     }
 154 #endif /* HAVE_STRVERSCMP */
 155 }
 156 
 157 /* --------------------------------------------------------------------------------------------- */

/* [previous][next][first][last][top][bottom][index][help]  */