-
Notifications
You must be signed in to change notification settings - Fork 0
/
stringbuilder.h
244 lines (199 loc) · 6.13 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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
#ifndef STRING_BUILDER_H
#define STRING_BUILDER_H
#include <stdarg.h>
#include <stdbool.h>
#include <stdlib.h>
#ifdef __cplusplus
extern "C" {
#endif
typedef struct
{
char *value;
size_t length;
size_t capacity;
} StringBuilder;
StringBuilder sb_create(void);
StringBuilder sb_create_and_allocate(size_t capacity);
void sb_destroy(StringBuilder *sb);
void sb_init(StringBuilder *sb);
bool sb_ensure_capacity(StringBuilder *sb, size_t min_capacity);
void sb_append(StringBuilder *sb, const char *value);
void sb_append_ex(StringBuilder *sb, const char *value, size_t length);
void sb_append_line(StringBuilder *sb, const char *value);
void sb_append_format(StringBuilder *sb, const char *format, ...);
void sb_append_format_list(StringBuilder *sb, const char *format, va_list args);
void sb_append_char(StringBuilder *sb, char value);
void sb_insert(StringBuilder *sb, size_t index, const char *value);
void sb_insert_ex(StringBuilder *sb, size_t index, const char *value, size_t length);
void sb_remove(StringBuilder *sb, size_t index, size_t length);
void sb_replace(StringBuilder *sb, const char *old_value, const char *new_value);
void sb_clear(StringBuilder *sb);
#ifdef __cplusplus
}
#endif
#endif // STRING_BUILDER_H
#ifdef STRING_BUILDER_IMPLEMENTATION
#include <stdio.h>
#include <string.h>
#ifndef STRING_BUILDER_NEWLINE
#define STRING_BUILDER_NEWLINE "\n"
#endif
StringBuilder sb_create(void)
{
StringBuilder sb;
sb_init(&sb);
return sb;
}
StringBuilder sb_create_and_allocate(size_t capacity)
{
StringBuilder sb;
sb_init(&sb);
if (capacity > 0 && sb_ensure_capacity(&sb, capacity))
sb.value[0] = '\0';
return sb;
}
void sb_destroy(StringBuilder *sb)
{
free(sb->value);
}
void sb_init(StringBuilder *sb)
{
sb->value = NULL;
sb->length = 0;
sb->capacity = 0;
}
bool sb_ensure_capacity(StringBuilder *sb, size_t min_capacity)
{
if (sb->capacity < min_capacity) {
size_t capacity = 16;
while (capacity < min_capacity && capacity > 0)
capacity *= 2;
if (capacity == 0)
return false;
char *value = (char *)realloc(sb->value, capacity);
if (value == NULL)
return false;
sb->value = value;
sb->capacity = capacity;
}
return true;
}
void sb_append(StringBuilder *sb, const char *value)
{
if (value)
sb_append_ex(sb, value, strlen(value));
}
void sb_append_ex(StringBuilder *sb, const char *value, size_t length)
{
if (length > 0 && sb_ensure_capacity(sb, sb->length + length + 1)) {
memcpy(sb->value + sb->length, value, length);
sb->length += length;
sb->value[sb->length] = '\0';
}
}
void sb_append_line(StringBuilder *sb, const char *value)
{
sb_append(sb, value);
sb_append(sb, STRING_BUILDER_NEWLINE);
}
void sb_append_format(StringBuilder *sb, const char *format, ...)
{
va_list args;
va_start(args, format);
sb_append_format_list(sb, format, args);
va_end(args);
}
void sb_append_format_list(StringBuilder *sb, const char *format, va_list args)
{
va_list args_copy;
va_copy(args_copy, args);
char buffer[4096];
int needed_length = vsnprintf(buffer, sizeof(buffer), format, args_copy);
va_end(args_copy);
if (needed_length < 0) {
return;
}
size_t length = (size_t)needed_length;
if (length < sizeof(buffer)) {
sb_append_ex(sb, buffer, length);
} else {
if (sb_ensure_capacity(sb, sb->length + length + 1)) {
vsnprintf(sb->value + sb->length, length + 1, format, args);
sb->length += length;
}
}
}
void sb_append_char(StringBuilder *sb, char value)
{
if (value != '\0')
sb_append_ex(sb, &value, 1);
}
void sb_insert(StringBuilder *sb, size_t index, const char *value)
{
if (value)
sb_insert_ex(sb, index, value, strlen(value));
}
void sb_insert_ex(StringBuilder *sb, size_t index, const char *value, size_t length)
{
if (index < sb->length) {
if (length > 0 && sb_ensure_capacity(sb, sb->length + length + 1)) {
char *offset = sb->value + index;
memmove(offset + length, offset, sb->length - index);
memmove(offset, value, length);
sb->length += length;
sb->value[sb->length] = '\0';
}
} else {
sb_append_ex(sb, value, length);
}
}
void sb_remove(StringBuilder *sb, size_t index, size_t length)
{
if (index >= sb->length || length == 0)
return;
if (index + length < sb->length) {
size_t num_chars_to_move = sb->length - length - index;
char *offset = sb->value + index;
memmove(offset, offset + length, num_chars_to_move);
sb->length -= length;
} else {
sb->length = index;
}
sb->value[sb->length] = '\0';
}
void sb_replace(StringBuilder *sb, const char *old_value, const char *new_value)
{
if (sb->length == 0 || old_value == NULL || new_value == NULL)
return;
size_t old_length = strlen(old_value);
size_t new_length = strlen(new_value);
char *match = strstr(sb->value, old_value);
if (old_length == 0 || match == NULL)
return;
if (old_length == new_length) {
while (match) {
memcpy(match, new_value, new_length);
match = strstr(match + old_length, old_value);
}
} else {
StringBuilder tmp = sb_create();
char *start = sb->value;
while (match) {
sb_append_ex(&tmp, start, match - start);
sb_append_ex(&tmp, new_value, new_length);
start = match + old_length;
match = strstr(start, old_value);
}
if (*start != '\0')
sb_append_ex(&tmp, start, sb->value + sb->length - start);
free(sb->value);
*sb = tmp;
}
}
void sb_clear(StringBuilder *sb)
{
if (sb->value)
sb->value[0] = '\0';
sb->length = 0;
}
#endif // STRING_BUILDER_IMPLEMENTATION