While scanning directories, print directory counter and size
[midnight-commander.git] / lib / strutil / strverscmp.c
blob606bf89606de6b9d3f19fca56578a36f6eb98072
1 /*
2 Compare strings while treating digits characters numerically.
4 Copyright (C) 1997, 2002, 2011
5 The 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 /* states: S_N: normal, S_I: comparing integral part, S_F: comparing
36 fractionnal parts, S_Z: idem but with leading Zeroes only */
37 #define S_N 0x0
38 #define S_I 0x4
39 #define S_F 0x8
40 #define S_Z 0xC
42 /* result_type: CMP: return diff; LEN: compare using len_diff/diff */
43 #define CMP 2
44 #define LEN 3
47 /* Compare S1 and S2 as strings holding indices/version numbers,
48 returning less than, equal to or greater than zero if S1 is less than,
49 equal to or greater than S2 (for more info, see the texinfo doc).
52 int
53 str_verscmp (const char *s1, const char *s2)
55 #ifdef HAVE_STRVERSCMP
56 return strverscmp (s1, s2);
57 #else /* HAVE_STRVERSCMP */
58 unsigned char *p1 = (unsigned char *) s1;
59 unsigned char *p2 = (unsigned char *) s2;
60 unsigned char c1, c2;
61 int state;
62 int diff;
64 /* Symbol(s) 0 [1-9] others (padding)
65 Transition (10) 0 (01) d (00) x (11) - */
66 static const unsigned int next_state[] = {
67 /* state x d 0 - */
68 /* S_N */ S_N, S_I, S_Z, S_N,
69 /* S_I */ S_N, S_I, S_I, S_I,
70 /* S_F */ S_N, S_F, S_F, S_F,
71 /* S_Z */ S_N, S_F, S_Z, S_Z
74 static const int result_type[] = {
75 /* state x/x x/d x/0 x/- d/x d/d d/0 d/-
76 0/x 0/d 0/0 0/- -/x -/d -/0 -/- */
78 /* S_N */ CMP, CMP, CMP, CMP, CMP, LEN, CMP, CMP,
79 CMP, CMP, CMP, CMP, CMP, CMP, CMP, CMP,
80 /* S_I */ CMP, -1, -1, CMP, +1, LEN, LEN, CMP,
81 +1, LEN, LEN, CMP, CMP, CMP, CMP, CMP,
82 /* S_F */ CMP, CMP, CMP, CMP, CMP, LEN, CMP, CMP,
83 CMP, CMP, CMP, CMP, CMP, CMP, CMP, CMP,
84 /* S_Z */ CMP, +1, +1, CMP, -1, CMP, CMP, CMP,
85 -1, CMP, CMP, CMP
88 if (p1 == p2)
89 return 0;
91 c1 = *p1++;
92 c2 = *p2++;
93 /* Hint: '0' is a digit too. */
94 state = S_N | ((c1 == '0') + (isdigit (c1) != 0));
96 while ((diff = c1 - c2) == 0 && c1 != '\0')
98 state = next_state[state];
99 c1 = *p1++;
100 c2 = *p2++;
101 state |= (c1 == '0') + (isdigit (c1) != 0);
104 state = result_type[state << 2 | (((c2 == '0') + (isdigit (c2) != 0)))];
106 switch (state)
108 case CMP:
109 return diff;
111 case LEN:
112 while (isdigit (*p1++))
113 if (!isdigit (*p2++))
114 return 1;
116 return isdigit (*p2) ? -1 : diff;
118 default:
119 return state;
121 #endif /* HAVE_STRVERSCMP */