Skip to content

Commit b178464

Browse files
szedergitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 51acfa9 commit b178464

File tree

1 file changed

+39
-25
lines changed

1 file changed

+39
-25
lines changed

versioncmp.c

Lines changed: 39 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,31 @@
2424
static const struct string_list *prereleases;
2525
static int initialized;
2626

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+
2752
/*
2853
* off is the offset of the first different character in the two strings
2954
* s1 and s2. If either s1 or s2 contains a prerelease suffix containing
@@ -47,46 +72,35 @@ static int swap_prereleases(const char *s1,
4772
int off,
4873
int *diff)
4974
{
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 };
5278

5379
for (i = 0; i < prereleases->nr; i++) {
5480
const char *suffix = prereleases->items[i].string;
55-
int j, start, end, suffix_len = strlen(suffix);
81+
int start, suffix_len = strlen(suffix);
5682
if (suffix_len < off)
5783
start = off - suffix_len;
5884
else
5985
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);
7690
}
77-
if (i1 == -1 && i2 == -1)
91+
if (match1.conf_pos == -1 && match2.conf_pos == -1)
7892
return 0;
79-
if (i1 == i2)
93+
if (match1.conf_pos == match2.conf_pos)
8094
/* Found the same suffix in both, e.g. "-rc" in "v1.0-rcX"
8195
* and "v1.0-rcY": the caller should decide based on "X"
8296
* and "Y". */
8397
return 0;
8498

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)
88102
*diff = -1;
89-
else /* if (i2 >= 0) */
103+
else /* if (match2.conf_pos >= 0) */
90104
*diff = 1;
91105
return 1;
92106
}

0 commit comments

Comments
 (0)