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/>.
29 #ifdef HAVE_STRVERSCMP
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 */
42 /* result_type: CMP: return diff; LEN: compare using len_diff/diff */
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).
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
;
64 /* Symbol(s) 0 [1-9] others (padding)
65 Transition (10) 0 (01) d (00) x (11) - */
66 static const unsigned int next_state
[] = {
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
,
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
];
101 state
|= (c1
== '0') + (isdigit (c1
) != 0);
104 state
= result_type
[state
<< 2 | (((c2
== '0') + (isdigit (c2
) != 0)))];
112 while (isdigit (*p1
++))
113 if (!isdigit (*p2
++))
116 return isdigit (*p2
) ? -1 : diff
;
121 #endif /* HAVE_STRVERSCMP */