1*e4b17023SJohn Marino /* Compare strings while treating digits characters numerically.
2*e4b17023SJohn Marino    Copyright (C) 1997, 2002, 2005 Free Software Foundation, Inc.
3*e4b17023SJohn Marino    This file is part of the libiberty library.
4*e4b17023SJohn Marino    Contributed by Jean-Fran�ois Bignolles <bignolle@ecoledoc.ibp.fr>, 1997.
5*e4b17023SJohn Marino 
6*e4b17023SJohn Marino    Libiberty is free software; you can redistribute it and/or
7*e4b17023SJohn Marino    modify it under the terms of the GNU Lesser General Public
8*e4b17023SJohn Marino    License as published by the Free Software Foundation; either
9*e4b17023SJohn Marino    version 2.1 of the License, or (at your option) any later version.
10*e4b17023SJohn Marino 
11*e4b17023SJohn Marino    Libiberty is distributed in the hope that it will be useful,
12*e4b17023SJohn Marino    but WITHOUT ANY WARRANTY; without even the implied warranty of
13*e4b17023SJohn Marino    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14*e4b17023SJohn Marino    Lesser General Public License for more details.
15*e4b17023SJohn Marino 
16*e4b17023SJohn Marino    You should have received a copy of the GNU Lesser General Public
17*e4b17023SJohn Marino    License along with the GNU C Library; if not, write to the Free
18*e4b17023SJohn Marino    Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19*e4b17023SJohn Marino    02110-1301 USA.  */
20*e4b17023SJohn Marino 
21*e4b17023SJohn Marino #include "libiberty.h"
22*e4b17023SJohn Marino #include "safe-ctype.h"
23*e4b17023SJohn Marino 
24*e4b17023SJohn Marino /*
25*e4b17023SJohn Marino @deftypefun int strverscmp (const char *@var{s1}, const char *@var{s2})
26*e4b17023SJohn Marino The @code{strverscmp} function compares the string @var{s1} against
27*e4b17023SJohn Marino @var{s2}, considering them as holding indices/version numbers.  Return
28*e4b17023SJohn Marino value follows the same conventions as found in the @code{strverscmp}
29*e4b17023SJohn Marino function.  In fact, if @var{s1} and @var{s2} contain no digits,
30*e4b17023SJohn Marino @code{strverscmp} behaves like @code{strcmp}.
31*e4b17023SJohn Marino 
32*e4b17023SJohn Marino Basically, we compare strings normally (character by character), until
33*e4b17023SJohn Marino we find a digit in each string - then we enter a special comparison
34*e4b17023SJohn Marino mode, where each sequence of digits is taken as a whole.  If we reach the
35*e4b17023SJohn Marino end of these two parts without noticing a difference, we return to the
36*e4b17023SJohn Marino standard comparison mode.  There are two types of numeric parts:
37*e4b17023SJohn Marino "integral" and "fractional" (those  begin with a '0'). The types
38*e4b17023SJohn Marino of the numeric parts affect the way we sort them:
39*e4b17023SJohn Marino 
40*e4b17023SJohn Marino @itemize @bullet
41*e4b17023SJohn Marino @item
42*e4b17023SJohn Marino integral/integral: we compare values as you would expect.
43*e4b17023SJohn Marino 
44*e4b17023SJohn Marino @item
45*e4b17023SJohn Marino fractional/integral: the fractional part is less than the integral one.
46*e4b17023SJohn Marino Again, no surprise.
47*e4b17023SJohn Marino 
48*e4b17023SJohn Marino @item
49*e4b17023SJohn Marino fractional/fractional: the things become a bit more complex.
50*e4b17023SJohn Marino If the common prefix contains only leading zeroes, the longest part is less
51*e4b17023SJohn Marino than the other one; else the comparison behaves normally.
52*e4b17023SJohn Marino @end itemize
53*e4b17023SJohn Marino 
54*e4b17023SJohn Marino @smallexample
55*e4b17023SJohn Marino strverscmp ("no digit", "no digit")
56*e4b17023SJohn Marino     @result{} 0    // @r{same behavior as strcmp.}
57*e4b17023SJohn Marino strverscmp ("item#99", "item#100")
58*e4b17023SJohn Marino     @result{} <0   // @r{same prefix, but 99 < 100.}
59*e4b17023SJohn Marino strverscmp ("alpha1", "alpha001")
60*e4b17023SJohn Marino     @result{} >0   // @r{fractional part inferior to integral one.}
61*e4b17023SJohn Marino strverscmp ("part1_f012", "part1_f01")
62*e4b17023SJohn Marino     @result{} >0   // @r{two fractional parts.}
63*e4b17023SJohn Marino strverscmp ("foo.009", "foo.0")
64*e4b17023SJohn Marino     @result{} <0   // @r{idem, but with leading zeroes only.}
65*e4b17023SJohn Marino @end smallexample
66*e4b17023SJohn Marino 
67*e4b17023SJohn Marino This function is especially useful when dealing with filename sorting,
68*e4b17023SJohn Marino because filenames frequently hold indices/version numbers.
69*e4b17023SJohn Marino @end deftypefun
70*e4b17023SJohn Marino 
71*e4b17023SJohn Marino */
72*e4b17023SJohn Marino 
73*e4b17023SJohn Marino /* states: S_N: normal, S_I: comparing integral part, S_F: comparing
74*e4b17023SJohn Marino            fractional parts, S_Z: idem but with leading Zeroes only */
75*e4b17023SJohn Marino #define  S_N    0x0
76*e4b17023SJohn Marino #define  S_I    0x4
77*e4b17023SJohn Marino #define  S_F    0x8
78*e4b17023SJohn Marino #define  S_Z    0xC
79*e4b17023SJohn Marino 
80*e4b17023SJohn Marino /* result_type: CMP: return diff; LEN: compare using len_diff/diff */
81*e4b17023SJohn Marino #define  CMP    2
82*e4b17023SJohn Marino #define  LEN    3
83*e4b17023SJohn Marino 
84*e4b17023SJohn Marino 
85*e4b17023SJohn Marino /* Compare S1 and S2 as strings holding indices/version numbers,
86*e4b17023SJohn Marino    returning less than, equal to or greater than zero if S1 is less than,
87*e4b17023SJohn Marino    equal to or greater than S2 (for more info, see the Glibc texinfo doc).  */
88*e4b17023SJohn Marino 
89*e4b17023SJohn Marino int
strverscmp(const char * s1,const char * s2)90*e4b17023SJohn Marino strverscmp (const char *s1, const char *s2)
91*e4b17023SJohn Marino {
92*e4b17023SJohn Marino   const unsigned char *p1 = (const unsigned char *) s1;
93*e4b17023SJohn Marino   const unsigned char *p2 = (const unsigned char *) s2;
94*e4b17023SJohn Marino   unsigned char c1, c2;
95*e4b17023SJohn Marino   int state;
96*e4b17023SJohn Marino   int diff;
97*e4b17023SJohn Marino 
98*e4b17023SJohn Marino   /* Symbol(s)    0       [1-9]   others  (padding)
99*e4b17023SJohn Marino      Transition   (10) 0  (01) d  (00) x  (11) -   */
100*e4b17023SJohn Marino   static const unsigned int next_state[] =
101*e4b17023SJohn Marino     {
102*e4b17023SJohn Marino       /* state    x    d    0    - */
103*e4b17023SJohn Marino       /* S_N */  S_N, S_I, S_Z, S_N,
104*e4b17023SJohn Marino       /* S_I */  S_N, S_I, S_I, S_I,
105*e4b17023SJohn Marino       /* S_F */  S_N, S_F, S_F, S_F,
106*e4b17023SJohn Marino       /* S_Z */  S_N, S_F, S_Z, S_Z
107*e4b17023SJohn Marino     };
108*e4b17023SJohn Marino 
109*e4b17023SJohn Marino   static const int result_type[] =
110*e4b17023SJohn Marino     {
111*e4b17023SJohn Marino       /* state   x/x  x/d  x/0  x/-  d/x  d/d  d/0  d/-
112*e4b17023SJohn Marino                  0/x  0/d  0/0  0/-  -/x  -/d  -/0  -/- */
113*e4b17023SJohn Marino 
114*e4b17023SJohn Marino       /* S_N */  CMP, CMP, CMP, CMP, CMP, LEN, CMP, CMP,
115*e4b17023SJohn Marino                  CMP, CMP, CMP, CMP, CMP, CMP, CMP, CMP,
116*e4b17023SJohn Marino       /* S_I */  CMP, -1,  -1,  CMP, +1,  LEN, LEN, CMP,
117*e4b17023SJohn Marino                  +1,  LEN, LEN, CMP, CMP, CMP, CMP, CMP,
118*e4b17023SJohn Marino       /* S_F */  CMP, CMP, CMP, CMP, CMP, LEN, CMP, CMP,
119*e4b17023SJohn Marino                  CMP, CMP, CMP, CMP, CMP, CMP, CMP, CMP,
120*e4b17023SJohn Marino       /* S_Z */  CMP, +1,  +1,  CMP, -1,  CMP, CMP, CMP,
121*e4b17023SJohn Marino                  -1,  CMP, CMP, CMP
122*e4b17023SJohn Marino     };
123*e4b17023SJohn Marino 
124*e4b17023SJohn Marino   if (p1 == p2)
125*e4b17023SJohn Marino     return 0;
126*e4b17023SJohn Marino 
127*e4b17023SJohn Marino   c1 = *p1++;
128*e4b17023SJohn Marino   c2 = *p2++;
129*e4b17023SJohn Marino   /* Hint: '0' is a digit too.  */
130*e4b17023SJohn Marino   state = S_N | ((c1 == '0') + (ISDIGIT (c1) != 0));
131*e4b17023SJohn Marino 
132*e4b17023SJohn Marino   while ((diff = c1 - c2) == 0 && c1 != '\0')
133*e4b17023SJohn Marino     {
134*e4b17023SJohn Marino       state = next_state[state];
135*e4b17023SJohn Marino       c1 = *p1++;
136*e4b17023SJohn Marino       c2 = *p2++;
137*e4b17023SJohn Marino       state |= (c1 == '0') + (ISDIGIT (c1) != 0);
138*e4b17023SJohn Marino     }
139*e4b17023SJohn Marino 
140*e4b17023SJohn Marino   state = result_type[state << 2 | (((c2 == '0') + (ISDIGIT (c2) != 0)))];
141*e4b17023SJohn Marino 
142*e4b17023SJohn Marino   switch (state)
143*e4b17023SJohn Marino     {
144*e4b17023SJohn Marino     case CMP:
145*e4b17023SJohn Marino       return diff;
146*e4b17023SJohn Marino 
147*e4b17023SJohn Marino     case LEN:
148*e4b17023SJohn Marino       while (ISDIGIT (*p1++))
149*e4b17023SJohn Marino 	if (!ISDIGIT (*p2++))
150*e4b17023SJohn Marino 	  return 1;
151*e4b17023SJohn Marino 
152*e4b17023SJohn Marino       return ISDIGIT (*p2) ? -1 : diff;
153*e4b17023SJohn Marino 
154*e4b17023SJohn Marino     default:
155*e4b17023SJohn Marino       return state;
156*e4b17023SJohn Marino     }
157*e4b17023SJohn Marino }
158