Skip to content

Commit

Permalink
src: make it easy to print strings
Browse files Browse the repository at this point in the history
StringBuilder is utility struct. Connects Format..() functions with
your allocated string.
Abstracting creating string is eases printing messages to user.
  • Loading branch information
e2dk4r committed Oct 29, 2024
1 parent 8076433 commit b681da4
Show file tree
Hide file tree
Showing 3 changed files with 159 additions and 187 deletions.
72 changes: 72 additions & 0 deletions include/StringBuilder.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
#include "memory.h"
#include "text.h"

struct string_builder {
struct string *outBuffer;
struct string *stringBuffer;
u64 length;
};

static inline void
StringBuilderAppendString(struct string_builder *stringBuilder, struct string *string)
{
struct string *outBuffer = stringBuilder->outBuffer;
memcpy(outBuffer->value + stringBuilder->length, string->value, string->length);
stringBuilder->length += string->length;
debug_assert(stringBuilder->length <= outBuffer->length);
}

static inline void
StringBuilderAppendU64(struct string_builder *stringBuilder, u64 value)
{
struct string *outBuffer = stringBuilder->outBuffer;
struct string *stringBuffer = stringBuilder->stringBuffer;

struct string string = FormatU64(stringBuffer, value);
memcpy(outBuffer->value + stringBuilder->length, string.value, string.length);
stringBuilder->length += string.length;
debug_assert(stringBuilder->length <= outBuffer->length);
}

static inline void
StringBuilderAppendHex(struct string_builder *stringBuilder, u64 value)
{
struct string *outBuffer = stringBuilder->outBuffer;
struct string *stringBuffer = stringBuilder->stringBuffer;

struct string string = FormatHex(stringBuffer, value);
memcpy(outBuffer->value + stringBuilder->length, string.value, string.length);
stringBuilder->length += string.length;
debug_assert(stringBuilder->length <= outBuffer->length);
}

static inline void
StringBuilderAppendF32(struct string_builder *stringBuilder, f32 value, u64 fractionCount)
{
struct string *outBuffer = stringBuilder->outBuffer;
struct string *stringBuffer = stringBuilder->stringBuffer;

struct string string = FormatF32(stringBuffer, value, fractionCount);
memcpy(outBuffer->value + stringBuilder->length, string.value, string.length);
stringBuilder->length += string.length;
debug_assert(stringBuilder->length <= outBuffer->length);
}

/*
* Returns string that is ready for transmit.
* Also resets length of builder.
*
* @code
* StringBuilderAppend..(stringBuilder, x);
* struct string string = StringBuilderFlush(stringBuilder);
* write(x, string.value, string.length);
* @endcode
*/
static inline struct string
StringBuilderFlush(struct string_builder *stringBuilder)
{
struct string *outBuffer = stringBuilder->outBuffer;
struct string result = (struct string){.value = outBuffer->value, .length = stringBuilder->length};
stringBuilder->length = 0;
return result;
}
1 change: 1 addition & 0 deletions meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ add_project_arguments(
'-funroll-loops',
'-fomit-frame-pointer',

'-Wno-unused-result',
'-Wno-unused-function',

'-DCOMPILER_GCC=' + (cc.get_id() == 'gcc').to_int().to_string(),
Expand Down
Loading

0 comments on commit b681da4

Please sign in to comment.