about summary refs log tree commit diff
path: root/src/libnm-glib-aux/nm-ref-string.h
diff options
context:
space:
mode:
authorMichael Biebl <biebl@debian.org>2022-02-14 19:23:28 +0100
committerMichael Biebl <biebl@debian.org>2022-02-14 19:23:28 +0100
commit2f94dba7385fd0e0ef19a06eb4a2fcf6c43d7946 (patch)
treee2222f5577115985dd52044d2991253403cdd952 /src/libnm-glib-aux/nm-ref-string.h
parent88c227d90a6b7b388c5c85d72802a0ca8f05ed5c (diff)
New upstream version 1.35.91 upstream/1.35.91
Diffstat (limited to 'src/libnm-glib-aux/nm-ref-string.h')
-rw-r--r--src/libnm-glib-aux/nm-ref-string.h65
1 files changed, 60 insertions, 5 deletions
diff --git a/src/libnm-glib-aux/nm-ref-string.h b/src/libnm-glib-aux/nm-ref-string.h
index 6950d1b9..3363bce0 100644
--- a/src/libnm-glib-aux/nm-ref-string.h
+++ b/src/libnm-glib-aux/nm-ref-string.h
@@ -46,6 +46,19 @@ nm_ref_string_new(const char *cstr)
 
 /*****************************************************************************/
 
+NMRefString *nmtst_ref_string_find_len(const char *cstr, gsize len);
+
+static inline NMRefString *
+nmtst_ref_string_find(const char *cstr)
+{
+    /* WARNING: only use for testing. See nmtst_ref_string_find_len() why. */
+    if (!cstr)
+        return FALSE;
+    return nmtst_ref_string_find_len(cstr, strlen(cstr));
+}
+
+/*****************************************************************************/
+
 static inline NMRefString *
 nm_ref_string_ref(NMRefString *rstr)
 {
@@ -118,11 +131,14 @@ static inline gboolean
 nm_ref_string_equal_str(NMRefString *rstr, const char *str)
 {
     if (!str)
-        return (!!rstr);
+        return !rstr;
 
     if (!rstr)
         return FALSE;
 
+    /* We don't use streq() here, because an NMRefString might have embedded NUL characters
+     * (as the length is tracked separately). The NUL terminated C string @str must not
+     * compare equal to such a @rstr, thus we first explicitly check strlen(). */
     return rstr->len == strlen(str) && (rstr->str == str || memcmp(rstr->str, str, rstr->len) == 0);
 }
 
@@ -164,24 +180,63 @@ nm_ref_string_unref_upcast(const char *str)
     nm_ref_string_unref(NM_REF_STRING_UPCAST(str));
 }
 
+/**
+ * nm_ref_string_reset_str_upcast:
+ * @ptr: the destination pointer that gets updated.
+ * @str: the new string to be set.
+ *
+ * @ptr is a location (destination pointer) of an "upcast" NMRefString.
+ * That is, it holds either %NULL or some ((NMRefString *) rstr)->str.
+ * In other words, @ptr holds an NMRefString which you could get via
+ * NM_REF_STRING_UPCAST(*ptr).
+ * This function resets @ptr to point to a NMRefString equal to @str.
+ *
+ * Returns: %TRUE if the pointer changed and %FALSE if the value was
+ *   already set to a string equal to @str.
+ */
+static inline gboolean
+nm_ref_string_reset_str_upcast(const char **ptr, const char *str)
+{
+    NMRefString *rstr;
+    gsize        l;
+
+    nm_assert(ptr);
+
+    if (!str)
+        return nm_clear_pointer(ptr, nm_ref_string_unref_upcast);
+
+    rstr = NM_REF_STRING_UPCAST(*ptr);
+
+    l = strlen(str);
+
+    if (rstr && rstr->len == l && (rstr->str == str || memcmp(rstr->str, str, l) == 0))
+        return FALSE;
+
+    *ptr = nm_ref_string_new_len(str, l)->str;
+    nm_ref_string_unref(rstr);
+    return TRUE;
+}
+
 static inline gboolean
 nm_ref_string_reset_str(NMRefString **ptr, const char *str)
 {
-    nm_auto_ref_string NMRefString *rstr = NULL;
-    gsize                           l;
+    NMRefString *rstr;
+    gsize        l;
 
     nm_assert(ptr);
 
     if (!str)
         return nm_clear_pointer(ptr, nm_ref_string_unref);
 
+    rstr = *ptr;
+
     l = strlen(str);
 
-    if ((*ptr) && (*ptr)->len == l && ((*ptr)->str == str || memcmp((*ptr)->str, str, l) == 0))
+    if (rstr && rstr->len == l && (rstr->str == str || memcmp(rstr->str, str, l) == 0))
         return FALSE;
 
-    rstr = *ptr;
     *ptr = nm_ref_string_new_len(str, l);
+    nm_ref_string_unref(rstr);
     return TRUE;
 }