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
|
#include "input_buffer.h"
#include <assert.h>
#include <string.h>
enum { KIT_IB_CACHE_SIZE = 256 };
static s32 kit_buf_adjust_(kit_input_buffer_t *buf, i64 size) {
assert(buf != NULL);
assert(size >= 0);
if (buf == NULL)
return KIT_ERROR_INTERNAL;
i64 offset = buf->data.size;
if (offset >= size)
return KIT_OK;
DA_RESIZE(buf->data, size);
if (buf->data.size != size)
return KIT_ERROR_OUT_OF_MEMORY;
str_t destination = { .size = size - offset,
.values = buf->data.values + offset };
i64 n = IS_READ(buf->upstream, destination);
DA_RESIZE(buf->data, offset + n);
if (buf->data.size != offset + n)
return KIT_ERROR_OUT_OF_MEMORY;
return KIT_OK;
}
kit_input_buffer_t kit_ib_init(is_handle_t upstream,
kit_allocator_t *alloc) {
kit_input_buffer_t buf;
memset(&buf, 0, sizeof buf);
buf.upstream = upstream;
DA_INIT(buf.data, 0, alloc);
return buf;
}
void kit_ib_destroy(kit_input_buffer_t *buf) {
assert(buf != NULL);
if (buf == NULL)
return;
DA_DESTROY(buf->data);
memset(buf, 0, sizeof *buf);
}
kit_ib_token_t kit_ib_token(kit_input_buffer_t *buf) {
return (kit_ib_token_t) {
.status = KIT_OK, .offset = 0, .size = 0, .buffer = buf
};
}
kit_str_t kit_ib_str(kit_ib_token_t tok) {
assert(tok.buffer != NULL);
if (tok.buffer == NULL || tok.buffer->data.values == NULL)
return (str_t) { .size = 0, .values = NULL };
return (str_t) { .size = tok.size,
.values = tok.buffer->data.values + tok.offset };
}
kit_ib_token_t kit_ib_read(kit_ib_token_t tok, i64 size) {
assert(tok.buffer != NULL);
kit_ib_token_t next = tok;
next.offset = tok.offset + tok.size;
next.size = 0;
if (tok.status != KIT_OK)
return next;
if (tok.buffer == NULL) {
next.status = KIT_ERROR_INVALID_ARGUMENT;
return next;
}
s32 s = kit_buf_adjust_(tok.buffer, tok.offset + tok.size + size);
if (s != KIT_OK) {
next.status = s;
return next;
}
assert(tok.buffer->data.values != NULL);
if (tok.buffer->data.values == NULL) {
next.status = KIT_ERROR_INTERNAL;
return next;
}
next.size = size < tok.buffer->data.size - tok.offset - tok.size
? size
: tok.buffer->data.size - tok.offset - tok.size;
return next;
}
#define IB_CACHE_INIT_(res_) \
str_builder_t cache_dynamic; \
memset(&cache_dynamic, 0, sizeof cache_dynamic); \
char cache_static[KIT_IB_CACHE_SIZE]; \
\
do { \
if (data.size > 0) { \
if (data.size < KIT_IB_CACHE_SIZE) { \
memcpy(cache_static, data.values, data.size); \
data.values = cache_static; \
} else { \
DA_INIT(cache_dynamic, data.size, tok.buffer->data.alloc); \
if (cache_dynamic.size != data.size) { \
(res_).status |= KIT_ERROR_OUT_OF_MEMORY; \
return (res_); \
} \
memcpy(cache_dynamic.values, data.values, data.size); \
data.values = cache_dynamic.values; \
} \
} \
} while (0)
#define IB_CACHE_CLEANUP_() \
do { \
if (cache_dynamic.values != NULL) \
DA_DESTROY(cache_dynamic); \
} while (0)
kit_ib_token_t kit_ib_any(kit_ib_token_t tok, str_t data) {
assert(tok.buffer != NULL);
kit_ib_token_t next = tok;
next.offset = tok.offset + tok.size;
next.size = 0;
if (tok.status != KIT_OK)
return next;
if (tok.buffer == NULL) {
next.status = KIT_ERROR_INVALID_ARGUMENT;
return next;
}
IB_CACHE_INIT_(next);
for (;; ++next.size) {
s32 s = kit_buf_adjust_(tok.buffer,
tok.offset + tok.size + next.size + 1);
if (s != KIT_OK) {
next.status = s;
return next;
}
assert(tok.buffer->data.values != NULL);
if (tok.buffer->data.values == NULL) {
next.status = KIT_ERROR_INTERNAL;
return next;
}
if (tok.offset + tok.size + next.size >= tok.buffer->data.size)
break;
i8 found = 0;
for (i64 i = 0; i < data.size; i++)
if (data.values[i] ==
tok.buffer->data.values[next.offset + next.size]) {
found = 1;
break;
}
if (!found)
break;
}
IB_CACHE_CLEANUP_();
return next;
}
kit_ib_token_t kit_ib_none(kit_ib_token_t tok, str_t data) {
assert(tok.buffer != NULL);
kit_ib_token_t next = tok;
next.offset = tok.offset + tok.size;
next.size = 0;
if (tok.status != KIT_OK)
return next;
if (tok.buffer == NULL) {
next.status = KIT_ERROR_INVALID_ARGUMENT;
return next;
}
IB_CACHE_INIT_(next);
for (;; ++next.size) {
s32 s = kit_buf_adjust_(tok.buffer,
tok.offset + tok.size + next.size + 1);
if (s != KIT_OK) {
next.status = s;
return next;
}
assert(tok.buffer->data.values != NULL);
if (tok.buffer->data.values == NULL) {
next.status = KIT_ERROR_INTERNAL;
return next;
}
if (tok.offset + tok.size + next.size >= tok.buffer->data.size)
break;
i8 found = 0;
for (i64 i = 0; i < data.size; i++)
if (data.values[i] ==
tok.buffer->data.values[next.offset + next.size]) {
found = 1;
break;
}
if (found)
break;
}
IB_CACHE_CLEANUP_();
return next;
}
kit_ib_token_t kit_ib_exact(kit_ib_token_t tok, str_t data) {
kit_ib_token_t res = tok;
res.offset = tok.offset + tok.size;
res.size = 0;
IB_CACHE_INIT_(res);
res = kit_ib_read(tok, data.size);
if (!AR_EQUAL(kit_ib_str(res), data))
res.status = KIT_PARSING_FAILED;
IB_CACHE_CLEANUP_();
return res;
}
kit_ib_token_t kit_ib_until(kit_ib_token_t tok, str_t data) {
assert(tok.buffer != NULL);
kit_ib_token_t next = tok;
next.offset = tok.offset + tok.size;
next.size = 0;
if (tok.status != KIT_OK)
return next;
if (tok.buffer == NULL) {
next.status = KIT_ERROR_INVALID_ARGUMENT;
return next;
}
IB_CACHE_INIT_(next);
for (;; ++next.size) {
s32 s = kit_buf_adjust_(tok.buffer,
tok.offset + tok.size + next.size + 1);
if (s != KIT_OK) {
next.status = s;
return next;
}
assert(tok.buffer->data.values != NULL);
if (tok.buffer->data.values == NULL) {
next.status = KIT_ERROR_INTERNAL;
return next;
}
if (tok.offset + tok.size + next.size >= tok.buffer->data.size)
break;
if (next.size + 1 >= data.size &&
AR_EQUAL(kit_str(data.size, tok.buffer->data.values +
(next.offset + next.size + 1 -
data.size)),
data)) {
next.size -= data.size - 1;
break;
}
}
IB_CACHE_CLEANUP_();
return next;
}
kit_ib_token_t kit_ib_while(kit_ib_token_t tok,
kit_ib_read_condition_fn condition,
void *context) {
assert(tok.buffer != NULL);
kit_ib_token_t next = tok;
next.offset = tok.offset + tok.size;
next.size = 0;
if (tok.status != KIT_OK)
return next;
if (tok.buffer == NULL) {
next.status = KIT_ERROR_INVALID_ARGUMENT;
return next;
}
for (;; ++next.size) {
s32 s = kit_buf_adjust_(tok.buffer,
tok.offset + tok.size + next.size + 1);
if (s != KIT_OK) {
next.status = s;
return next;
}
assert(tok.buffer->data.values != NULL);
if (tok.buffer->data.values == NULL) {
next.status = KIT_ERROR_INTERNAL;
return next;
}
if (tok.offset + tok.size + next.size >= tok.buffer->data.size)
break;
if (condition == NULL ||
!condition(kit_str(next.size + 1,
tok.buffer->data.values + next.offset),
context))
break;
}
return next;
}
|