imap-send.c: rearrange xcalloc arguments
[git/git-svn.git] / versioncmp.c
blob7511e08271fb60276008c0b364a45b9512f49cea
1 #include "cache.h"
3 /*
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
7 * to GPLv2.
8 */
11 * states: S_N: normal, S_I: comparing integral part, S_F: comparing
12 * fractionnal parts, S_Z: idem but with leading Zeroes only
14 #define S_N 0x0
15 #define S_I 0x3
16 #define S_F 0x6
17 #define S_Z 0x9
19 /* result_type: CMP: return diff; LEN: compare using len_diff/diff */
20 #define CMP 2
21 #define LEN 3
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
28 * doc).
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;
35 unsigned char c1, c2;
36 int state, diff;
39 * Symbol(s) 0 [1-9] others
40 * Transition (10) 0 (01) d (00) x
42 static const uint8_t next_state[] = {
43 /* state x d 0 */
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
59 if (p1 == p2)
60 return 0;
62 c1 = *p1++;
63 c2 = *p2++;
64 /* Hint: '0' is a digit too. */
65 state = S_N + ((c1 == '0') + (isdigit (c1) != 0));
67 while ((diff = c1 - c2) == 0) {
68 if (c1 == '\0')
69 return diff;
71 state = next_state[state];
72 c1 = *p1++;
73 c2 = *p2++;
74 state += (c1 == '0') + (isdigit (c1) != 0);
77 state = result_type[state * 3 + (((c2 == '0') + (isdigit (c2) != 0)))];
79 switch (state) {
80 case CMP:
81 return diff;
83 case LEN:
84 while (isdigit (*p1++))
85 if (!isdigit (*p2++))
86 return 1;
88 return isdigit (*p2) ? -1 : diff;
90 default:
91 return state;