added arena_sb_append_format and arena_sb_append_vformat #14
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
I figured it would be nice to append to a string builder the same way we do to a file with format strings and printf-type functions.
Because string builder are user-defined structure we need to use macros. I introduced two of them:
arena_sb_append_formatandarena_sb_append_vformat.arena_sb_append_formatenables us to do stuff likewhere
sbis a predefined string builder (see example 001).Note that
arena_sb_append_formatbehaves differently toarena_sprintf:arena_sprintfallocates a new buffer and copies the character data into it.On the other hand,
arena_sb_append_formatappends into an existing string builder so it might not even do further allocations!Furthermore note that we need to take into account that printf-type functions put a
NULLcharacter at the end of the string.So the capacity of the string builder needs to be big enough to hold that extra zero but when updating the count we only count the characters before the terminating
NULL.That way we can run
arena_sb_append_formatrepeatedly without having to deal with a zero between two calls.The use case for
arena_append_vformatis probably rare but I might as well include it.One could imagine a function that behaves like
arena_sb_append_formatand usesarena_append_vformat:Sadly I don't see a possibility to express
arena_sb_append_formatin terms ofarena_sb_append_vformatnicely. We would need to convert a (preprocessing-time) macro argument list to a (compile-time) function argument list.