X-Git-Url: https://pintos-os.org/cgi-bin/gitweb.cgi?a=blobdiff_plain;f=src%2Flibpspp%2Fstr.c;h=cf0069dd3d4e46e5ba292cdbb352fd4d4b89e48b;hb=8e018d25310cb53e5339b46e95f0abe02db83782;hp=6d25c339fad9550d618f63d6f33f9ba3f442eec7;hpb=b13f17becf74e4335c7e3206e4347cac07b38c56;p=pspp-builds.git diff --git a/src/libpspp/str.c b/src/libpspp/str.c index 6d25c339..cf0069dd 100644 --- a/src/libpspp/str.c +++ b/src/libpspp/str.c @@ -27,6 +27,7 @@ #include #include +#include #include "minmax.h" #include "size_max.h" @@ -676,6 +677,30 @@ ds_swap (struct string *a, struct string *b) *b = tmp; } +/* Helper function for ds_register_pool. */ +static void +free_string (void *st_) +{ + struct string *st = st_; + ds_destroy (st); +} + +/* Arranges for ST to be destroyed automatically as part of + POOL. */ +void +ds_register_pool (struct string *st, struct pool *pool) +{ + pool_register (pool, free_string, st); +} + +/* Cancels the arrangement for ST to be destroyed automatically + as part of POOL. */ +void +ds_unregister_pool (struct string *st, struct pool *pool) +{ + pool_unregister (pool, st); +} + /* Copies SRC into DST. DST and SRC may be the same string. */ void @@ -860,6 +885,18 @@ ds_rpad (struct string *st, size_t length, char pad) ds_put_char_multiple (st, pad, length - st->ss.length); } +/* Sets the length of ST to exactly NEW_LENGTH, + either by truncating characters from the end, + or by padding on the right with PAD. */ +void +ds_set_length (struct string *st, size_t new_length, char pad) +{ + if (st->ss.length < new_length) + ds_rpad (st, new_length, pad); + else + st->ss.length = new_length; +} + /* Returns true if ST is empty, false otherwise. */ bool ds_is_empty (const struct string *st)