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