versioncmp: factor out helper for suffix matching

As the number of identical steps to be done for both tagnames grows, extract them into a helper function, with the additional benefit that the conditionals near the end of swap_prereleases() will use more meaningful variable names. Signed-off-by: SZEDER Gábor <szeder.dev@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

SZEDER Gábor committed Dec 8, 2016 at 15:48 UTC b17846432da4f8530c7349561eac4a16f95bbd5b
1 file changed +39 -25
versioncmp.c
+39 -25
@@ -24,6 +24,31 @@
24 static const struct string_list *prereleases;
25 static int initialized;
26
27 +struct suffix_match {
28 + int conf_pos;
29 + int start;
30 + int len;
31 +};
32 +
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)
36 +{
37 + /*
38 + * A better match either starts earlier or starts at the same offset
39 + * but is longer.
40 + */
41 + int end = match->len < suffix_len ? match->start : match->start-1;
42 + int i;
43 + for (i = start; i <= end; i++)
44 + if (starts_with(tagname + i, suffix)) {
45 + match->conf_pos = conf_pos;
46 + match->start = i;
47 + match->len = suffix_len;
48 + break;
49 + }
50 +}
51 +
52 /*
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
@@ -47,46 +72,35 @@ static int swap_prereleases(const char *s1,
72 int off,
73 int *diff)
74 {
50 - int i, i1 = -1, i2 = -1;
51 - int start_at1 = off, start_at2 = off, match_len1 = -1, match_len2 = -1;
75 + int i;
76 + struct suffix_match match1 = { -1, off, -1 };
77 + struct suffix_match match2 = { -1, off, -1 };
78
79 for (i = 0; i < prereleases->nr; i++) {
80 const char *suffix = prereleases->items[i].string;
55 - int j, start, end, suffix_len = strlen(suffix);
81 + int start, suffix_len = strlen(suffix);
82 if (suffix_len < off)
83 start = off - suffix_len;
84 else
85 start = 0;
60 - end = match_len1 < suffix_len ? start_at1 : start_at1-1;
61 - for (j = start; j <= end; j++)
62 - if (starts_with(s1 + j, suffix)) {
63 - i1 = i;
64 - start_at1 = j;
65 - match_len1 = suffix_len;
66 - break;
67 - }
68 - end = match_len2 < suffix_len ? start_at2 : start_at2-1;
69 - for (j = start; j <= end; j++)
70 - if (starts_with(s2 + j, suffix)) {
71 - i2 = i;
72 - start_at2 = j;
73 - match_len2 = suffix_len;
74 - break;
75 - }
86 + find_better_matching_suffix(s1, suffix, suffix_len, start,
87 + i, &match1);
88 + find_better_matching_suffix(s2, suffix, suffix_len, start,
89 + i, &match2);
90 }
77 - if (i1 == -1 && i2 == -1)
91 + if (match1.conf_pos == -1 && match2.conf_pos == -1)
92 return 0;
79 - if (i1 == i2)
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"
96 * and "Y". */
97 return 0;
98
85 - if (i1 >= 0 && i2 >= 0)
86 - *diff = i1 - i2;
87 - else if (i1 >= 0)
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)
102 *diff = -1;
89 - else /* if (i2 >= 0) */
103 + else /* if (match2.conf_pos >= 0) */
104 *diff = 1;
105 return 1;
106 }