diff --git a/include/QF/set.h b/include/QF/set.h index 1261bdb40..52b1fae85 100644 --- a/include/QF/set.h +++ b/include/QF/set.h @@ -356,6 +356,7 @@ set_iter_t *set_first_r (set_pool_t *set_pool, const set_t *set); set_iter_t *set_next (set_iter_t *set_iter); set_iter_t *set_next_r (set_pool_t *set_pool, set_iter_t *set_iter); +struct dstring_s; /** Return a human-readable string representing the set. Empty sets will be represented by the string "{}". Sets of everything @@ -369,6 +370,21 @@ set_iter_t *set_next_r (set_pool_t *set_pool, set_iter_t *set_iter); will overwrite the results of preceeding calls. */ const char *set_as_string (const set_t *set); +/** Return a human-readable string representing the set. + + Empty sets will be represented by the string "{}". Sets of everything + will be represented by the string "{...}". Inverted sets will have + the first implicit member followed by "..." (eg, "256 ..."). + + \param str dstring to which the representation will be written + \param set The set to be converted to a string. + \return The string held in str + + \warning The string is NOT cleared, but rather the set representation + is appeneded to the string. This makes it more useful when + constructing strings in a threaded environment. +*/ +const char *set_as_string_r (struct dstring_s *str, const set_t *set); ///@} #endif//__QF_set_h diff --git a/libs/util/set.c b/libs/util/set.c index dfdd55b4e..6b15154e8 100644 --- a/libs/util/set.c +++ b/libs/util/set.c @@ -608,22 +608,19 @@ set_next (set_iter_t *set_iter) } const char * -set_as_string (const set_t *set) +set_as_string_r (dstring_t *str, const set_t *set) { - static dstring_t *str; unsigned i; - if (!str) - str = dstring_new (); if (set_is_empty (set)) { - dstring_copystr (str, "{}"); + dstring_appendstr (str, "{}"); return str->str; } if (set_is_everything (set)) { - dstring_copystr (str, "{...}"); + dstring_appendstr (str, "{...}"); return str->str; } - dstring_copystr (str, "{"); + dstring_appendstr (str, "{"); for (i = 0; i < set->size; i++) { if (set_is_member (set, i)) { if (str->str[1]) @@ -632,8 +629,21 @@ set_as_string (const set_t *set) dasprintf (str, "%d", i); } } - if (set->inverted) + if (set->inverted) { dasprintf (str, "%s%d ...", str->str[1] ? " " : "", i); + } dstring_appendstr (str, "}"); return str->str; } + +const char * +set_as_string (const set_t *set) +{ + static dstring_t *str; + + if (!str) { + str = dstring_new (); + } + dstring_clearstr (str); + return set_as_string_r (str, set); +}