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