• Thomas Helland's avatar
    util: Add a string buffer implementation · 7885bb68
    Thomas Helland authored
    Based on Vladislav Egorovs work on the preprocessor, but split
    out to a util functionality that should be universal. Setup, teardown,
    memory handling and general layout is modeled around the hash_table
    and the set, to make it familiar for everyone.
    A notable change is that this implementation is always null terminated.
    The rationale is that it will be less error-prone, as one might
    access the buffer directly, thereby reading a non-terminated string.
    Also, vsnprintf and friends prints the null-terminator.
    Signed-off-by: Thomas Helland's avatarThomas Helland <thomashelland90@gmail.com>
    Tested-by: Dieter Nützel <Dieter at nuetzel-hh.de>
    Reviewed-by: default avatarNicolai Hähnle <nicolai.haehnle@amd.com>
    V2: Address review feedback from Timothy and Grazvydas
       - Fix MINGW preprocessor check
       - Changed len from uint to int
       - Make string argument const in append function
       - Move to header and inline append function
       - Add crimp_to_fit function for resizing buffer
    V3: Move include of ralloc to string_buffer.h
    V4: Use u_string.h for a cross-platform working vsnprintf
    V5: Remember to cast to char * in crimp function
    V6: Address review feedback from Nicolai
       - Handle !str->buf in buffer_create
       - Ensure va_end is always called in buffer_append_all
       - Add overflow check in buffer_append_len
       - Do not expose buffer_space_left, just remove it
       - Clarify why a loop is used in vprintf, change to for-loop
       - Add a va_copy to buffer_vprintf to fix failure to append arguments
         when having to resize the buffer for vsnprintf.
    V7: Address more review feedback from Nicolai
       - Add missing va_end corresponding to va_copy
       - Error check failure to allocate in crimp_to_fit
string_buffer.c 4.21 KB