| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
#include "macro.h" |
| 4 |
#include "string-util.h" |
| 5 |
|
| 6 |
sd_char *startswith_internal(const sd_char *s, const sd_char *prefix) { |
| 7 |
size_t l; |
| 8 |
|
| 9 |
assert(s); |
| 10 |
assert(prefix); |
| 11 |
|
| 12 |
l = strlen(prefix); |
| 13 |
if (!strneq(s, prefix, l)) |
| 14 |
return NULL; |
| 15 |
|
| 16 |
return (sd_char*) s + l; |
| 17 |
} |
| 18 |
|
| 19 |
sd_char *startswith_no_case_internal(const sd_char *s, const sd_char *prefix) { |
| 20 |
size_t l; |
| 21 |
|
| 22 |
assert(s); |
| 23 |
assert(prefix); |
| 24 |
|
| 25 |
l = strlen(prefix); |
| 26 |
if (!strncaseeq(s, prefix, l)) |
| 27 |
return NULL; |
| 28 |
|
| 29 |
return (sd_char*) s + l; |
| 30 |
} |
| 31 |
|
| 32 |
sd_char* endswith_internal(const sd_char *s, const sd_char *suffix) { |
| 33 |
size_t sl, pl; |
| 34 |
|
| 35 |
assert(s); |
| 36 |
assert(suffix); |
| 37 |
|
| 38 |
sl = strlen(s); |
| 39 |
pl = strlen(suffix); |
| 40 |
|
| 41 |
if (pl == 0) |
| 42 |
return (sd_char*) s + sl; |
| 43 |
|
| 44 |
if (sl < pl) |
| 45 |
return NULL; |
| 46 |
|
| 47 |
if (!streq(s + sl - pl, suffix)) |
| 48 |
return NULL; |
| 49 |
|
| 50 |
return (sd_char*) s + sl - pl; |
| 51 |
} |
| 52 |
|
| 53 |
sd_char* endswith_no_case_internal(const sd_char *s, const sd_char *suffix) { |
| 54 |
size_t sl, pl; |
| 55 |
|
| 56 |
assert(s); |
| 57 |
assert(suffix); |
| 58 |
|
| 59 |
sl = strlen(s); |
| 60 |
pl = strlen(suffix); |
| 61 |
|
| 62 |
if (pl == 0) |
| 63 |
return (sd_char*) s + sl; |
| 64 |
|
| 65 |
if (sl < pl) |
| 66 |
return NULL; |
| 67 |
|
| 68 |
if (!strcaseeq(s + sl - pl, suffix)) |
| 69 |
return NULL; |
| 70 |
|
| 71 |
return (sd_char*) s + sl - pl; |
| 72 |
} |
| 73 |
|
| 74 |
static bool is_valid_version_char(sd_char a) { |
| 75 |
return ascii_isdigit(a) || ascii_isalpha(a) || IN_SET(a, '~', '-', '^', '.'); |
| 76 |
} |
| 77 |
|
| 78 |
int strverscmp_improved(const sd_char *a, const sd_char *b) { |
| 79 |
/* This function is similar to strverscmp(3), but it treats '-' and '.' as separators. |
| 80 |
* |
| 81 |
* The logic is based on rpm's rpmvercmp(), but unlike rpmvercmp(), it distinguishes e.g. |
| 82 |
* '123a' and '123.a', with '123a' being newer. |
| 83 |
* |
| 84 |
* It allows direct comparison of strings which contain both a version and a release; e.g. |
| 85 |
* '247.2-3.1.fc33.x86_64' or '5.11.0-0.rc5.20210128git76c057c84d28.137.fc34'. |
| 86 |
* |
| 87 |
* The input string is split into segments. Each segment is numeric or alphabetic, and may be |
| 88 |
* prefixed with the following: |
| 89 |
* '~' : used for pre-releases, a segment prefixed with this is the oldest, |
| 90 |
* '-' : used for the separator between version and release, |
| 91 |
* '^' : used for patched releases, a segment with this is newer than one with '-'. |
| 92 |
* '.' : used for point releases. |
| 93 |
* Note that no prefix segment is the newest. All non-supported characters are dropped, and |
| 94 |
* handled as a separator of segments, e.g., '123_a' is equivalent to '123a'. |
| 95 |
* |
| 96 |
* By using this, version strings can be sorted like following: |
| 97 |
* (older) 122.1 |
| 98 |
* ^ 123~rc1-1 |
| 99 |
* | 123 |
| 100 |
* | 123-a |
| 101 |
* | 123-a.1 |
| 102 |
* | 123-1 |
| 103 |
* | 123-1.1 |
| 104 |
* | 123^post1 |
| 105 |
* | 123.a-1 |
| 106 |
* | 123.1-1 |
| 107 |
* v 123a-1 |
| 108 |
* (newer) 124-1 |
| 109 |
*/ |
| 110 |
|
| 111 |
a = strempty(a); |
| 112 |
b = strempty(b); |
| 113 |
|
| 114 |
for (;;) { |
| 115 |
const sd_char *aa, *bb; |
| 116 |
int r; |
| 117 |
|
| 118 |
/* Drop leading invalid characters. */ |
| 119 |
while (*a != '\0' && !is_valid_version_char(*a)) |
| 120 |
a++; |
| 121 |
while (*b != '\0' && !is_valid_version_char(*b)) |
| 122 |
b++; |
| 123 |
|
| 124 |
/* Handle '~'. Used for pre-releases, e.g. 123~rc1, or 4.5~alpha1 */ |
| 125 |
if (*a == '~' || *b == '~') { |
| 126 |
/* The string prefixed with '~' is older. */ |
| 127 |
r = CMP(*a != '~', *b != '~'); |
| 128 |
if (r != 0) |
| 129 |
return r; |
| 130 |
|
| 131 |
/* Now both strings are prefixed with '~'. Compare remaining strings. */ |
| 132 |
a++; |
| 133 |
b++; |
| 134 |
} |
| 135 |
|
| 136 |
/* If at least one string reaches the end, then longer is newer. |
| 137 |
* Note that except for '~' prefixed segments, a string which has more segments is newer. |
| 138 |
* So, this check must be after the '~' check. */ |
| 139 |
if (*a == '\0' || *b == '\0') |
| 140 |
return CMP(*a, *b); |
| 141 |
|
| 142 |
/* Handle '-', which separates version and release, e.g 123.4-3.1.fc33.x86_64 */ |
| 143 |
if (*a == '-' || *b == '-') { |
| 144 |
/* The string prefixed with '-' is older (e.g., 123-9 vs 123.1-1) */ |
| 145 |
r = CMP(*a != '-', *b != '-'); |
| 146 |
if (r != 0) |
| 147 |
return r; |
| 148 |
|
| 149 |
a++; |
| 150 |
b++; |
| 151 |
} |
| 152 |
|
| 153 |
/* Handle '^'. Used for patched release. */ |
| 154 |
if (*a == '^' || *b == '^') { |
| 155 |
r = CMP(*a != '^', *b != '^'); |
| 156 |
if (r != 0) |
| 157 |
return r; |
| 158 |
|
| 159 |
a++; |
| 160 |
b++; |
| 161 |
} |
| 162 |
|
| 163 |
/* Handle '.'. Used for point releases. */ |
| 164 |
if (*a == '.' || *b == '.') { |
| 165 |
r = CMP(*a != '.', *b != '.'); |
| 166 |
if (r != 0) |
| 167 |
return r; |
| 168 |
|
| 169 |
a++; |
| 170 |
b++; |
| 171 |
} |
| 172 |
|
| 173 |
if (ascii_isdigit(*a) || ascii_isdigit(*b)) { |
| 174 |
/* Find the leading numeric segments. One may be an empty string. So, |
| 175 |
* numeric segments are always newer than alpha segments. */ |
| 176 |
for (aa = a; ascii_isdigit(*aa); aa++) |
| 177 |
; |
| 178 |
for (bb = b; ascii_isdigit(*bb); bb++) |
| 179 |
; |
| 180 |
|
| 181 |
/* Check if one of the strings was empty, but the other not. */ |
| 182 |
r = CMP(a != aa, b != bb); |
| 183 |
if (r != 0) |
| 184 |
return r; |
| 185 |
|
| 186 |
/* Skip leading '0', to make 00123 equivalent to 123. */ |
| 187 |
while (*a == '0') |
| 188 |
a++; |
| 189 |
while (*b == '0') |
| 190 |
b++; |
| 191 |
|
| 192 |
/* To compare numeric segments without parsing their values, first compare the |
| 193 |
* lengths of the segments. Eg. 12345 vs 123, longer is newer. */ |
| 194 |
r = CMP(aa - a, bb - b); |
| 195 |
if (r != 0) |
| 196 |
return r; |
| 197 |
|
| 198 |
/* Then, compare them as strings. */ |
| 199 |
r = CMP(strncmp(a, b, aa - a), 0); |
| 200 |
if (r != 0) |
| 201 |
return r; |
| 202 |
} else { |
| 203 |
/* Find the leading non-numeric segments. */ |
| 204 |
for (aa = a; ascii_isalpha(*aa); aa++) |
| 205 |
; |
| 206 |
for (bb = b; ascii_isalpha(*bb); bb++) |
| 207 |
; |
| 208 |
|
| 209 |
/* Note that the segments are usually not NUL-terminated. */ |
| 210 |
r = CMP(strncmp(a, b, MIN(aa - a, bb - b)), 0); |
| 211 |
if (r != 0) |
| 212 |
return r; |
| 213 |
|
| 214 |
/* Longer is newer, e.g. abc vs abcde. */ |
| 215 |
r = CMP(aa - a, bb - b); |
| 216 |
if (r != 0) |
| 217 |
return r; |
| 218 |
} |
| 219 |
|
| 220 |
/* The current segments are equivalent. Let's move to the next one. */ |
| 221 |
a = aa; |
| 222 |
b = bb; |
| 223 |
} |
| 224 |
} |