]> rtime.felk.cvut.cz Git - l4.git/blob - l4/pkg/uclibc/lib/contrib/uclibc/libc/string/strverscmp.c
74ae4c6ad37e63f5d817b29fe26e35957fa90aa5
[l4.git] / l4 / pkg / uclibc / lib / contrib / uclibc / libc / string / strverscmp.c
1 /* GNU's strverscmp() function, taken from glibc 2.3.2 sources
2  */
3
4 /* Compare strings while treating digits characters numerically.
5    Copyright (C) 1997, 2002 Free Software Foundation, Inc.
6    This file is part of the GNU C Library.
7    Contributed by Jean-François Bignolles <bignolle@ecoledoc.ibp.fr>, 1997.
8
9    Derived work for uClibc by Hai Zaar, Codefidence Ltd <haizaar@codefidence.com>
10
11    The GNU C Library is free software; you can redistribute it and/or
12    modify it under the terms of the GNU Lesser General Public
13    License as published by the Free Software Foundation; either
14    version 2.1 of the License, or (at your option) any later version.
15
16    The GNU C Library is distributed in the hope that it will be useful,
17    but WITHOUT ANY WARRANTY; without even the implied warranty of
18    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
19    Lesser General Public License for more details.
20
21    You should have received a copy of the GNU Lesser General Public
22    License along with the GNU C Library; if not, write to the Free
23    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
24    02111-1307 USA.  */
25
26 #include <string.h>
27 #include <ctype.h>
28 #include <stdint.h>
29
30
31 /* states: S_N: normal, S_I: comparing integral part, S_F: comparing
32    fractional parts, S_Z: idem but with leading Zeroes only */
33 #define  S_N    0x0
34 #define  S_I    0x4
35 #define  S_F    0x8
36 #define  S_Z    0xC
37
38 /* result_type: CMP: return diff; LEN: compare using len_diff/diff */
39 #define  CMP    2
40 #define  LEN    3
41
42 /* using more efficient isdigit() */
43 #undef isdigit
44 #define isdigit(a) ((unsigned)((a) - '0') <= 9)
45
46 /* Compare S1 and S2 as strings holding indices/version numbers,
47    returning less than, equal to or greater than zero if S1 is less than,
48    equal to or greater than S2 (for more info, see the texinfo doc).
49 */
50 int strverscmp (const char *s1, const char *s2)
51 {
52   const unsigned char *p1 = (const unsigned char *) s1;
53   const unsigned char *p2 = (const unsigned char *) s2;
54   unsigned char c1, c2;
55   int state;
56   int diff;
57
58   /* Symbol(s)    0       [1-9]   others  (padding)
59      Transition   (10) 0  (01) d  (00) x  (11) -   */
60   static const uint8_t next_state[] =
61   {
62       /* state    x    d    0    - */
63       /* S_N */  S_N, S_I, S_Z, S_N,
64       /* S_I */  S_N, S_I, S_I, S_I,
65       /* S_F */  S_N, S_F, S_F, S_F,
66       /* S_Z */  S_N, S_F, S_Z, S_Z
67   };
68
69   static const int8_t result_type[] =
70   {
71       /* state   x/x  x/d  x/0  x/-  d/x  d/d  d/0  d/-
72                  0/x  0/d  0/0  0/-  -/x  -/d  -/0  -/- */
73
74       /* S_N */  CMP, CMP, CMP, CMP, CMP, LEN, CMP, CMP,
75                  CMP, CMP, CMP, CMP, CMP, CMP, CMP, CMP,
76       /* S_I */  CMP, -1,  -1,  CMP, +1,  LEN, LEN, CMP,
77                  +1,  LEN, LEN, CMP, CMP, CMP, CMP, CMP,
78       /* S_F */  CMP, CMP, CMP, CMP, CMP, LEN, CMP, CMP,
79                  CMP, CMP, CMP, CMP, CMP, CMP, CMP, CMP,
80       /* S_Z */  CMP, +1,  +1,  CMP, -1,  CMP, CMP, CMP,
81                  -1,  CMP, CMP, CMP
82   };
83
84   if (p1 == p2)
85     return 0;
86
87   c1 = *p1++;
88   c2 = *p2++;
89   /* Hint: '0' is a digit too.  */
90   state = S_N | ((c1 == '0') + (isdigit (c1) != 0));
91
92   while ((diff = c1 - c2) == 0 && c1 != '\0')
93     {
94       state = next_state[state];
95       c1 = *p1++;
96       c2 = *p2++;
97       state |= (c1 == '0') + (isdigit (c1) != 0);
98     }
99
100   state = result_type[state << 2 | (((c2 == '0') + (isdigit (c2) != 0)))];
101
102   switch (state)
103   {
104     case CMP:
105       return diff;
106
107     case LEN:
108       while (isdigit (*p1++))
109         if (!isdigit (*p2++))
110           return 1;
111
112       return isdigit (*p2) ? -1 : diff;
113
114     default:
115       return state;
116   }
117 }