stringi-set: New functions for not necessarily null terminated strings.
[pspp] / src / libpspp / string-array.c
index b9067ab60d431f260dad52a04ca05aaf0822b7d7..d4badf4638df124681e964f78286a9cb3fda02bc 100644 (file)
@@ -234,6 +234,36 @@ string_array_sort (struct string_array *sa)
   qsort (sa->strings, sa->n, sizeof *sa->strings, compare_strings);
 }
 
+/* Removes all but one of any series of adjacent duplicate strings in SA. */
+void
+string_array_uniq (struct string_array *sa)
+{
+  if (!sa->n)
+    return;
+
+  size_t n = 1;
+  for (size_t i = 1; i < sa->n; i++)
+    {
+      char *s = sa->strings[i];
+      if (strcmp (sa->strings[n - 1], s))
+        sa->strings[n++] = s;
+      else
+        free (s);
+    }
+  sa->n = n;
+}
+
+/* Divides STRING into tokens at DELIMITERS and adds each token to SA. */
+void
+string_array_parse (struct string_array *sa, struct substring string,
+                    struct substring delimiters)
+{
+  size_t save_idx = 0;
+  struct substring token;
+  while (ss_tokenize (string, delimiters, &save_idx, &token))
+    string_array_append_nocopy (sa, ss_xstrdup (token));
+}
+
 /* Returns a single string that consists of each of the strings in SA
    concatenated, separated from each other with SEPARATOR.