[util] Add re-entrant set_as_string

Makes it easier to debug set-related code in multi-threaded code.
This commit is contained in:
Bill Currie 2021-03-28 20:25:57 +09:00
parent ff4cd84891
commit 00aa584506
2 changed files with 34 additions and 8 deletions

View file

@ -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

View file

@ -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);
}