From f967d0e36a2193c1249799f463ea9109b753f7a8 Mon Sep 17 00:00:00 2001 From: Ben Pfaff Date: Sun, 8 Dec 2019 02:13:37 +0000 Subject: [PATCH] string-array: New function string_array_parse(). This will acquire its first user in an upcoming commit. --- src/libpspp/string-array.c | 11 +++++++++++ src/libpspp/string-array.h | 3 +++ 2 files changed, 14 insertions(+) diff --git a/src/libpspp/string-array.c b/src/libpspp/string-array.c index b9067ab60d..a22ade6e10 100644 --- a/src/libpspp/string-array.c +++ b/src/libpspp/string-array.c @@ -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. diff --git a/src/libpspp/string-array.h b/src/libpspp/string-array.h index 36ad738396..ecad3326b6 100644 --- a/src/libpspp/string-array.h +++ b/src/libpspp/string-array.h @@ -19,6 +19,7 @@ #include #include +#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 -- 2.30.2