1
0
mirror of https://github.com/systemd/systemd.git synced 2024-10-28 03:25:31 +03:00

string-util: trivial optimizations for strverscmp_improved()

This commit is contained in:
Yu Watanabe 2021-06-01 17:58:56 +09:00
parent 3fe398cec0
commit 122dbd92c9

View File

@ -152,7 +152,7 @@ sd_int strverscmp_improved(const sd_char *a, const sd_char *b) {
* Note that except for '~' prefixed segments, a string has more segments is newer.
* So, this check must be after the '~' check. */
if (*a == '\0' || *b == '\0')
return strcmp(a, b);
return CMP(*a, *b);
/* Handle '-', which separates version and release, e.g 123.4-3.1.fc33.x86_64 */
if (*a == '-' || *b == '-') {
@ -194,9 +194,9 @@ sd_int strverscmp_improved(const sd_char *a, const sd_char *b) {
/* Find the leading numeric segments. One may be an empty string. So,
* numeric segments are always newer than alpha segments. */
for (aa = a; *aa != '\0' && isdigit(*aa); aa++)
for (aa = a; isdigit(*aa); aa++)
;
for (bb = b; *bb != '\0' && isdigit(*bb); bb++)
for (bb = b; isdigit(*bb); bb++)
;
/* To compare numeric segments without parsing their values, first compare the
@ -211,9 +211,9 @@ sd_int strverscmp_improved(const sd_char *a, const sd_char *b) {
return r;
} else {
/* Find the leading non-numeric segments. */
for (aa = a; *aa != '\0' && is_alpha(*aa); aa++)
for (aa = a; is_alpha(*aa); aa++)
;
for (bb = b; *bb != '\0' && is_alpha(*bb); bb++)
for (bb = b; is_alpha(*bb); bb++)
;
/* Note that the segments are usually not NUL-terminated. */