X-Git-Url: https://pintos-os.org/cgi-bin/gitweb.cgi?a=blobdiff_plain;f=src%2Flibpspp%2Fstringi-set.c;h=b442a41567805a9ca92d431fc413468f5f9abc22;hb=300b8be9a0ce46e5dae3895b99bc8d54eaf6edfb;hp=36745742d477350ffbb4f08cd5d8b7eb6ab94d39;hpb=ac4a498528bce608cd2009bef01fd08d049a02af;p=pspp diff --git a/src/libpspp/stringi-set.c b/src/libpspp/stringi-set.c index 36745742d4..b442a41567 100644 --- a/src/libpspp/stringi-set.c +++ b/src/libpspp/stringi-set.c @@ -1,5 +1,5 @@ /* PSPP - a program for statistical analysis. - Copyright (C) 2009, 2010 Free Software Foundation, Inc. + Copyright (C) 2009, 2010, 2012 Free Software Foundation, Inc. This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -19,12 +19,14 @@ #include -#include +#include "libpspp/stringi-set.h" #include #include -#include +#include "libpspp/cast.h" +#include "libpspp/hash-functions.h" +#include "libpspp/i18n.h" #include "gl/xalloc.h" @@ -87,7 +89,7 @@ stringi_set_contains (const struct stringi_set *set, const char *s) struct stringi_set_node * stringi_set_find_node (const struct stringi_set *set, const char *s) { - return stringi_set_find_node__ (set, s, hash_case_string (s, 0)); + return stringi_set_find_node__ (set, s, utf8_hash_case_string (s, 0)); } /* Inserts a copy of S into SET. Returns true if successful, false if SET @@ -95,7 +97,7 @@ stringi_set_find_node (const struct stringi_set *set, const char *s) bool stringi_set_insert (struct stringi_set *set, const char *s) { - unsigned int hash = hash_case_string (s, 0); + unsigned int hash = utf8_hash_case_string (s, 0); if (!stringi_set_find_node__ (set, s, hash)) { stringi_set_insert__ (set, xstrdup (s), hash); @@ -111,7 +113,7 @@ stringi_set_insert (struct stringi_set *set, const char *s) bool stringi_set_insert_nocopy (struct stringi_set *set, char *s) { - unsigned int hash = hash_case_string (s, 0); + unsigned int hash = utf8_hash_case_string (s, 0); if (!stringi_set_find_node__ (set, s, hash)) { stringi_set_insert__ (set, s, hash); @@ -129,7 +131,7 @@ stringi_set_insert_nocopy (struct stringi_set *set, char *s) bool stringi_set_delete (struct stringi_set *set, const char *s) { - return stringi_set_delete__ (set, s, hash_case_string (s, 0)); + return stringi_set_delete__ (set, s, utf8_hash_case_string (s, 0)); } /* Deletes NODE from SET, and frees NODE and its string. */ @@ -226,6 +228,54 @@ stringi_set_subtract (struct stringi_set *a, const struct stringi_set *b) stringi_set_delete__ (a, node->string, node->hmap_node.hash); } } + +/* Allocates and returns an array that points to each of the strings in SET. + The caller must not free or modify any of the strings. Removing a string + from SET invalidates the corresponding element of the returned array. The + caller it is responsible for freeing the returned array itself (with + free()). + + The returned array is in the same order as observed by stringi_set_first() + and stringi_set_next(), that is, no particular order. */ +char ** +stringi_set_get_array (const struct stringi_set *set) +{ + const struct stringi_set_node *node; + const char *s; + char **array; + size_t i; + + array = xnmalloc (stringi_set_count (set), sizeof *array); + + i = 0; + STRINGI_SET_FOR_EACH (s, node, set) + array[i++] = CONST_CAST (char *, s); + + return array; +} + +static int +compare_strings (const void *a_, const void *b_) +{ + const char *const *a = a_; + const char *const *b = b_; + return utf8_strcasecmp (*a, *b); +} + +/* Allocates and returns an array that points to each of the strings in SET. + The caller must not free or modify any of the strings. Removing a string + from SET invalidates the corresponding element of the returned array. The + caller it is responsible for freeing the returned array itself (with + free()). + + The returned array is ordered according to utf8_strcasecmp(). */ +char ** +stringi_set_get_sorted_array (const struct stringi_set *set) +{ + char **array = stringi_set_get_array (set); + qsort (array, stringi_set_count (set), sizeof *array, compare_strings); + return array; +} /* Internal functions. */ @@ -237,7 +287,7 @@ stringi_set_find_node__ (const struct stringi_set *set, const char *s, HMAP_FOR_EACH_WITH_HASH (node, struct stringi_set_node, hmap_node, hash, &set->hmap) - if (!strcasecmp (s, node->string)) + if (!utf8_strcasecmp (s, node->string)) return node; return NULL;