X-Git-Url: https://pintos-os.org/cgi-bin/gitweb.cgi?a=blobdiff_plain;f=src%2Flibpspp%2Fstring-array.c;h=d162593b2cfb4102dd30a959cb1fc306276f0241;hb=c8479b88177d3b757dd0f0ab91da59face163d2a;hp=b9067ab60d431f260dad52a04ca05aaf0822b7d7;hpb=f550aee00a62fe1d8baf62d83cd7efef6cc2ee92;p=pspp diff --git a/src/libpspp/string-array.c b/src/libpspp/string-array.c index b9067ab60d..d162593b2c 100644 --- a/src/libpspp/string-array.c +++ b/src/libpspp/string-array.c @@ -23,6 +23,7 @@ #include #include "libpspp/array.h" +#include "libpspp/i18n.h" #include "libpspp/str.h" #include "gl/xalloc.h" @@ -234,6 +235,66 @@ 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; +} + +/* Returns true if A and B contain the same strings in the same order, + false otherwise. */ +bool +string_array_equal (const struct string_array *a, + const struct string_array *b) +{ + if (a->n != b->n) + return false; + + for (size_t i = 0; i < a->n; i++) + if (strcmp (a->strings[i], b->strings[i])) + return false; + return true; +} + +/* Returns true if A and B contain the same strings in the same order, + false otherwise. */ +bool +string_array_equal_case (const struct string_array *a, + const struct string_array *b) +{ + if (a->n != b->n) + return false; + + for (size_t i = 0; i < a->n; i++) + if (utf8_strcasecmp (a->strings[i], b->strings[i])) + return false; + return true; +} + +/* 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.