]> rtime.felk.cvut.cz Git - notmuch.git/commitdiff
util: add strcmp_null, a strcmp that handles NULL parameters
authorJani Nikula <jani@nikula.org>
Fri, 25 Sep 2015 16:48:19 +0000 (19:48 +0300)
committerDavid Bremner <david@tethera.net>
Sat, 26 Sep 2015 10:37:35 +0000 (07:37 -0300)
Add strcmp_null, a strcmp that handles NULL strings; in strcmp terms a
NULL string is considered to be less than a non-NULL string.

util/string-util.c
util/string-util.h

index 76c0b9025d0f535243ac7003d5316b22e7e53f17..92af937f45ec15eab8ae00d27b287bf6a265134a 100644 (file)
@@ -222,6 +222,19 @@ parse_boolean_term (void *ctx, const char *str,
     return -1;
 }
 
+int
+strcmp_null (const char *s1, const char *s2)
+{
+    if (s1 && s2)
+       return strcmp (s1, s2);
+    else if (! s1 && ! s2)
+       return 0;
+    else if (s1)
+       return 1;       /* s1 (non-NULL) is greater than s2 (NULL) */
+    else
+       return -1;      /* s1 (NULL) is less than s2 (non-NULL) */
+}
+
 int
 strcase_equal (const void *a, const void *b)
 {
index 80d24d1c1053035b81144ea51e3ac1934b3d4be7..87917b8fd279de6e07f2fb919429fb50512a85b4 100644 (file)
@@ -64,6 +64,11 @@ int
 parse_boolean_term (void *ctx, const char *str,
                    char **prefix_out, char **term_out);
 
+/* strcmp that handles NULL strings; in strcmp terms a NULL string is
+ * considered to be less than a non-NULL string.
+ */
+int strcmp_null (const char *s1, const char *s2);
+
 /* GLib GEqualFunc compatible strcasecmp wrapper */
 int strcase_equal (const void *a, const void *b);