-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcstring.c
More file actions
431 lines (341 loc) · 8.54 KB
/
cstring.c
File metadata and controls
431 lines (341 loc) · 8.54 KB
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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
#include "cstring.h"
#include <assert.h>
#include <errno.h>
#include <limits.h>
#include <stdlib.h>
#include <string.h>
/// Helper macro to return with error code if condition is false.
#define return_unless(cond, err, ret) \
do { \
if (!(cond)) { \
errno = (err); \
return (ret); \
} \
} while (0)
/// Growth factor for capacity when resizing.
/// 2.0x balances memory overhead (~50% extra on average after growth) with reallocation frequency.
/// See Herb Sutter's "Allocators" article for analysis of growth factors.
#define STRING_GROWTH_FACTOR 2
/// Small string optimization.
/// Holds up to 7 chars + NUL inline.
/// Arbitrary choice.
#define SSO_SIZE 8
struct string {
/// Capacity of buffer (excluding NUL terminator).
size_t cap;
/// Length of buffer (excluding NUL terminator).
size_t len;
/// Buffer (always NUL terminated).
char *buf;
/// Internal storage (small string optimization).
char sso[SSO_SIZE];
};
#define SSO_CAPACITY (sizeof(((struct string *)0)->sso) - 1 /* Space for NUL */)
struct string *string_new(void)
{
struct string *str = NULL;
str = calloc(1, sizeof(struct string));
if (!str) {
errno = ENOMEM;
return NULL;
}
str->buf = str->sso;
str->cap = SSO_CAPACITY;
return str;
}
static bool internal_storage_used(const struct string *str)
{
// Precondition.
assert(str);
return str->buf == str->sso;
}
void string_delete(struct string *str)
{
if (!str) {
return;
}
if (!internal_storage_used(str)) {
free(str->buf);
}
str->buf = NULL;
free(str);
}
bool string_empty(const struct string *str)
{
return string_size(str) == 0;
}
size_t string_size(const struct string *str)
{
if (!str) {
return 0;
}
return str->len;
}
int string_reserve(struct string *str, size_t cap)
{
char *buf;
bool is_sso;
return_unless(str, EFAULT, -1);
if (cap == SIZE_MAX) {
// Cannot allocate enough memory to hold NUL terminator.
errno = ENOMEM;
return -1;
}
if (cap < str->len) {
return 0;
}
is_sso = internal_storage_used(str);
if (is_sso) {
buf = malloc(cap + 1);
} else {
buf = realloc(str->buf, cap + 1);
}
if (!buf) {
errno = ENOMEM;
return -1;
}
if (is_sso) {
memcpy(buf, str->sso, str->len + 1);
}
str->cap = cap;
str->buf = buf;
str->buf[str->len] = 0;
return 0;
}
size_t string_capacity(const struct string *str)
{
if (!str) {
return 0;
}
return str->cap;
}
char string_at(const struct string *str, size_t pos)
{
if (!str) {
return 0;
}
if (pos >= str->len) {
return 0;
}
return str->buf[pos];
}
const char *string_c_str(const struct string *str)
{
return_unless(str, EFAULT, NULL);
return str->buf;
}
char *string_c_str_move(struct string *str)
{
char *buf;
return_unless(str, EFAULT, NULL);
if (internal_storage_used(str)) {
// Duplicate internal storage.
buf = strdup(str->sso);
if (!buf) {
errno = ENOMEM;
return NULL;
}
} else {
// Detach allocated buffer.
buf = str->buf;
}
str->buf = str->sso;
str->cap = SSO_CAPACITY;
str->len = 0;
str->buf[0] = 0;
return buf;
}
void string_clear(struct string *str)
{
if (!str) {
return;
}
str->len = 0;
str->buf[0] = 0;
}
/// Avoid performance issues with repeated small appends.
/// @return New capacity to reserve.
static size_t compute_growth(size_t current, size_t required)
{
size_t doubled;
// Precondition.
assert(required > 0);
if (current == 0) {
return required;
}
if (current > SIZE_MAX / STRING_GROWTH_FACTOR) {
// Doubling would overflow SIZE_MAX; grow to exact required size instead.
return required;
}
doubled = current * STRING_GROWTH_FACTOR;
return (doubled > required) ? doubled : required;
}
/// Insert @c n characters at position @c pos.
/// @return Pointer to the start of the inserted area on success, NULL on failure.
static char *impl_insert(struct string *str, size_t pos, size_t n)
{
size_t required;
char *dest;
// Precondition.
assert(str);
if (n > SIZE_MAX - str->len) {
// Check for overflow.
errno = ENOMEM;
return NULL;
}
required = str->len + n;
if (required > str->cap) {
int r = string_reserve(str, compute_growth(str->cap, required));
if (r < 0) {
return NULL;
}
}
dest = &str->buf[pos];
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 dest;
}
/// Insert buffer @c s of length @c n at position @c pos.
/// @return 0 on success, or -1 otherwise.
static int impl_insert_buffer(struct string *str, size_t pos, size_t n, const char *s)
{
char *dest;
// Preconditions.
assert(str);
assert(s);
dest = impl_insert(str, pos, n);
if (!dest) {
return -1;
}
memmove(dest, s, n);
return 0;
}
/// Insert @c n copies of character @c c at position @c pos.
/// @return 0 on success, or -1 otherwise.
static int impl_insert_fill(struct string *str, size_t pos, size_t n, char c)
{
char *dest;
// Precondition.
assert(str);
dest = impl_insert(str, pos, n);
if (!dest) {
return -1;
}
memset(dest, c, n);
return 0;
}
int string_insert_buffer(struct string *str, size_t pos, size_t n, const char *s)
{
return_unless(str, EFAULT, -1);
return_unless(s, EFAULT, -1);
return_unless(pos <= str->len, ERANGE, -1);
return impl_insert_buffer(str, pos, n, s);
}
int string_insert_c_str(struct string *str, size_t pos, const char *s)
{
return_unless(str, EFAULT, -1);
return_unless(s, EFAULT, -1);
return_unless(pos <= str->len, ERANGE, -1);
return impl_insert_buffer(str, pos, strlen(s), s);
}
int string_insert_fill(struct string *str, size_t pos, size_t n, char c)
{
return_unless(str, EFAULT, -1);
return_unless(pos <= str->len, ERANGE, -1);
return impl_insert_fill(str, pos, n, c);
}
int string_erase(struct string *str, size_t pos, size_t len)
{
size_t rhs;
size_t n;
return_unless(str, EFAULT, -1);
return_unless(pos <= str->len, ERANGE, -1);
// For consistency with string_insert_*().
if (pos == str->len) {
return 0;
}
if (len == 0) {
return 0;
}
// rhs
// /--------------\
// len n
// /----\/--------\
// --+--+--+--+--+--+
// | | | | | |
// --+--+--+--+--+--+
// ^ ^
// pos str->len
rhs = str->len - pos;
n = 0;
if (len > rhs) {
// Erase as many as possible.
len = rhs;
}
n = rhs - len;
memmove(&str->buf[pos],
&str->buf[pos + len],
n + 1);
str->len -= len;
return 0;
}
int string_push_back(struct string *str, char c)
{
return_unless(str, EFAULT, -1);
return impl_insert_fill(str, str->len, 1, c);
}
int string_pop_back(struct string *str)
{
return_unless(str, EFAULT, -1);
return_unless(str->len > 0, ERANGE, -1);
str->buf[--str->len] = 0;
return 0;
}
int string_append_buffer(struct string *str, size_t n, const char *s)
{
return_unless(str, EFAULT, -1);
return_unless(s, EFAULT, -1);
return impl_insert_buffer(str, str->len, n, s);
}
int string_append_c_str(struct string *str, const char *s)
{
return_unless(str, EFAULT, -1);
return_unless(s, EFAULT, -1);
return impl_insert_buffer(str, str->len, strlen(s), s);
}
int string_append_fill(struct string *str, size_t n, char c)
{
return_unless(str, EFAULT, -1);
return impl_insert_fill(str, str->len, n, c);
}
struct string *string_substr(const struct string *str, size_t pos, size_t len)
{
struct string *sub;
int r;
return_unless(str, EFAULT, NULL);
return_unless(pos <= str->len, ERANGE, NULL);
if (len > SIZE_MAX - pos) {
// Cap in case of numerical overflow.
len = str->len - pos;
} else if (pos + len > str->len) {
// Cap to [pos, size()).
len = str->len - pos;
}
sub = string_new();
if (!sub) {
return NULL;
}
r = string_append_buffer(sub, len, &str->buf[pos]);
if (r < 0) {
string_delete(sub);
return NULL;
}
return sub;
}