[gnome-builder] libide/core: add string set helpers



commit df4b3560826ff6986cd8b4cc24cb6e4223baae44
Author: Christian Hergert <chergert redhat com>
Date:   Sat Aug 27 20:00:13 2022 -0700

    libide/core: add string set helpers
    
    This is a string set add/remove implementation which tries to preserve
    the order of the strings when removing.

 src/libide/core/ide-macros.h | 52 ++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 52 insertions(+)
---
diff --git a/src/libide/core/ide-macros.h b/src/libide/core/ide-macros.h
index e51be8824..0b8c80975 100644
--- a/src/libide/core/ide-macros.h
+++ b/src/libide/core/ide-macros.h
@@ -301,6 +301,58 @@ ide_strv_sort (char   **strv,
                      NULL);
 }
 
+static inline gboolean
+ide_strv_add_to_set (char ***strv,
+                     char   *value)
+{
+  gsize len;
+
+  if (value == NULL)
+    return FALSE;
+
+  if (*strv != NULL && g_strv_contains ((const char * const *)*strv, value))
+    {
+      g_free (value);
+      return FALSE;
+    }
+
+  len = *strv ? g_strv_length (*strv) : 0;
+  *strv = g_renew (char *, *strv, len + 2);
+  (*strv)[len++] = value;
+  (*strv)[len] = NULL;
+
+  return TRUE;
+}
+
+static inline gboolean
+ide_strv_remove_from_set (char       **strv,
+                          const char  *value)
+{
+  if (value == NULL)
+    return FALSE;
+
+  if (strv == NULL)
+    return FALSE;
+
+  for (guint i = 0; strv[i]; i++)
+    {
+      if (strcmp (value, strv[i]) == 0)
+        {
+          g_free (strv[i]);
+
+          while (TRUE)
+            {
+              strv[i] = strv[i+1];
+              if (strv[i] == NULL)
+                return TRUE;
+              i++;
+            }
+        }
+    }
+
+  return FALSE;
+}
+
 static inline int
 ide_steal_fd (int *fd)
 {


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]