Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
Add string builder utility.
  • Loading branch information
e2dk4r committed Oct 28, 2024
1 parent 8076433 commit d9af5e5
Show file tree
Hide file tree
Showing 3 changed files with 164 additions and 189 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 d9af5e5

Please sign in to comment.