]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
tree-wide: replace strverscmp() and str_verscmp() with strverscmp_improved() 18416/head
authorYu Watanabe <watanabe.yu+github@gmail.com>
Sat, 30 Jan 2021 16:12:27 +0000 (01:12 +0900)
committerYu Watanabe <watanabe.yu+github@gmail.com>
Tue, 9 Feb 2021 05:25:03 +0000 (14:25 +0900)
src/basic/cgroup-util.c
src/basic/util.c
src/basic/util.h
src/boot/bootctl.c
src/boot/efi/boot.c
src/shared/bootspec.c
src/shared/condition.c
src/sysext/sysext.c

index b567822b7ef4ff0341d96885a1c90b2dd691f3fc..bb20a12294d5406ed963c291013d1185fa2e7485 100644 (file)
@@ -2164,7 +2164,7 @@ CGroupMask get_cpu_accounting_mask(void) {
                         struct utsname u;
                         assert_se(uname(&u) >= 0);
 
-                        if (str_verscmp(u.release, "4.15") < 0)
+                        if (strverscmp_improved(u.release, "4.15") < 0)
                                 needed_mask = CGROUP_MASK_CPU;
                         else
                                 needed_mask = 0;
index de04a01a759fdce78a2258f937047a4e17ee16dd..955b18bd2aaf5bf8b98f042d53fcc22b1354f1e3 100644 (file)
@@ -227,68 +227,6 @@ int version(void) {
         return 0;
 }
 
-/* This is a direct translation of str_verscmp from boot.c */
-static bool is_digit(int c) {
-        return c >= '0' && c <= '9';
-}
-
-static int c_order(int c) {
-        if (c == 0 || is_digit(c))
-                return 0;
-
-        if ((c >= 'a') && (c <= 'z'))
-                return c;
-
-        return c + 0x10000;
-}
-
-int str_verscmp(const char *s1, const char *s2) {
-        const char *os1, *os2;
-
-        assert(s1);
-        assert(s2);
-
-        os1 = s1;
-        os2 = s2;
-
-        while (*s1 || *s2) {
-                int first;
-
-                while ((*s1 && !is_digit(*s1)) || (*s2 && !is_digit(*s2))) {
-                        int order;
-
-                        order = c_order(*s1) - c_order(*s2);
-                        if (order != 0)
-                                return order;
-                        s1++;
-                        s2++;
-                }
-
-                while (*s1 == '0')
-                        s1++;
-                while (*s2 == '0')
-                        s2++;
-
-                first = 0;
-                while (is_digit(*s1) && is_digit(*s2)) {
-                        if (first == 0)
-                                first = *s1 - *s2;
-                        s1++;
-                        s2++;
-                }
-
-                if (is_digit(*s1))
-                        return 1;
-                if (is_digit(*s2))
-                        return -1;
-
-                if (first != 0)
-                        return first;
-        }
-
-        return strcmp(os1, os2);
-}
-
 /* Turn off core dumps but only if we're running outside of a container. */
 void disable_coredumps(void) {
         int r;
index 942d773ff11f1e52d5f1716f02428deeb2888e76..b6c51c036eb89c85e6aaffff541635beb0dba2c7 100644 (file)
@@ -63,6 +63,4 @@ int container_get_leader(const char *machine, pid_t *pid);
 
 int version(void);
 
-int str_verscmp(const char *s1, const char *s2);
-
 void disable_coredumps(void);
index 8d2be21dc53e4e81f64751321a9992e7338b6703..7b759bcd0257a6593ce10b3bdad2e85f59b5bf45 100644 (file)
@@ -471,7 +471,7 @@ static int compare_version(const char *a, const char *b) {
         b += strcspn(b, " ");
         b += strspn(b, " ");
 
-        return strverscmp(a, b);
+        return strverscmp_improved(a, b);
 }
 
 static int version_check(int fd_from, const char *from, int fd_to, const char *to) {
index 5f2e8f4b7ca12343ea4f738885dc4735419863b1..e0df0dcc4891ef00157670b63a42b34210e49aed 100644 (file)
@@ -914,63 +914,6 @@ static VOID config_entry_free(ConfigEntry *entry) {
         FreePool(entry);
 }
 
-static BOOLEAN is_digit(CHAR16 c) {
-        return (c >= '0') && (c <= '9');
-}
-
-static UINTN c_order(CHAR16 c) {
-        if (c == '\0')
-                return 0;
-        if (is_digit(c))
-                return 0;
-        else if ((c >= 'a') && (c <= 'z'))
-                return c;
-        else
-                return c + 0x10000;
-}
-
-static INTN str_verscmp(CHAR16 *s1, CHAR16 *s2) {
-        CHAR16 *os1 = s1;
-        CHAR16 *os2 = s2;
-
-        while (*s1 || *s2) {
-                INTN first;
-
-                while ((*s1 && !is_digit(*s1)) || (*s2 && !is_digit(*s2))) {
-                        INTN order;
-
-                        order = c_order(*s1) - c_order(*s2);
-                        if (order != 0)
-                                return order;
-                        s1++;
-                        s2++;
-                }
-
-                while (*s1 == '0')
-                        s1++;
-                while (*s2 == '0')
-                        s2++;
-
-                first = 0;
-                while (is_digit(*s1) && is_digit(*s2)) {
-                        if (first == 0)
-                                first = *s1 - *s2;
-                        s1++;
-                        s2++;
-                }
-
-                if (is_digit(*s1))
-                        return 1;
-                if (is_digit(*s2))
-                        return -1;
-
-                if (first != 0)
-                        return first;
-        }
-
-        return StrCmp(os1, os2);
-}
-
 static CHAR8 *line_get_key_value(
                 CHAR8 *content,
                 CHAR8 *sep,
@@ -1535,7 +1478,7 @@ static INTN config_entry_compare(ConfigEntry *a, ConfigEntry *b) {
         if (a->tries_left == 0 && b->tries_left != 0)
                 return -1;
 
-        r = str_verscmp(a->id, b->id);
+        r = strverscmp_improved(a->id, b->id);
         if (r != 0)
                 return r;
 
index e50408ab53bb308113b47ae45e0ffe4748c1a6ac..98b380476b41939f5168fea1297f60f555effa23 100644 (file)
@@ -244,7 +244,7 @@ static int boot_loader_read_conf(const char *path, BootConfig *config) {
 }
 
 static int boot_entry_compare(const BootEntry *a, const BootEntry *b) {
-        return str_verscmp(a->id, b->id);
+        return strverscmp_improved(a->id, b->id);
 }
 
 static int boot_entries_find(
index 41d3a16391f67ba2361f79acc1a48a62f3cc32bc..616e77994d85ba85d60459710d7830fd252c2c80 100644 (file)
@@ -247,7 +247,7 @@ static int condition_test_kernel_version(Condition *c, char **env) {
                                         return log_debug_errno(SYNTHETIC_ERRNO(EINVAL), "Unexpected end of expression: %s", p);
                         }
 
-                        r = test_order(str_verscmp(u.release, s), order);
+                        r = test_order(strverscmp_improved(u.release, s), order);
                 } else
                         /* No prefix? Then treat as glob string */
                         r = fnmatch(s, u.release, 0) == 0;
index a17f4e2c02ba9b71c757fe0633527ffa02444ea7..1ce9939afb03405e2ce1bd666db67d32a7d5e323 100644 (file)
@@ -395,9 +395,9 @@ static int merge_hierarchy(
         return 1;
 }
 
-static int strverscmpp(char *const* a, char *const* b) {
-        /* usable in qsort() for sorting a string array with strverscmp() */
-        return strverscmp(*a, *b);
+static int strverscmp_improvedp(char *const* a, char *const* b) {
+        /* usable in qsort() for sorting a string array with strverscmp_improved() */
+        return strverscmp_improved(*a, *b);
 }
 
 static int validate_version(
@@ -623,8 +623,8 @@ static int merge_subprocess(Hashmap *images, const char *workspace) {
                 return 0;
         }
 
-        /* Order by version sort (i.e. libc strverscmp()) */
-        typesafe_qsort(extensions, n_extensions, strverscmpp);
+        /* Order by version sort with strverscmp_improved() */
+        typesafe_qsort(extensions, n_extensions, strverscmp_improvedp);
 
         buf = strv_join(extensions, "', '");
         if (!buf)