X-Git-Url: https://pintos-os.org/cgi-bin/gitweb.cgi?a=blobdiff_plain;ds=sidebyside;f=src%2Flibpspp%2Fstring-array.c;h=d4badf4638df124681e964f78286a9cb3fda02bc;hb=261e25786073840d341ffdd5c36a08b0b9e46504;hp=a22ade6e10880e28fb47bbca5010ec63a3fa7737;hpb=f967d0e36a2193c1249799f463ea9109b753f7a8;p=pspp diff --git a/src/libpspp/string-array.c b/src/libpspp/string-array.c index a22ade6e10..d4badf4638 100644 --- a/src/libpspp/string-array.c +++ b/src/libpspp/string-array.c @@ -234,6 +234,25 @@ 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,