6 #define SNOWBALL_RUNTIME_THROW_EXCEPTIONS
10 #ifdef SNOWBALL_RUNTIME_THROW_EXCEPTIONS
13 # define SNOWBALL_RETURN_OK return
14 # define SNOWBALL_RETURN_OR_THROW(R, E) throw E
15 # define SNOWBALL_PROPAGATE_ERR(F) F
17 # define SNOWBALL_RETURN_OK return 0
18 # define SNOWBALL_RETURN_OR_THROW(R, E) return R
19 # define SNOWBALL_PROPAGATE_ERR(F) do { \
20 int snowball_err = F; \
21 if (snowball_err < 0) return snowball_err; \
39 if (
p == NULL)
return;
40 free((
char *)
p -
HEAD);
54 if (c >= limit)
return -1;
59 if (b >= 0xC0 || b < 0x80)
break;
79 if (c <= limit)
return -1;
98 if (b0 < 0xC0 || c == l) {
103 if (b0 < 0xE0 || c == l) {
104 *slot = (b0 & 0x1F) << 6 | b1;
108 if (b0 < 0xF0 || c == l) {
109 *slot = (b0 & 0xF) << 12 | b1 << 6 | b2;
112 *slot = (b0 & 0x7) << 18 | b1 << 12 | b2 << 6 | (
p[c] & 0x3F);
118 if (c <= lb)
return 0;
120 if (b < 0x80 || c == lb) {
126 if (b >= 0xC0 || c == lb) {
127 *slot = (b & 0x1F) << 6 | a;
130 a |= (b & 0x3F) << 6;
132 if (b >= 0xE0 || c == lb) {
133 *slot = (b & 0xF) << 12 | a;
136 *slot = (
p[--c] & 0x7) << 18 | (b & 0x3F) << 12 | a;
145 if (ch > max || (ch -= min) < 0 || (s[ch >> 3] & (0X1 << (ch & 0X7))) == 0)
157 if (ch > max || (ch -= min) < 0 || (s[ch >> 3] & (0X1 << (ch & 0X7))) == 0)
169 if (!(ch > max || (ch -= min) < 0 || (s[ch >> 3] & (0X1 << (ch & 0X7))) == 0))
181 if (!(ch > max || (ch -= min) < 0 || (s[ch >> 3] & (0X1 << (ch & 0X7))) == 0))
190 extern int in_grouping(
struct SN_env * z,
const unsigned char * s,
int min,
int max,
int repeat) {
193 if (z->
c >= z->
l)
return -1;
195 if (ch > max || (ch -= min) < 0 || (s[ch >> 3] & (0X1 << (ch & 0X7))) == 0)
205 if (z->
c <= z->
lb)
return -1;
207 if (ch > max || (ch -= min) < 0 || (s[ch >> 3] & (0X1 << (ch & 0X7))) == 0)
217 if (z->
c >= z->
l)
return -1;
219 if (!(ch > max || (ch -= min) < 0 || (s[ch >> 3] & (0X1 << (ch & 0X7))) == 0))
229 if (z->
c <= z->
lb)
return -1;
231 if (!(ch > max || (ch -= min) < 0 || (s[ch >> 3] & (0X1 << (ch & 0X7))) == 0))
239 if (z->
l - z->
c < s_size || memcmp(z->
p + z->
c, s, s_size *
sizeof(
symbol)) != 0)
return 0;
240 z->
c += s_size;
return 1;
244 if (z->
c - z->
lb < s_size || memcmp(z->
p + z->
c - s_size, s, s_size *
sizeof(
symbol)) != 0)
return 0;
245 z->
c -= s_size;
return 1;
257 int (*call_among_func)(
struct SN_env*)) {
262 int c = z->
c;
int l = z->
l;
265 const struct among * w;
270 int first_key_inspected = 0;
273 int k = i + ((j - i) >> 1);
275 int common = common_i < common_j ? common_i : common_j;
278 int i2;
for (i2 = common; i2 < w->
s_size; i2++) {
279 if (c + common == l) { diff = -1;
break; }
280 diff = q[common] - w->
s[i2];
281 if (diff != 0)
break;
300 if (first_key_inspected)
break;
301 first_key_inspected = 1;
306 if (common_i >= w->
s_size) {
310 if (call_among_func(z)) {
323 int (*call_among_func)(
struct SN_env*)) {
328 int c = z->
c;
int lb = z->
lb;
329 const symbol * q = z->
p + c - 1;
331 const struct among * w;
336 int first_key_inspected = 0;
339 int k = i + ((j - i) >> 1);
341 int common = common_i < common_j ? common_i : common_j;
344 int i2;
for (i2 = w->
s_size - 1 - common; i2 >= 0; i2--) {
345 if (c - common == lb) { diff = -1;
break; }
346 diff = q[- common] - w->
s[i2];
347 if (diff != 0)
break;
351 if (diff < 0) { j = k; common_j = common; }
352 else { i = k; common_i = common; }
356 if (first_key_inspected)
break;
357 first_key_inspected = 1;
362 if (common_i >= w->
s_size) {
366 if (call_among_func(z)) {
381 int new_size = n + 20;
382 void * mem = realloc((
char *) *
p -
HEAD,
385 if (mem == NULL)
return -1;
399 if (adjustment != 0) {
400 int len =
SIZE(z->
p);
404 memmove(z->
p +
c_ket + adjustment,
418 # define REPLACE_S(Z, B, K, SIZE, S) \
419 SNOWBALL_PROPAGATE_ERR(replace_s(Z, B, K, SIZE, S))
429 fprintf(stderr,
"faulty slice operation:\n");
437 # define SLICE_CHECK(Z) SNOWBALL_PROPAGATE_ERR(slice_check(Z))
453 int slice_size = z->
ket - z->
bra;
454 if (slice_size != 0) {
455 int len =
SIZE(z->
p);
456 memmove(z->
p + z->
bra,
463 else if (z->
c > z->
bra)
474 int adjustment =
s_size - (ket - bra);
475 z->
ket += adjustment;
476 if (bra <= z->bra) z->
bra += adjustment;
488 int len = z->
ket - z->
bra;
503 memmove(*
p, z->
p, len *
sizeof(
symbol));
513 if (
b >= 0xC0 ||
b < 0x80) ++len;
#define REPLACE_S(Z, B, K, SIZE, S)
#define SNOWBALL_RETURN_OR_THROW(R, E)
int out_grouping_U(struct SN_env *z, const unsigned char *s, int min, int max, int repeat)
int in_grouping_U(struct SN_env *z, const unsigned char *s, int min, int max, int repeat)
SNOWBALL_ERR insert_s(struct SN_env *z, int bra, int ket, int s_size, const symbol *s)
int eq_v_b(struct SN_env *z, const symbol *p)
SNOWBALL_ERR slice_from_s(struct SN_env *z, int s_size, const symbol *s)
int in_grouping(struct SN_env *z, const unsigned char *s, int min, int max, int repeat)
SNOWBALL_ERR slice_to(struct SN_env *z, symbol **p)
SNOWBALL_ERR assign_to(struct SN_env *z, symbol **p)
SNOWBALL_ERR slice_from_v(struct SN_env *z, const symbol *p)
int eq_s(struct SN_env *z, int s_size, const symbol *s)
SNOWBALL_ERR insert_v(struct SN_env *z, int bra, int ket, const symbol *p)
#define SNOWBALL_PROPAGATE_ERR(F)
static SNOWBALL_ERR slice_check(struct SN_env *z)
int eq_v(struct SN_env *z, const symbol *p)
SNOWBALL_ERR replace_s(struct SN_env *z, int c_bra, int c_ket, int s_size, const symbol *s)
static int get_utf8(const symbol *p, int c, int l, int *slot)
int out_grouping_b(struct SN_env *z, const unsigned char *s, int min, int max, int repeat)
int find_among_b(struct SN_env *z, const struct among *v, int v_size, int(*call_among_func)(struct SN_env *))
#define SNOWBALL_RETURN_OK
int in_grouping_b_U(struct SN_env *z, const unsigned char *s, int min, int max, int repeat)
int skip_b_utf8(const symbol *p, int c, int limit, int n)
int eq_s_b(struct SN_env *z, int s_size, const symbol *s)
int out_grouping(struct SN_env *z, const unsigned char *s, int min, int max, int repeat)
SNOWBALL_ERR slice_del(struct SN_env *z)
int out_grouping_b_U(struct SN_env *z, const unsigned char *s, int min, int max, int repeat)
int skip_utf8(const symbol *p, int c, int limit, int n)
int in_grouping_b(struct SN_env *z, const unsigned char *s, int min, int max, int repeat)
int len_utf8(const symbol *p)
static int get_b_utf8(const symbol *p, int c, int lb, int *slot)
static int increase_size(symbol **p, int n)
int find_among(struct SN_env *z, const struct among *v, int v_size, int(*call_among_func)(struct SN_env *))