-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathvstring.h
436 lines (370 loc) · 8.26 KB
/
vstring.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
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
432
433
434
435
436
/*
* vstring: https://github.com/dhobsd/vstring
*
* Copyright 2003-2016 Devon H. O'Dell <[email protected]>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#ifndef VSTRING_H_
#define VSTRING_H_
#include <assert.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <math.h>
#ifndef vstring_inline
# if defined(_MSC_VER) || defined(__BORLANDC__) || \
defined(__DMC__) || defined(__SC__) || \
defined(__WATCOMC__) || defined(__LCC__) || \
defined(__DECC) || defined(__EABI__)
# define vstring_inline __inline
# else
# define vstring_inline inline
# endif
#endif
typedef struct vstring_malloc {
void *(*vs_malloc)(size_t);
void *(*vs_realloc)(void *, size_t);
void (*vs_free)(void *);
} vstring_malloc;
enum vstring_type {
VS_TYPE_DYNAMIC = 1,
VS_TYPE_STATIC = 1 << 1,
VS_TYPE_GROWABLE = 1 << 2,
VS_TYPE_MAX = 0x7fffffff,
};
enum vstring_flags {
VS_NEEDSFREE = 1, /* Set if the API needs to free the vs itself */
VS_FLAG_MAX = 0x7fffffff,
};
typedef struct vstring {
char *contents;
enum vstring_type type;
enum vstring_flags flags;
uint64_t pointer;
uint64_t size;
vstring_malloc vm;
char vs___pad[8];
} vstring;
enum {
VS_ALLOCSIZE = 256,
};
static vstring_inline vstring *
vs_init(vstring *vs, vstring_malloc *vm, enum vstring_type type, char *buf,
size_t size)
{
if ((type & VS_TYPE_DYNAMIC)) {
if (vs == NULL) {
if (vm != NULL) {
vs = vm->vs_malloc(sizeof (*vs));
if (vs == NULL) {
return NULL;
}
memset(vs, 0, sizeof (*vs));
} else {
vs = calloc(1, sizeof (*vs));
if (vs == NULL) {
return NULL;
}
}
vs->flags |= VS_NEEDSFREE;
} else {
memset(vs, 0, sizeof (*vs));
}
if (buf != NULL && size > 0) {
vs->contents = buf;
vs->size = size;
}
} else if ((type & VS_TYPE_STATIC) || (type & VS_TYPE_GROWABLE)) {
assert(buf != NULL);
assert(size > 0);
if (vs == NULL) {
if (vm != NULL) {
vs = vm->vs_malloc(sizeof (*vs));
if (vs == NULL) {
return NULL;
}
memset(vs, 0, sizeof (*vs));
} else {
vs = calloc(1, sizeof (*vs));
if (vs == NULL) {
return NULL;
}
}
vs->flags |= VS_NEEDSFREE;
} else {
memset(vs, 0, sizeof (*vs));
}
vs->contents = buf;
vs->size = size;
}
vs->type = type;
if (vm != NULL) {
vs->vm.vs_malloc = vm->vs_malloc;
vs->vm.vs_realloc = vm->vs_realloc;
vs->vm.vs_free = vm->vs_free;
} else {
memset(&vs->vm, 0, sizeof (vs->vm));
}
return vs;
}
static vstring_inline void
vs_deinit(vstring *vs)
{
if ((vs->type & VS_TYPE_DYNAMIC) && vs->contents != NULL) {
if (vs->vm.vs_free) {
vs->vm.vs_free(vs->contents);
} else {
free(vs->contents);
}
}
if (vs->flags & VS_NEEDSFREE) {
memset(vs, 0, sizeof (*vs));
if (vs->vm.vs_free) {
vs->vm.vs_free(vs);
} else {
free(vs);
}
} else {
memset(vs, 0, sizeof (*vs));
}
}
static vstring_inline void
vs_rewind(vstring *vs)
{
vs->pointer = 0;
}
static vstring_inline void *
vs_resize(vstring *vs, size_t hint)
{
char *tmp;
if (vs->size == 0) {
if (VS_ALLOCSIZE < hint) {
vs->size = hint;
} else {
vs->size = VS_ALLOCSIZE;
}
if (vs->vm.vs_malloc) {
vs->contents = vs->vm.vs_malloc(vs->size);
} else {
vs->contents = calloc(1, vs->size);
}
if (vs->contents == NULL) {
return NULL;
}
} else {
size_t size = vs->size * 2;
if (size < hint) {
size = size + hint * 2;
}
if ((vs->type & VS_TYPE_GROWABLE)) {
/* Upgrade to a dynamic string. */
vs->type = VS_TYPE_DYNAMIC;
if (vs->vm.vs_malloc) {
tmp = vs->vm.vs_malloc(size);
} else {
tmp = calloc(1, size);
}
if (tmp == NULL) {
return NULL;
}
memcpy(tmp, vs->contents, vs->size);
vs->contents = tmp;
vs->size = size;
} else if ((vs->type & VS_TYPE_DYNAMIC)) {
if (vs->vm.vs_realloc) {
tmp = vs->vm.vs_realloc(vs->contents, size);
} else {
tmp = realloc(vs->contents, size);
}
if (tmp == NULL) {
return NULL;
}
vs->contents = tmp;
vs->size = size;
} else if ((vs->type & VS_TYPE_STATIC)) {
/*
* VS_TYPE_STATIC strings that do not also have
* VS_TYPE_GROWABLE set cannot be resized.
*/
return NULL;
}
}
/* This API is meant to return NULL on failure */
return vs->contents;
}
static vstring_inline bool
vs_push(vstring *vs, char c)
{
if (vs->pointer >= vs->size) {
if (vs_resize(vs, vs->size + 1) == NULL) {
return false;
}
}
vs->contents[vs->pointer++] = c;
return true;
}
static vstring_inline bool
vs_pushstr(vstring *vs, const char *s, uint64_t len)
{
assert(s != NULL);
if (len == 0) {
return false;
}
if (vs->pointer + len > vs->size) {
if (vs_resize(vs, vs->size + len) == NULL) {
return false;
}
}
memmove(&vs->contents[vs->pointer], s, len);
vs->pointer += len;
return true;
}
static vstring_inline bool
vs_pushuint(vstring *vs, uint64_t n)
{
char buf[20];
int s, e, l;
if (n != 0) {
s = e = 0;
/* Parse the number into the buffer in reverse order. */
while (n != 0) {
buf[e++] = '0' + (n % 10);
n /= 10;
}
/* Reverse the string in-place. */
for (l = --e; e > s; --e, ++s) {
buf[s] ^= buf[e];
buf[e] ^= buf[s];
buf[s] ^= buf[e];
}
return vs_pushstr(vs, buf, l + 1);
} else {
return vs_push(vs, '0');
}
}
static vstring_inline bool
vs_pushint(vstring *vs, int64_t n)
{
char buf[21];
int s, e, l;
int64_t t;
if (n != 0) {
s = e = 0;
t = n;
if (t < 0) {
n = -n;
}
while (n != 0) {
buf[e++] = '0' + (n % 10);
n /= 10;
}
if (t < 0) {
buf[e++] = '-';
}
/* Reverse the string in-place. */
for (l = --e; e > s; --e, ++s) {
buf[s] ^= buf[e];
buf[e] ^= buf[s];
buf[s] ^= buf[e];
}
return vs_pushstr(vs, buf, l + 1);
} else {
return vs_push(vs, '0');
}
}
static vstring_inline bool
vs_padint(vstring *vs, uint64_t n, int places)
{
char buf[20];
int s, e, l;
if (places > 19) {
return false;
}
s = e = 0;
while (n != 0) {
buf[e++] = '0' + (n % 10);
n /= 10;
if (e == places) {
break;
}
}
n = places - e;
while (n--) {
buf[e++] = '0';
}
for (l = --e; e > s; --e, ++s) {
buf[s] ^= buf[e];
buf[e] ^= buf[s];
buf[s] ^= buf[e];
}
return vs_pushstr(vs, buf, l + 1);
}
static vstring_inline bool
vs_pushdouble(vstring *vs, double n)
{
double modf_int, modf_frac;
int fpclass;
fpclass = fpclassify(n);
switch (fpclass) {
case FP_NAN:
return vs_pushstr(vs, "NaN", 3);
case FP_INFINITE:
if (n == INFINITY) {
return vs_pushstr(vs, "inf", 3);
}
return vs_pushstr(vs, "-inf", 4);
case FP_NORMAL:
case FP_ZERO:
if (signbit(n)) {
if (!vs_push(vs, '-')) {
return false;
}
}
modf_frac = modf(n, &modf_int);
return (vs_pushuint(vs, (uint64_t)modf_int) &&
vs_push(vs, '.') &&
vs_padint(vs, (uint64_t)(1e9*modf_frac), 9));
case FP_SUBNORMAL:
default:
return false;
}
}
static vstring_inline bool
vs_finalize(vstring *vs)
{
return vs_push(vs, '\0');
}
static vstring_inline char *
vs_contents(vstring *vs)
{
return vs->contents;
}
static vstring_inline uint64_t
vs_len(vstring *vs)
{
return vs->pointer;
}
#endif