Merge branch '3684_mc_profile_root'
[midnight-commander.git] / lib / strutil / strverscmp.c
blob81f878a832a275b78f4528b7b861d5dc6a678e96
1 /*
2 Compare strings while treating digits characters numerically.
4 Copyright (C) 1997-2016
5 Free Software Foundation, Inc.
7 This file is part of the GNU C Library.
8 Contributed by Jean-Fran�ois Bignolles <bignolle ecoledoc ibp fr>, 1997.
10 This file is part of the Midnight Commander.
12 The Midnight Commander is free software: you can redistribute it
13 and/or modify it under the terms of the GNU General Public License as
14 published by the Free Software Foundation, either version 3 of the License,
15 or (at your option) any later version.
17 The Midnight Commander is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 GNU General Public License for more details.
22 You should have received a copy of the GNU General Public License
23 along with this program. If not, see <http://www.gnu.org/licenses/>.
26 #include <config.h>
28 #include <ctype.h>
29 #ifdef HAVE_STRVERSCMP
30 #include <string.h>
31 #endif /* HAVE_STRVERSCMP */
33 #include "lib/strutil.h"
35 /*** global variables ****************************************************************************/
37 /*** file scope macro definitions ****************************************************************/
39 #ifndef HAVE_STRVERSCMP
41 /* states: S_N: normal, S_I: comparing integral part, S_F: comparing
42 fractionnal parts, S_Z: idem but with leading Zeroes only */
43 #define S_N 0x0
44 #define S_I 0x4
45 #define S_F 0x8
46 #define S_Z 0xC
48 /* result_type: CMP: return diff; LEN: compare using len_diff/diff */
49 #define CMP 2
50 #define LEN 3
52 #endif /* HAVE_STRVERSCMP */
54 /*** file scope type declarations ****************************************************************/
56 /*** file scope variables ************************************************************************/
58 /* --------------------------------------------------------------------------------------------- */
59 /*** file scope functions ************************************************************************/
60 /* --------------------------------------------------------------------------------------------- */
62 /* --------------------------------------------------------------------------------------------- */
63 /*** public functions ****************************************************************************/
64 /* --------------------------------------------------------------------------------------------- */
65 /* Compare S1 and S2 as strings holding indices/version numbers,
66 returning less than, equal to or greater than zero if S1 is less than,
67 equal to or greater than S2 (for more info, see the texinfo doc).
69 int
70 str_verscmp (const char *s1, const char *s2)
72 #ifdef HAVE_STRVERSCMP
73 return strverscmp (s1, s2);
75 #else /* HAVE_STRVERSCMP */
76 unsigned char *p1 = (unsigned char *) s1;
77 unsigned char *p2 = (unsigned char *) s2;
78 unsigned char c1, c2;
79 int state;
80 int diff;
82 /* Symbol(s) 0 [1-9] others (padding)
83 Transition (10) 0 (01) d (00) x (11) - */
84 static const unsigned int next_state[] = {
85 /* state x d 0 - */
86 /* S_N */ S_N, S_I, S_Z, S_N,
87 /* S_I */ S_N, S_I, S_I, S_I,
88 /* S_F */ S_N, S_F, S_F, S_F,
89 /* S_Z */ S_N, S_F, S_Z, S_Z
92 static const int result_type[] = {
93 /* state x/x x/d x/0 x/- d/x d/d d/0 d/-
94 0/x 0/d 0/0 0/- -/x -/d -/0 -/- */
96 /* S_N */ CMP, CMP, CMP, CMP, CMP, LEN, CMP, CMP,
97 CMP, CMP, CMP, CMP, CMP, CMP, CMP, CMP,
98 /* S_I */ CMP, -1, -1, CMP, +1, LEN, LEN, CMP,
99 +1, LEN, LEN, CMP, CMP, CMP, CMP, CMP,
100 /* S_F */ CMP, CMP, CMP, CMP, CMP, LEN, CMP, CMP,
101 CMP, CMP, CMP, CMP, CMP, CMP, CMP, CMP,
102 /* S_Z */ CMP, +1, +1, CMP, -1, CMP, CMP, CMP,
103 -1, CMP, CMP, CMP
106 if (p1 == p2)
107 return 0;
109 c1 = *p1++;
110 c2 = *p2++;
111 /* Hint: '0' is a digit too. */
112 state = S_N | ((c1 == '0') + (isdigit (c1) != 0));
114 while ((diff = c1 - c2) == 0 && c1 != '\0')
116 state = next_state[state];
117 c1 = *p1++;
118 c2 = *p2++;
119 state |= (c1 == '0') + (isdigit (c1) != 0);
122 state = result_type[state << 2 | (((c2 == '0') + (isdigit (c2) != 0)))];
124 switch (state)
126 case CMP:
127 return diff;
129 case LEN:
130 while (isdigit (*p1++))
131 if (!isdigit (*p2++))
132 return 1;
134 return isdigit (*p2) ? -1 : diff;
136 default:
137 return state;
139 #endif /* HAVE_STRVERSCMP */
142 /* --------------------------------------------------------------------------------------------- */