3 #include "string-list.h"
6 * versioncmp(): copied from string/strverscmp.c in glibc commit
7 * ee9247c38a8def24a59eb5cfb7196a98bef8cfdc, reformatted to Git coding
8 * style. The implementation is under LGPL-2.1 and Git relicenses it
13 * states: S_N: normal, S_I: comparing integral part, S_F: comparing
14 * fractionnal parts, S_Z: idem but with leading Zeroes only
21 /* result_type: CMP: return diff; LEN: compare using len_diff/diff */
25 static const struct string_list *prereleases;
26 static int initialized;
34 static void find_better_matching_suffix(const char *tagname, const char *suffix,
35 int suffix_len, int start, int conf_pos,
36 struct suffix_match *match)
39 * A better match either starts earlier or starts at the same offset
42 int end = match->len < suffix_len ? match->start : match->start-1;
44 for (i = start; i <= end; i++)
45 if (starts_with(tagname + i, suffix)) {
46 match->conf_pos = conf_pos;
48 match->len = suffix_len;
54 * off is the offset of the first different character in the two strings
55 * s1 and s2. If either s1 or s2 contains a prerelease suffix containing
56 * that offset or a suffix ends right before that offset, then that
57 * string will be forced to be on top.
59 * If both s1 and s2 contain a (different) suffix around that position,
60 * their order is determined by the order of those two suffixes in the
62 * If any of the strings contains more than one different suffixes around
63 * that position, then that string is sorted according to the contained
64 * suffix which starts at the earliest offset in that string.
65 * If more than one different contained suffixes start at that earliest
66 * offset, then that string is sorted according to the longest of those
69 * Return non-zero if *diff contains the return value for versioncmp()
71 static int swap_prereleases(const char *s1,
77 struct suffix_match match1 = { -1, off, -1 };
78 struct suffix_match match2 = { -1, off, -1 };
80 for (i = 0; i < prereleases->nr; i++) {
81 const char *suffix = prereleases->items[i].string;
82 int start, suffix_len = strlen(suffix);
84 start = off - suffix_len;
87 find_better_matching_suffix(s1, suffix, suffix_len, start,
89 find_better_matching_suffix(s2, suffix, suffix_len, start,
92 if (match1.conf_pos == -1 && match2.conf_pos == -1)
94 if (match1.conf_pos == match2.conf_pos)
95 /* Found the same suffix in both, e.g. "-rc" in "v1.0-rcX"
96 * and "v1.0-rcY": the caller should decide based on "X"
100 if (match1.conf_pos >= 0 && match2.conf_pos >= 0)
101 *diff = match1.conf_pos - match2.conf_pos;
102 else if (match1.conf_pos >= 0)
104 else /* if (match2.conf_pos >= 0) */
110 * Compare S1 and S2 as strings holding indices/version numbers,
111 * returning less than, equal to or greater than zero if S1 is less
112 * than, equal to or greater than S2 (for more info, see the texinfo
116 int versioncmp(const char *s1, const char *s2)
118 const unsigned char *p1 = (const unsigned char *) s1;
119 const unsigned char *p2 = (const unsigned char *) s2;
120 unsigned char c1, c2;
124 * Symbol(s) 0 [1-9] others
125 * Transition (10) 0 (01) d (00) x
127 static const uint8_t next_state[] = {
129 /* S_N */ S_N, S_I, S_Z,
130 /* S_I */ S_N, S_I, S_I,
131 /* S_F */ S_N, S_F, S_F,
132 /* S_Z */ S_N, S_F, S_Z
135 static const int8_t result_type[] = {
136 /* state x/x x/d x/0 d/x d/d d/0 0/x 0/d 0/0 */
138 /* S_N */ CMP, CMP, CMP, CMP, LEN, CMP, CMP, CMP, CMP,
139 /* S_I */ CMP, -1, -1, +1, LEN, LEN, +1, LEN, LEN,
140 /* S_F */ CMP, CMP, CMP, CMP, CMP, CMP, CMP, CMP, CMP,
141 /* S_Z */ CMP, +1, +1, -1, CMP, CMP, -1, CMP, CMP
149 /* Hint: '0' is a digit too. */
150 state = S_N + ((c1 == '0') + (isdigit (c1) != 0));
152 while ((diff = c1 - c2) == 0) {
156 state = next_state[state];
159 state += (c1 == '0') + (isdigit (c1) != 0);
163 const struct string_list *deprecated_prereleases;
165 prereleases = git_config_get_value_multi("versionsort.suffix");
166 deprecated_prereleases = git_config_get_value_multi("versionsort.prereleasesuffix");
168 if (deprecated_prereleases)
169 warning("ignoring versionsort.prereleasesuffix because versionsort.suffix is set");
171 prereleases = deprecated_prereleases;
173 if (prereleases && swap_prereleases(s1, s2, (const char *) p1 - s1 - 1,
177 state = result_type[state * 3 + (((c2 == '0') + (isdigit (c2) != 0)))];
184 while (isdigit (*p1++))
185 if (!isdigit (*p2++))
188 return isdigit (*p2) ? -1 : diff;