stclib / STC

A modern, user friendly, generic, type-safe and fast C99 container library: String, Vector, Sorted and Unordered Map and Set, Deque, Forward List, Smart Pointers, Bitset and Random numbers.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

`cstr_printf(&str, ...)` drops/rebuilds the string str

polijan opened this issue · comments

str_printf first drops itself and then rebuilds the string (possibly reallocating). While this actually could be fine, it's maybe a little inconsistent compared to the workings of other assignment routines (cstr_assign*) ?

I agree in principle, but it was intentional and needed for "self assignment" to work:

cstr s = cstr_new("hello world");
cstr_printf(&s, "new: %s, %s", cstr_str(&s), cstr_str(&s));

You need to allocate another buffer. Normal assign works with self assign (of substring) by using realloc() and memmove(), and append does a special trick to handle self append, so it can still use just realloc().
Thanks for your feedback and attention to details.