-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
StringBuilder is utility struct. Connects Format..() functions with your allocated string. Abstracting creating string is eases printing messages to user.
- Loading branch information
Showing
3 changed files
with
159 additions
and
187 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.