1 /* GNU's strverscmp() function, taken from glibc 2.3.2 sources
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.
9 Derived work for uClibc by Hai Zaar, Codefidence Ltd <haizaar@codefidence.com>
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.
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.
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
31 /* states: S_N: normal, S_I: comparing integral part, S_F: comparing
32 fractional parts, S_Z: idem but with leading Zeroes only */
38 /* result_type: CMP: return diff; LEN: compare using len_diff/diff */
42 /* using more efficient isdigit() */
44 #define isdigit(a) ((unsigned)((a) - '0') <= 9)
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).
50 int strverscmp (const char *s1, const char *s2)
52 const unsigned char *p1 = (const unsigned char *) s1;
53 const unsigned char *p2 = (const unsigned char *) s2;
58 /* Symbol(s) 0 [1-9] others (padding)
59 Transition (10) 0 (01) d (00) x (11) - */
60 static const uint8_t next_state[] =
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
69 static const int8_t result_type[] =
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 -/- */
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,
89 /* Hint: '0' is a digit too. */
90 state = S_N | ((c1 == '0') + (isdigit (c1) != 0));
92 while ((diff = c1 - c2) == 0 && c1 != '\0')
94 state = next_state[state];
97 state |= (c1 == '0') + (isdigit (c1) != 0);
100 state = result_type[state << 2 | (((c2 == '0') + (isdigit (c2) != 0)))];
108 while (isdigit (*p1++))
109 if (!isdigit (*p2++))
112 return isdigit (*p2) ? -1 : diff;
118 libc_hidden_def(strverscmp)