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