Skip to contentSkip to conversation

util: Add l_streq0

PATCH New Andrew Zaborowski 1 participant 1 message Jun 30, 2021

lore
Share & tools
lore
#0 lore raw

PATCH 3/3 util: Add l_streq0

Add a glib/nm-utils -style utility for comparing two strings that handles NULL values (unlike strcmp). Two NULL strings are treated as equal.

---
 ell/ell.sym |  1 +
 ell/util.c  | 13 +++++++++++++
 ell/util.h  |  1 +
 3 files changed, 15 insertions(+)
ell/ell.sym +1 -0
index add7fef..4391051 100644
@@ -12,6 +12,7 @@ global:
 	l_strlcpy;
 	l_str_has_prefix;
 	l_str_has_suffix;
+	l_streq0;
 	l_util_hexstring;
 	l_util_hexstring_upper;
 	l_util_from_hexstring;
ell/util.c +13 -0
index 531b49e..5fb1a43 100644
@@ -342,6 +342,19 @@ LIB_EXPORT bool l_str_has_suffix(const char *str, const char *suffix)
 	return !strcmp(&str[len_diff], suffix);
 }
 
+/**
+ * l_streq0:
+ * @a: First operand
+ * @b: Second operand
+ *
+ * Returns: True if @a and @b are both NULL or both non-NULL and identical
+ * according to strcmp.  False otherwise.
+ */
+LIB_EXPORT bool l_streq0(const char *a, const char *b)
+{
+	return a == b || (a && b && !strcmp(a, b));
+}
+
 static char *hexstring_common(const unsigned char *buf, size_t len,
 				const char hexdigits[static 16])
 {
ell/util.h +1 -0
index bdf6935..d56c2a5 100644
@@ -276,6 +276,7 @@ size_t l_strlcpy(char* dst, const char *src, size_t len);
 
 bool l_str_has_prefix(const char *str, const char *prefix);
 bool l_str_has_suffix(const char *str, const char *suffix);
+bool l_streq0(const char *a, const char *b);
 
 char *l_util_hexstring(const unsigned char *buf, size_t len);
 char *l_util_hexstring_upper(const unsigned char *buf, size_t len);
Signature · 1 line
2.30.2