1#include "header.h"
2
3#define CREATE_SIZE 1
4
5extern symbol * create_s(void) {
6 symbol * p;
7 void * mem = malloc(HEAD + (CREATE_SIZE + 1) * sizeof(symbol));
8 if (mem == NULL) return NULL;
9 p = (symbol *) (HEAD + (char *) mem);
10 CAPACITY(p) = CREATE_SIZE;
11 SET_SIZE(p, 0);
12 return p;
13}
14
15extern void lose_s(symbol * p) {
16 if (p == NULL) return;
17 free((char *) p - HEAD);
18}
19
20/*
21 new_p = skip_utf8(p, c, lb, l, n); skips n characters forwards from p + c
22 if n +ve, or n characters backwards from p + c - 1 if n -ve. new_p is the new
23 position, or -1 on failure.
24
25 -- used to implement hop and next in the utf8 case.
26*/
27
28extern int skip_utf8(const symbol * p, int c, int lb, int l, int n) {
29 int b;
30 if (n >= 0) {
31 for (; n > 0; n--) {
32 if (c >= l) return -1;
33 b = p[c++];
34 if (b >= 0xC0) { /* 1100 0000 */
35 while (c < l) {
36 b = p[c];
37 if (b >= 0xC0 || b < 0x80) break;
38 /* break unless b is 10------ */
39 c++;
40 }
41 }
42 }
43 } else {
44 for (; n < 0; n++) {
45 if (c <= lb) return -1;
46 b = p[--c];
47 if (b >= 0x80) { /* 1000 0000 */
48 while (c > lb) {
49 b = p[c];
50 if (b >= 0xC0) break; /* 1100 0000 */
51 c--;
52 }
53 }
54 }
55 }
56 return c;
57}
58
59/* Code for character groupings: utf8 cases */
60
61static int get_utf8(const symbol * p, int c, int l, int * slot) {
62 int b0, b1;
63 if (c >= l) return 0;
64 b0 = p[c++];
65 if (b0 < 0xC0 || c == l) { /* 1100 0000 */
66 * slot = b0; return 1;
67 }
68 b1 = p[c++];
69 if (b0 < 0xE0 || c == l) { /* 1110 0000 */
70 * slot = (b0 & 0x1F) << 6 | (b1 & 0x3F); return 2;
71 }
72 * slot = (b0 & 0xF) << 12 | (b1 & 0x3F) << 6 | (p[c] & 0x3F); return 3;
73}
74
75static int get_b_utf8(const symbol * p, int c, int lb, int * slot) {
76 int b0, b1;
77 if (c <= lb) return 0;
78 b0 = p[--c];
79 if (b0 < 0x80 || c == lb) { /* 1000 0000 */
80 * slot = b0; return 1;
81 }
82 b1 = p[--c];
83 if (b1 >= 0xC0 || c == lb) { /* 1100 0000 */
84 * slot = (b1 & 0x1F) << 6 | (b0 & 0x3F); return 2;
85 }
86 * slot = (p[--c] & 0xF) << 12 | (b1 & 0x3F) << 6 | (b0 & 0x3F); return 3;
87}
88
89extern int in_grouping_U(struct SN_env * z, const unsigned char * s, int min, int max, int repeat) {
90 do {
91 int ch;
92 int w = get_utf8(z->p, z->c, z->l, & ch);
93 if (!w) return -1;
94 if (ch > max || (ch -= min) < 0 || (s[ch >> 3] & (0X1 << (ch & 0X7))) == 0)
95 return w;
96 z->c += w;
97 } while (repeat);
98 return 0;
99}
100
101extern int in_grouping_b_U(struct SN_env * z, const unsigned char * s, int min, int max, int repeat) {
102 do {
103 int ch;
104 int w = get_b_utf8(z->p, z->c, z->lb, & ch);
105 if (!w) return -1;
106 if (ch > max || (ch -= min) < 0 || (s[ch >> 3] & (0X1 << (ch & 0X7))) == 0)
107 return w;
108 z->c -= w;
109 } while (repeat);
110 return 0;
111}
112
113extern int out_grouping_U(struct SN_env * z, const unsigned char * s, int min, int max, int repeat) {
114 do {
115 int ch;
116 int w = get_utf8(z->p, z->c, z->l, & ch);
117 if (!w) return -1;
118 if (!(ch > max || (ch -= min) < 0 || (s[ch >> 3] & (0X1 << (ch & 0X7))) == 0))
119 return w;
120 z->c += w;
121 } while (repeat);
122 return 0;
123}
124
125extern int out_grouping_b_U(struct SN_env * z, const unsigned char * s, int min, int max, int repeat) {
126 do {
127 int ch;
128 int w = get_b_utf8(z->p, z->c, z->lb, & ch);
129 if (!w) return -1;
130 if (!(ch > max || (ch -= min) < 0 || (s[ch >> 3] & (0X1 << (ch & 0X7))) == 0))
131 return w;
132 z->c -= w;
133 } while (repeat);
134 return 0;
135}
136
137/* Code for character groupings: non-utf8 cases */
138
139extern int in_grouping(struct SN_env * z, const unsigned char * s, int min, int max, int repeat) {
140 do {
141 int ch;
142 if (z->c >= z->l) return -1;
143 ch = z->p[z->c];
144 if (ch > max || (ch -= min) < 0 || (s[ch >> 3] & (0X1 << (ch & 0X7))) == 0)
145 return 1;
146 z->c++;
147 } while (repeat);
148 return 0;
149}
150
151extern int in_grouping_b(struct SN_env * z, const unsigned char * s, int min, int max, int repeat) {
152 do {
153 int ch;
154 if (z->c <= z->lb) return -1;
155 ch = z->p[z->c - 1];
156 if (ch > max || (ch -= min) < 0 || (s[ch >> 3] & (0X1 << (ch & 0X7))) == 0)
157 return 1;
158 z->c--;
159 } while (repeat);
160 return 0;
161}
162
163extern int out_grouping(struct SN_env * z, const unsigned char * s, int min, int max, int repeat) {
164 do {
165 int ch;
166 if (z->c >= z->l) return -1;
167 ch = z->p[z->c];
168 if (!(ch > max || (ch -= min) < 0 || (s[ch >> 3] & (0X1 << (ch & 0X7))) == 0))
169 return 1;
170 z->c++;
171 } while (repeat);
172 return 0;
173}
174
175extern int out_grouping_b(struct SN_env * z, const unsigned char * s, int min, int max, int repeat) {
176 do {
177 int ch;
178 if (z->c <= z->lb) return -1;
179 ch = z->p[z->c - 1];
180 if (!(ch > max || (ch -= min) < 0 || (s[ch >> 3] & (0X1 << (ch & 0X7))) == 0))
181 return 1;
182 z->c--;
183 } while (repeat);
184 return 0;
185}
186
187extern int eq_s(struct SN_env * z, int s_size, const symbol * s) {
188 if (z->l - z->c < s_size || memcmp(z->p + z->c, s, s_size * sizeof(symbol)) != 0) return 0;
189 z->c += s_size; return 1;
190}
191
192extern int eq_s_b(struct SN_env * z, int s_size, const symbol * s) {
193 if (z->c - z->lb < s_size || memcmp(z->p + z->c - s_size, s, s_size * sizeof(symbol)) != 0) return 0;
194 z->c -= s_size; return 1;
195}
196
197extern int eq_v(struct SN_env * z, const symbol * p) {
198 return eq_s(z, SIZE(p), p);
199}
200
201extern int eq_v_b(struct SN_env * z, const symbol * p) {
202 return eq_s_b(z, SIZE(p), p);
203}
204
205extern int find_among(struct SN_env * z, const struct among * v, int v_size) {
206
207 int i = 0;
208 int j = v_size;
209
210 int c = z->c; int l = z->l;
211 symbol * q = z->p + c;
212
213 const struct among * w;
214
215 int common_i = 0;
216 int common_j = 0;
217
218 int first_key_inspected = 0;
219
220 while(1) {
221 int k = i + ((j - i) >> 1);
222 int diff = 0;
223 int common = common_i < common_j ? common_i : common_j; /* smaller */
224 w = v + k;
225 {
226 int i2; for (i2 = common; i2 < w->s_size; i2++) {
227 if (c + common == l) { diff = -1; break; }
228 diff = q[common] - w->s[i2];
229 if (diff != 0) break;
230 common++;
231 }
232 }
233 if (diff < 0) { j = k; common_j = common; }
234 else { i = k; common_i = common; }
235 if (j - i <= 1) {
236 if (i > 0) break; /* v->s has been inspected */
237 if (j == i) break; /* only one item in v */
238
239 /* - but now we need to go round once more to get
240 v->s inspected. This looks messy, but is actually
241 the optimal approach. */
242
243 if (first_key_inspected) break;
244 first_key_inspected = 1;
245 }
246 }
247 while(1) {
248 w = v + i;
249 if (common_i >= w->s_size) {
250 z->c = c + w->s_size;
251 if (w->function == 0) return w->result;
252 {
253 int res = w->function(z);
254 z->c = c + w->s_size;
255 if (res) return w->result;
256 }
257 }
258 i = w->substring_i;
259 if (i < 0) return 0;
260 }
261}
262
263/* find_among_b is for backwards processing. Same comments apply */
264
265extern int find_among_b(struct SN_env * z, const struct among * v, int v_size) {
266
267 int i = 0;
268 int j = v_size;
269
270 int c = z->c; int lb = z->lb;
271 symbol * q = z->p + c - 1;
272
273 const struct among * w;
274
275 int common_i = 0;
276 int common_j = 0;
277
278 int first_key_inspected = 0;
279
280 while(1) {
281 int k = i + ((j - i) >> 1);
282 int diff = 0;
283 int common = common_i < common_j ? common_i : common_j;
284 w = v + k;
285 {
286 int i2; for (i2 = w->s_size - 1 - common; i2 >= 0; i2--) {
287 if (c - common == lb) { diff = -1; break; }
288 diff = q[- common] - w->s[i2];
289 if (diff != 0) break;
290 common++;
291 }
292 }
293 if (diff < 0) { j = k; common_j = common; }
294 else { i = k; common_i = common; }
295 if (j - i <= 1) {
296 if (i > 0) break;
297 if (j == i) break;
298 if (first_key_inspected) break;
299 first_key_inspected = 1;
300 }
301 }
302 while(1) {
303 w = v + i;
304 if (common_i >= w->s_size) {
305 z->c = c - w->s_size;
306 if (w->function == 0) return w->result;
307 {
308 int res = w->function(z);
309 z->c = c - w->s_size;
310 if (res) return w->result;
311 }
312 }
313 i = w->substring_i;
314 if (i < 0) return 0;
315 }
316}
317
318
319/* Increase the size of the buffer pointed to by p to at least n symbols.
320 * If insufficient memory, returns NULL and frees the old buffer.
321 */
322static symbol * increase_size(symbol * p, int n) {
323 symbol * q;
324 int new_size = n + 20;
325 void * mem = realloc((char *) p - HEAD,
326 HEAD + (new_size + 1) * sizeof(symbol));
327 if (mem == NULL) {
328 lose_s(p);
329 return NULL;
330 }
331 q = (symbol *) (HEAD + (char *)mem);
332 CAPACITY(q) = new_size;
333 return q;
334}
335
336/* to replace symbols between c_bra and c_ket in z->p by the
337 s_size symbols at s.
338 Returns 0 on success, -1 on error.
339 Also, frees z->p (and sets it to NULL) on error.
340*/
341extern int replace_s(struct SN_env * z, int c_bra, int c_ket, int s_size, const symbol * s, int * adjptr)
342{
343 int adjustment;
344 int len;
345 if (z->p == NULL) {
346 z->p = create_s();
347 if (z->p == NULL) return -1;
348 }
349 adjustment = s_size - (c_ket - c_bra);
350 len = SIZE(z->p);
351 if (adjustment != 0) {
352 if (adjustment + len > CAPACITY(z->p)) {
353 z->p = increase_size(z->p, adjustment + len);
354 if (z->p == NULL) return -1;
355 }
356 memmove(z->p + c_ket + adjustment,
357 z->p + c_ket,
358 (len - c_ket) * sizeof(symbol));
359 SET_SIZE(z->p, adjustment + len);
360 z->l += adjustment;
361 if (z->c >= c_ket)
362 z->c += adjustment;
363 else
364 if (z->c > c_bra)
365 z->c = c_bra;
366 }
367 if (s_size) memmove(z->p + c_bra, s, s_size * sizeof(symbol));
368 if (adjptr != NULL)
369 *adjptr = adjustment;
370 return 0;
371}
372
373static int slice_check(struct SN_env * z) {
374
375 if (z->bra < 0 ||
376 z->bra > z->ket ||
377 z->ket > z->l ||
378 z->p == NULL ||
379 z->l > SIZE(z->p)) /* this line could be removed */
380 {
381#if 0
382 fprintf(stderr, "faulty slice operation:\n");
383 debug(z, -1, 0);
384#endif
385 return -1;
386 }
387 return 0;
388}
389
390extern int slice_from_s(struct SN_env * z, int s_size, const symbol * s) {
391 if (slice_check(z)) return -1;
392 return replace_s(z, z->bra, z->ket, s_size, s, NULL);
393}
394
395extern int slice_from_v(struct SN_env * z, const symbol * p) {
396 return slice_from_s(z, SIZE(p), p);
397}
398
399extern int slice_del(struct SN_env * z) {
400 return slice_from_s(z, 0, 0);
401}
402
403extern int insert_s(struct SN_env * z, int bra, int ket, int s_size, const symbol * s) {
404 int adjustment;
405 if (replace_s(z, bra, ket, s_size, s, &adjustment))
406 return -1;
407 if (bra <= z->bra) z->bra += adjustment;
408 if (bra <= z->ket) z->ket += adjustment;
409 return 0;
410}
411
412extern int insert_v(struct SN_env * z, int bra, int ket, const symbol * p) {
413 return insert_s(z, bra, ket, SIZE(p), p);
414}
415
416extern symbol * slice_to(struct SN_env * z, symbol * p) {
417 if (slice_check(z)) {
418 lose_s(p);
419 return NULL;
420 }
421 {
422 int len = z->ket - z->bra;
423 if (CAPACITY(p) < len) {
424 p = increase_size(p, len);
425 if (p == NULL)
426 return NULL;
427 }
428 memmove(p, z->p + z->bra, len * sizeof(symbol));
429 SET_SIZE(p, len);
430 }
431 return p;
432}
433
434extern symbol * assign_to(struct SN_env * z, symbol * p) {
435 int len = z->l;
436 if (CAPACITY(p) < len) {
437 p = increase_size(p, len);
438 if (p == NULL)
439 return NULL;
440 }
441 memmove(p, z->p, len * sizeof(symbol));
442 SET_SIZE(p, len);
443 return p;
444}
445
446extern int len_utf8(const symbol * p) {
447 int size = SIZE(p);
448 int len = 0;
449 while (size--) {
450 symbol b = *p++;
451 if (b >= 0xC0 || b < 0x80) ++len;
452 }
453 return len;
454}
455
456#if 0
457extern void debug(struct SN_env * z, int number, int line_count) {
458 int i;
459 int limit = SIZE(z->p);
460 /*if (number >= 0) printf("%3d (line %4d): '", number, line_count);*/
461 if (number >= 0) printf("%3d (line %4d): [%d]'", number, line_count,limit);
462 for (i = 0; i <= limit; i++) {
463 if (z->lb == i) printf("{");
464 if (z->bra == i) printf("[");
465 if (z->c == i) printf("|");
466 if (z->ket == i) printf("]");
467 if (z->l == i) printf("}");
468 if (i < limit)
469 { int ch = z->p[i];
470 if (ch == 0) ch = '#';
471 printf("%c", ch);
472 }
473 }
474 printf("'\n");
475}
476#endif
477