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/>.
27 #ifdef HAVE_STRVERSCMP
29 #endif /* HAVE_STRVERSCMP */
31 #include "lib/strutil.h"
33 /* states: S_N: normal, S_I: comparing integral part, S_F: comparing
34 fractionnal parts, S_Z: idem but with leading Zeroes only */
40 /* result_type: CMP: return diff; LEN: compare using len_diff/diff */
45 /* Compare S1 and S2 as strings holding indices/version numbers,
46 returning less than, equal to or greater than zero if S1 is less than,
47 equal to or greater than S2 (for more info, see the texinfo doc).
51 str_verscmp (const char *s1
, const char *s2
)
53 #ifdef HAVE_STRVERSCMP
54 return strverscmp (s1
, s2
);
55 #else /* HAVE_STRVERSCMP */
56 unsigned char *p1
= (unsigned char *) s1
;
57 unsigned char *p2
= (unsigned char *) s2
;
62 /* Symbol(s) 0 [1-9] others (padding)
63 Transition (10) 0 (01) d (00) x (11) - */
64 static const unsigned int next_state
[] = {
66 /* S_N */ S_N
, S_I
, S_Z
, S_N
,
67 /* S_I */ S_N
, S_I
, S_I
, S_I
,
68 /* S_F */ S_N
, S_F
, S_F
, S_F
,
69 /* S_Z */ S_N
, S_F
, S_Z
, S_Z
72 static const int result_type
[] = {
73 /* state x/x x/d x/0 x/- d/x d/d d/0 d/-
74 0/x 0/d 0/0 0/- -/x -/d -/0 -/- */
76 /* S_N */ CMP
, CMP
, CMP
, CMP
, CMP
, LEN
, CMP
, CMP
,
77 CMP
, CMP
, CMP
, CMP
, CMP
, CMP
, CMP
, CMP
,
78 /* S_I */ CMP
, -1, -1, CMP
, +1, LEN
, LEN
, CMP
,
79 +1, LEN
, LEN
, CMP
, CMP
, CMP
, CMP
, CMP
,
80 /* S_F */ CMP
, CMP
, CMP
, CMP
, CMP
, LEN
, CMP
, CMP
,
81 CMP
, CMP
, CMP
, CMP
, CMP
, CMP
, CMP
, CMP
,
82 /* S_Z */ CMP
, +1, +1, CMP
, -1, CMP
, CMP
, CMP
,
91 /* Hint: '0' is a digit too. */
92 state
= S_N
| ((c1
== '0') + (isdigit (c1
) != 0));
94 while ((diff
= c1
- c2
) == 0 && c1
!= '\0')
96 state
= next_state
[state
];
99 state
|= (c1
== '0') + (isdigit (c1
) != 0);
102 state
= result_type
[state
<< 2 | (((c2
== '0') + (isdigit (c2
) != 0)))];
110 while (isdigit (*p1
++))
111 if (!isdigit (*p2
++))
114 return isdigit (*p2
) ? -1 : diff
;
119 #endif /* HAVE_STRVERSCMP */