]> rtime.felk.cvut.cz Git - l4.git/blob - l4/pkg/uclibc/lib/contrib/uclibc/libc/string/strverscmp.c
update
[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, see
23    <http://www.gnu.org/licenses/>.  */
24
25 #include <string.h>
26 #include <ctype.h>
27 #include <stdint.h>
28
29
30 /* states: S_N: normal, S_I: comparing integral part, S_F: comparing
31    fractional parts, S_Z: idem but with leading Zeroes only */
32 #define  S_N    0x0
33 #define  S_I    0x4
34 #define  S_F    0x8
35 #define  S_Z    0xC
36
37 /* result_type: CMP: return diff; LEN: compare using len_diff/diff */
38 #define  CMP    2
39 #define  LEN    3
40
41 /* using more efficient isdigit() */
42 #undef isdigit
43 #define isdigit(a) ((unsigned)((a) - '0') <= 9)
44
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).
48 */
49 int strverscmp (const char *s1, const char *s2)
50 {
51   const unsigned char *p1 = (const unsigned char *) s1;
52   const unsigned char *p2 = (const unsigned char *) s2;
53   unsigned char c1, c2;
54   int state;
55   int diff;
56
57   /* Symbol(s)    0       [1-9]   others  (padding)
58      Transition   (10) 0  (01) d  (00) x  (11) -   */
59   static const uint8_t next_state[] =
60   {
61       /* state    x    d    0    - */
62       /* S_N */  S_N, S_I, S_Z, S_N,
63       /* S_I */  S_N, S_I, S_I, S_I,
64       /* S_F */  S_N, S_F, S_F, S_F,
65       /* S_Z */  S_N, S_F, S_Z, S_Z
66   };
67
68   static const int8_t result_type[] =
69   {
70       /* state   x/x  x/d  x/0  x/-  d/x  d/d  d/0  d/-
71                  0/x  0/d  0/0  0/-  -/x  -/d  -/0  -/- */
72
73       /* S_N */  CMP, CMP, CMP, CMP, CMP, LEN, CMP, CMP,
74                  CMP, CMP, CMP, CMP, CMP, CMP, CMP, CMP,
75       /* S_I */  CMP, -1,  -1,  CMP, +1,  LEN, LEN, CMP,
76                  +1,  LEN, LEN, CMP, CMP, CMP, CMP, CMP,
77       /* S_F */  CMP, CMP, CMP, CMP, CMP, LEN, CMP, CMP,
78                  CMP, CMP, CMP, CMP, CMP, CMP, CMP, CMP,
79       /* S_Z */  CMP, +1,  +1,  CMP, -1,  CMP, CMP, CMP,
80                  -1,  CMP, CMP, CMP
81   };
82
83   if (p1 == p2)
84     return 0;
85
86   c1 = *p1++;
87   c2 = *p2++;
88   /* Hint: '0' is a digit too.  */
89   state = S_N | ((c1 == '0') + (isdigit (c1) != 0));
90
91   while ((diff = c1 - c2) == 0 && c1 != '\0')
92     {
93       state = next_state[state];
94       c1 = *p1++;
95       c2 = *p2++;
96       state |= (c1 == '0') + (isdigit (c1) != 0);
97     }
98
99   state = result_type[state << 2 | (((c2 == '0') + (isdigit (c2) != 0)))];
100
101   switch (state)
102   {
103     case CMP:
104       return diff;
105
106     case LEN:
107       while (isdigit (*p1++))
108         if (!isdigit (*p2++))
109           return 1;
110
111       return isdigit (*p2) ? -1 : diff;
112
113     default:
114       return state;
115   }
116 }
117 libc_hidden_def(strverscmp)