-
Notifications
You must be signed in to change notification settings - Fork 1
/
StringBuilder.h
63 lines (48 loc) · 1.79 KB
/
StringBuilder.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#pragma once
#include "Kr/KrCommon.h"
constexpr uint32_t STRING_BUILDER_BUCKET_SIZE = 16 * 1024;
struct String_Builder {
struct Bucket {
Bucket *next = nullptr;
int32_t written = 0;
uint8_t data[STRING_BUILDER_BUCKET_SIZE];
};
Bucket head;
Bucket *current = &head;
int64_t written = 0;
Bucket *free_list = nullptr;
Memory_Allocator allocator = ThreadContext.allocator;
};
int WriteBuffer(String_Builder *builder, void *buffer, int64_t size);
int Write(String_Builder *builder, bool value);
int Write(String_Builder *builder, char value);
int Write(String_Builder *builder, uint8_t value);
int Write(String_Builder *builder, int32_t value);
int Write(String_Builder *builder, uint32_t value);
int Write(String_Builder *builder, int64_t value);
int Write(String_Builder *builder, uint64_t value);
int Write(String_Builder *builder, float value);
int Write(String_Builder *builder, double value);
int Write(String_Builder *builder, void *value);
int Write(String_Builder *builder, const char *value);
int Write(String_Builder *builder, String value);
template <int64_t _Length>
int Write(String_Builder *builder, const char(&a)[_Length]) {
return Write(builder, String(a, _Length));
}
int WriteFormatted(String_Builder *builder, const char *format);
template <typename Type, typename ...Args>
int WriteFormatted(String_Builder *builder, const char *format, Type value, Args... args) {
int written = 0;
for (; *format; ++format) {
if (*format == '%') {
written += Write(builder, value);
return written + WriteFormatted(builder, format + 1, args...);
}
written += Write(builder, *format);
}
return written;
}
String BuildString(String_Builder *builder, Memory_Allocator &allocator = ThreadContext.allocator);
void ResetBuilder(String_Builder *builder);
void FreeBuilder(String_Builder *builder);