2 #include "string-list.h"
5 * versioncmp(): copied from string/strverscmp.c in glibc commit
6 * ee9247c38a8def24a59eb5cfb7196a98bef8cfdc, reformatted to Git coding
7 * style. The implementation is under LGPL-2.1 and Git relicenses it
12 * states: S_N: normal, S_I: comparing integral part, S_F: comparing
13 * fractionnal parts, S_Z: idem but with leading Zeroes only
20 /* result_type: CMP: return diff; LEN: compare using len_diff/diff */
24 static const struct string_list *prereleases;
25 static int initialized;
33 static void find_better_matching_suffix(const char *tagname, const char *suffix,
34 int suffix_len, int start, int conf_pos,
35 struct suffix_match *match)
38 * A better match either starts earlier or starts at the same offset
41 int end = match->len < suffix_len ? match->start : match->start-1;
43 for (i = start; i <= end; i++)
44 if (starts_with(tagname + i, suffix)) {
45 match->conf_pos = conf_pos;
47 match->len = suffix_len;
53 * off is the offset of the first different character in the two strings
54 * s1 and s2. If either s1 or s2 contains a prerelease suffix containing
55 * that offset or a suffix ends right before that offset, then that
56 * string will be forced to be on top.
58 * If both s1 and s2 contain a (different) suffix around that position,
59 * their order is determined by the order of those two suffixes in the
61 * If any of the strings contains more than one different suffixes around
62 * that position, then that string is sorted according to the contained
63 * suffix which starts at the earliest offset in that string.
64 * If more than one different contained suffixes start at that earliest
65 * offset, then that string is sorted according to the longest of those
68 * Return non-zero if *diff contains the return value for versioncmp()
70 static int swap_prereleases(const char *s1,
76 struct suffix_match match1 = { -1, off, -1 };
77 struct suffix_match match2 = { -1, off, -1 };
79 for (i = 0; i < prereleases->nr; i++) {
80 const char *suffix = prereleases->items[i].string;
81 int start, suffix_len = strlen(suffix);
83 start = off - suffix_len;
86 find_better_matching_suffix(s1, suffix, suffix_len, start,
88 find_better_matching_suffix(s2, suffix, suffix_len, start,
91 if (match1.conf_pos == -1 && match2.conf_pos == -1)
93 if (match1.conf_pos == match2.conf_pos)
94 /* Found the same suffix in both, e.g. "-rc" in "v1.0-rcX"
95 * and "v1.0-rcY": the caller should decide based on "X"
99 if (match1.conf_pos >= 0 && match2.conf_pos >= 0)
100 *diff = match1.conf_pos - match2.conf_pos;
101 else if (match1.conf_pos >= 0)
103 else /* if (match2.conf_pos >= 0) */
109 * Compare S1 and S2 as strings holding indices/version numbers,
110 * returning less than, equal to or greater than zero if S1 is less
111 * than, equal to or greater than S2 (for more info, see the texinfo
115 int versioncmp(const char *s1, const char *s2)
117 const unsigned char *p1 = (const unsigned char *) s1;
118 const unsigned char *p2 = (const unsigned char *) s2;
119 unsigned char c1, c2;
123 * Symbol(s) 0 [1-9] others
124 * Transition (10) 0 (01) d (00) x
126 static const uint8_t next_state[] = {
128 /* S_N */ S_N, S_I, S_Z,
129 /* S_I */ S_N, S_I, S_I,
130 /* S_F */ S_N, S_F, S_F,
131 /* S_Z */ S_N, S_F, S_Z
134 static const int8_t result_type[] = {
135 /* state x/x x/d x/0 d/x d/d d/0 0/x 0/d 0/0 */
137 /* S_N */ CMP, CMP, CMP, CMP, LEN, CMP, CMP, CMP, CMP,
138 /* S_I */ CMP, -1, -1, +1, LEN, LEN, +1, LEN, LEN,
139 /* S_F */ CMP, CMP, CMP, CMP, CMP, CMP, CMP, CMP, CMP,
140 /* S_Z */ CMP, +1, +1, -1, CMP, CMP, -1, CMP, CMP
148 /* Hint: '0' is a digit too. */
149 state = S_N + ((c1 == '0') + (isdigit (c1) != 0));
151 while ((diff = c1 - c2) == 0) {
155 state = next_state[state];
158 state += (c1 == '0') + (isdigit (c1) != 0);
162 const struct string_list *deprecated_prereleases;
164 prereleases = git_config_get_value_multi("versionsort.suffix");
165 deprecated_prereleases = git_config_get_value_multi("versionsort.prereleasesuffix");
167 if (deprecated_prereleases)
168 warning("ignoring versionsort.prereleasesuffix because versionsort.suffix is set");
170 prereleases = deprecated_prereleases;
172 if (prereleases && swap_prereleases(s1, s2, (const char *) p1 - s1 - 1,
176 state = result_type[state * 3 + (((c2 == '0') + (isdigit (c2) != 0)))];
183 while (isdigit (*p1++))
184 if (!isdigit (*p2++))
187 return isdigit (*p2) ? -1 : diff;