24
24
static const struct string_list * prereleases ;
25
25
static int initialized ;
26
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
+
27
52
/*
28
53
* off is the offset of the first different character in the two strings
29
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,
47
72
int off ,
48
73
int * diff )
49
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 };
52
78
53
79
for (i = 0 ; i < prereleases -> nr ; i ++ ) {
54
80
const char * suffix = prereleases -> items [i ].string ;
55
- int j , start , end , suffix_len = strlen (suffix );
81
+ int start , suffix_len = strlen (suffix );
56
82
if (suffix_len < off )
57
83
start = off - suffix_len ;
58
84
else
59
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 );
76
90
}
77
- if (i1 == -1 && i2 == -1 )
91
+ if (match1 . conf_pos == -1 && match2 . conf_pos == -1 )
78
92
return 0 ;
79
- if (i1 == i2 )
93
+ if (match1 . conf_pos == match2 . conf_pos )
80
94
/* Found the same suffix in both, e.g. "-rc" in "v1.0-rcX"
81
95
* and "v1.0-rcY": the caller should decide based on "X"
82
96
* and "Y". */
83
97
return 0 ;
84
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 )
88
102
* diff = -1 ;
89
- else /* if (i2 >= 0) */
103
+ else /* if (match2.conf_pos >= 0) */
90
104
* diff = 1 ;
91
105
return 1 ;
92
106
}
0 commit comments