]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
strv: introduce strv_compare()
authorYu Watanabe <watanabe.yu+github@gmail.com>
Tue, 14 Jan 2020 09:29:50 +0000 (18:29 +0900)
committerYu Watanabe <watanabe.yu+github@gmail.com>
Wed, 15 Jan 2020 02:52:40 +0000 (11:52 +0900)
src/basic/strv.c
src/basic/strv.h
src/test/test-strv.c

index 99bcfde089d7c70cdfda2fac53d6353218c2b1d1..5588cd7c413fbb7969ffa2d6c55f4810f6e344db 100644 (file)
@@ -730,19 +730,26 @@ char **strv_sort(char **l) {
         return l;
 }
 
-bool strv_equal(char * const *a, char * const *b) {
+int strv_compare(char * const *a, char * const *b) {
+        int r;
 
-        if (strv_isempty(a))
-                return strv_isempty(b);
+        if (strv_isempty(a)) {
+                if (strv_isempty(b))
+                        return 0;
+                else
+                        return -1;
+        }
 
         if (strv_isempty(b))
-                return false;
+                return 1;
 
-        for ( ; *a || *b; ++a, ++b)
-                if (!streq_ptr(*a, *b))
-                        return false;
+        for ( ; *a || *b; ++a, ++b) {
+                r = strcmp_ptr(*a, *b);
+                if (r != 0)
+                        return r;
+        }
 
-        return true;
+        return 0;
 }
 
 void strv_print(char * const *l) {
index f335aeee32fa74a3bf306fbff82fb631a023608a..f150456682dfa3aaee9daa24e8c02f9e741008ea 100644 (file)
@@ -51,7 +51,10 @@ char **strv_remove(char **l, const char *s);
 char **strv_uniq(char **l);
 bool strv_is_uniq(char * const *l);
 
-bool strv_equal(char * const *a, char * const *b);
+int strv_compare(char * const *a, char * const *b);
+static inline bool strv_equal(char * const *a, char * const *b) {
+        return strv_compare(a, b) == 0;
+}
 
 #define strv_contains(l, s) (!!strv_find((l), (s)))
 
index f31ea6f8c61cf6b86efc4c8757ac4509029cb538..55ea79d1fc553906991b174b39c2f11ef30dc0b6 100644 (file)
@@ -711,27 +711,33 @@ static void test_strv_push(void) {
         assert_se(streq_ptr(a[3], NULL));
 }
 
-static void test_strv_equal(void) {
+static void test_strv_compare(void) {
         _cleanup_strv_free_ char **a = NULL;
         _cleanup_strv_free_ char **b = NULL;
         _cleanup_strv_free_ char **c = NULL;
+        _cleanup_strv_free_ char **d = NULL;
 
         log_info("/* %s */", __func__);
 
         a = strv_new("one", "two", "three");
         assert_se(a);
         b = strv_new("one", "two", "three");
-        assert_se(a);
+        assert_se(b);
         c = strv_new("one", "two", "three", "four");
-        assert_se(a);
+        assert_se(c);
+        d = strv_new(NULL);
+        assert_se(d);
 
-        assert_se(strv_equal(a, a));
-        assert_se(strv_equal(a, b));
-        assert_se(strv_equal(NULL, NULL));
+        assert_se(strv_compare(a, a) == 0);
+        assert_se(strv_compare(a, b) == 0);
+        assert_se(strv_compare(d, d) == 0);
+        assert_se(strv_compare(d, NULL) == 0);
+        assert_se(strv_compare(NULL, NULL) == 0);
 
-        assert_se(!strv_equal(a, c));
-        assert_se(!strv_equal(b, c));
-        assert_se(!strv_equal(b, NULL));
+        assert_se(strv_compare(a, c) < 0);
+        assert_se(strv_compare(b, c) < 0);
+        assert_se(strv_compare(b, d) == 1);
+        assert_se(strv_compare(b, NULL) == 1);
 }
 
 static void test_strv_is_uniq(void) {
@@ -988,7 +994,7 @@ int main(int argc, char *argv[]) {
         test_strv_insert();
         test_strv_push_prepend();
         test_strv_push();
-        test_strv_equal();
+        test_strv_compare();
         test_strv_is_uniq();
         test_strv_reverse();
         test_strv_shell_escape();