string-array: New function string_array_parse().
authorBen Pfaff <blp@cs.stanford.edu>
Sun, 8 Dec 2019 02:13:37 +0000 (02:13 +0000)
committerBen Pfaff <blp@cs.stanford.edu>
Sun, 29 Dec 2019 05:28:10 +0000 (05:28 +0000)
This will acquire its first user in an upcoming commit.

src/libpspp/string-array.c
src/libpspp/string-array.h

index b9067ab60d431f260dad52a04ca05aaf0822b7d7..a22ade6e10880e28fb47bbca5010ec63a3fa7737 100644 (file)
@@ -234,6 +234,17 @@ string_array_sort (struct string_array *sa)
   qsort (sa->strings, sa->n, sizeof *sa->strings, compare_strings);
 }
 
+/* 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.
 
index 36ad7383963d8ca7d23a80b8a820f4ecb8969de9..ecad3326b6a73f3a02ff99101d3462e5ce3936fb 100644 (file)
@@ -19,6 +19,7 @@
 
 #include <stdbool.h>
 #include <stddef.h>
+#include "libpspp/str.h"
 
 /* An unordered array of strings.
 
@@ -61,6 +62,8 @@ void string_array_shrink (struct string_array *);
 
 void string_array_sort (struct string_array *);
 
+void string_array_parse (struct string_array *, struct substring string,
+                         struct substring delimiters);
 char *string_array_join (const struct string_array *, const char *separator);
 
 /* Macros for conveniently iterating through a string_array, e.g. to print all