4 * versioncmp(): copied from string/strverscmp.c in glibc commit
5 * ee9247c38a8def24a59eb5cfb7196a98bef8cfdc, reformatted to Git coding
6 * style. The implementation is under LGPL-2.1 and Git relicenses it
11 * states: S_N: normal, S_I: comparing integral part, S_F: comparing
12 * fractionnal parts, S_Z: idem but with leading Zeroes only
19 /* result_type: CMP: return diff; LEN: compare using len_diff/diff */
25 * Compare S1 and S2 as strings holding indices/version numbers,
26 * returning less than, equal to or greater than zero if S1 is less
27 * than, equal to or greater than S2 (for more info, see the texinfo
31 int versioncmp(const char *s1
, const char *s2
)
33 const unsigned char *p1
= (const unsigned char *) s1
;
34 const unsigned char *p2
= (const unsigned char *) s2
;
39 * Symbol(s) 0 [1-9] others
40 * Transition (10) 0 (01) d (00) x
42 static const uint8_t next_state
[] = {
44 /* S_N */ S_N
, S_I
, S_Z
,
45 /* S_I */ S_N
, S_I
, S_I
,
46 /* S_F */ S_N
, S_F
, S_F
,
47 /* S_Z */ S_N
, S_F
, S_Z
50 static const int8_t result_type
[] = {
51 /* state x/x x/d x/0 d/x d/d d/0 0/x 0/d 0/0 */
53 /* S_N */ CMP
, CMP
, CMP
, CMP
, LEN
, CMP
, CMP
, CMP
, CMP
,
54 /* S_I */ CMP
, -1, -1, +1, LEN
, LEN
, +1, LEN
, LEN
,
55 /* S_F */ CMP
, CMP
, CMP
, CMP
, CMP
, CMP
, CMP
, CMP
, CMP
,
56 /* S_Z */ CMP
, +1, +1, -1, CMP
, CMP
, -1, CMP
, CMP
64 /* Hint: '0' is a digit too. */
65 state
= S_N
+ ((c1
== '0') + (isdigit (c1
) != 0));
67 while ((diff
= c1
- c2
) == 0) {
71 state
= next_state
[state
];
74 state
+= (c1
== '0') + (isdigit (c1
) != 0);
77 state
= result_type
[state
* 3 + (((c2
== '0') + (isdigit (c2
) != 0)))];
84 while (isdigit (*p1
++))
88 return isdigit (*p2
) ? -1 : diff
;