-
Notifications
You must be signed in to change notification settings - Fork 0
/
cstring.c
246 lines (207 loc) · 4.55 KB
/
cstring.c
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
245
246
#include "cstring.h"
#include <stdlib.h>
#include <string.h>
struct string {
size_t cap;
size_t len;
char *buf;
};
static struct {
void *(*realloc)(void *, size_t);
void (*dealloc)(void *);
} mem = {
realloc,
free
};
void string_allocator_set(
void *(*realloc)(void *, size_t),
void (*dealloc)(void *))
{
mem.realloc = realloc;
mem.dealloc = dealloc;
}
static char *impl_append(struct string *str, size_t n)
{
size_t pos = str->len;
size_t len_ = str->len + n;
if (len_ > str->cap || !str->buf) {
str->cap = len_;
str->buf = mem.realloc(str->buf, str->cap + 1);
}
str->buf[pos + n] = 0;
str->len += n;
return &str->buf[pos];
}
void string_append_buffer(struct string *str, const char *s, size_t n)
{
if (s && *s && n) {
memcpy(impl_append(str, n), s, n);
}
}
void string_append_c_str(struct string *str, const char *s)
{
if (s && *s) {
strcpy(impl_append(str, strlen(s)), s);
}
}
void string_append_fill(struct string *str, size_t n, char c)
{
if (n) {
memset(impl_append(str, n), c, n);
}
}
char string_at(const struct string *str, size_t pos)
{
if (pos >= str->len) {
return 0;
}
return str->buf[pos];
}
char string_back(const struct string *str)
{
if (str->len > 0) {
return str->buf[str->len - 1];
}
return 0;
}
const char *string_c_str(const struct string *str)
{
return str->buf;
}
char *string_c_str_move(struct string *str)
{
char *buf = str->buf;
str->cap = 0;
str->len = 0;
str->buf = NULL;
return buf;
}
size_t string_capacity(const struct string *str)
{
return str->cap;
}
void string_clear(struct string *str)
{
str->len = 0;
if (str->buf) {
str->buf[0] = 0;
}
}
void string_delete(struct string *str)
{
mem.dealloc(str->buf);
mem.dealloc(str);
}
bool string_empty(const struct string *str)
{
return string_size(str) == 0;
}
void string_erase(struct string *str, size_t pos, size_t len)
{
/*
* rhs
* /--------------\
* len n
* /----\/--------\
* --+--+--+--+--+--+
* | | | | | |
* --+--+--+--+--+--+
* ^ ^
* pos str->len
*/
if (pos < str->len) {
size_t rhs = str->len - pos;
size_t n = 0;
if (len > rhs) {
len = rhs;
}
n = rhs - len;
memmove(&str->buf[pos],
&str->buf[pos + len],
n + 1);
str->len -= len;
}
}
static char *impl_insert(struct string *str, size_t pos, size_t n)
{
size_t len_ = str->len + n;
if (len_ > str->cap) {
str->cap = len_;
str->buf = mem.realloc(str->buf, str->cap + 1);
}
if (pos < str->len) {
size_t rhs = str->len - pos;
memmove(&str->buf[pos + n],
&str->buf[pos],
rhs + 1);
} else {
str->buf[pos + n] = 0;
}
str->len += n;
return &str->buf[pos];
}
void string_insert_buffer(struct string *str, size_t pos, const char *s, size_t n)
{
if (s && *s && n) {
memcpy(impl_insert(str, pos, n), s, n);
}
}
void string_insert_c_str(struct string *str, size_t pos, const char *s)
{
if (s && *s) {
size_t n = strlen(s);
memcpy(impl_insert(str, pos, n), s, n);
}
}
void string_insert_fill(struct string *str, size_t pos, size_t n, char c)
{
if (n) {
memset(impl_insert(str, pos, n), c, n);
}
}
struct string *string_new(void)
{
struct string *str = mem.realloc(NULL, sizeof(struct string));
str->cap = 0;
str->len = 0;
str->buf = NULL;
return str;
}
void string_pop_back(struct string *str)
{
if (str->len > 0) {
str->buf[--str->len] = 0;
}
}
void string_push_back(struct string *str, char c)
{
string_append_fill(str, 1, c);
}
void string_reserve(struct string *str, size_t cap)
{
if (str->cap < cap) {
str->cap = cap;
str->buf = mem.realloc(str->buf, str->cap + 1);
if (str->len == 0) {
str->buf[0] = 0;
}
}
}
size_t string_size(const struct string *str)
{
return str->len;
}
struct string *string_substr(const struct string *str, size_t pos, size_t len)
{
struct string *sub = string_new();
if (len == 0) {
// Return a newly constructed (empty) string.
} else if (pos < str->len) {
if (pos + len > str->len) {
// Cap.
len = str->len - pos;
}
string_append_buffer(sub, &str->buf[pos], len);
}
return sub;
}