1#include "snowball_runtime.h"
3#ifdef SNOWBALL_RUNTIME_THROW_EXCEPTIONS
6# define SNOWBALL_RETURN_OK return
7# define SNOWBALL_RETURN_OR_THROW(R, E) throw E
8# define SNOWBALL_PROPAGATE_ERR(F) F
10# define SNOWBALL_RETURN_OK return 0
11# define SNOWBALL_RETURN_OR_THROW(R, E) return R
12# define SNOWBALL_PROPAGATE_ERR(F) do { \
13 int snowball_err = F; \
14 if (snowball_err < 0) return snowball_err; \
32 if (p == NULL)
return;
47 if (
c >= limit)
return -1;
52 if (
b >= 0xC0 ||
b < 0x80)
break;
72 if (
c <= limit)
return -1;
91 if (b0 < 0xC0 ||
c == l) {
96 if (b0 < 0xE0 ||
c == l) {
97 *slot = (b0 & 0x1F) << 6 | b1;
101 if (b0 < 0xF0 ||
c == l) {
102 *slot = (b0 & 0xF) << 12 | b1 << 6 | b2;
105 *slot = (b0 & 0x7) << 18 | b1 << 12 | b2 << 6 | (p[
c] & 0x3F);
111 if (
c <= lb)
return 0;
113 if (
b < 0x80 ||
c == lb) {
119 if (
b >= 0xC0 ||
c == lb) {
120 *slot = (
b & 0x1F) << 6 |
a;
123 a |= (
b & 0x3F) << 6;
125 if (
b >= 0xE0 ||
c == lb) {
126 *slot = (
b & 0xF) << 12 |
a;
129 *slot = (p[--
c] & 0x7) << 18 | (
b & 0x3F) << 12 |
a;
138 if (ch > max || (ch -= min) < 0 || (s[ch >> 3] & (0X1 << (ch & 0X7))) == 0)
150 if (ch > max || (ch -= min) < 0 || (s[ch >> 3] & (0X1 << (ch & 0X7))) == 0)
162 if (!(ch > max || (ch -= min) < 0 || (s[ch >> 3] & (0X1 << (ch & 0X7))) == 0))
174 if (!(ch > max || (ch -= min) < 0 || (s[ch >> 3] & (0X1 << (ch & 0X7))) == 0))
186 if (z->
c >= z->
l)
return -1;
188 if (ch > max || (ch -= min) < 0 || (s[ch >> 3] & (0X1 << (ch & 0X7))) == 0)
198 if (z->
c <= z->
lb)
return -1;
200 if (ch > max || (ch -= min) < 0 || (s[ch >> 3] & (0X1 << (ch & 0X7))) == 0)
210 if (z->
c >= z->
l)
return -1;
212 if (!(ch > max || (ch -= min) < 0 || (s[ch >> 3] & (0X1 << (ch & 0X7))) == 0))
222 if (z->
c <= z->
lb)
return -1;
224 if (!(ch > max || (ch -= min) < 0 || (s[ch >> 3] & (0X1 << (ch & 0X7))) == 0))
232 if (z->
l - z->
c < s_size || memcmp(z->
p + z->
c, s, s_size *
sizeof(
symbol)) != 0)
return 0;
233 z->
c += s_size;
return 1;
237 if (z->
c - z->
lb < s_size || memcmp(z->
p + z->
c - s_size, s, s_size *
sizeof(
symbol)) != 0)
return 0;
238 z->
c -= s_size;
return 1;
250 int (*call_among_func)(
struct SN_env*)) {
255 int c = z->
c;
int l = z->
l;
258 const struct among * w;
263 int first_key_inspected = 0;
266 int k =
i + ((
j -
i) >> 1);
268 int common = common_i < common_j ? common_i : common_j;
271 int i2;
for (i2 = common; i2 < w->
s_size; i2++) {
272 if (
c + common == l) { diff = -1;
break; }
273 diff = q[common] - w->
s[i2];
274 if (diff != 0)
break;
293 if (first_key_inspected)
break;
294 first_key_inspected = 1;
299 if (common_i >= w->
s_size) {
303 if (call_among_func(z)) {
316 int (*call_among_func)(
struct SN_env*)) {
321 int c = z->
c;
int lb = z->
lb;
324 const struct among * w;
329 int first_key_inspected = 0;
332 int k =
i + ((
j -
i) >> 1);
334 int common = common_i < common_j ? common_i : common_j;
337 int i2;
for (i2 = w->
s_size - 1 - common; i2 >= 0; i2--) {
338 if (
c - common == lb) { diff = -1;
break; }
339 diff = q[- common] - w->
s[i2];
340 if (diff != 0)
break;
344 if (diff < 0) {
j = k; common_j = common; }
345 else {
i = k; common_i = common; }
349 if (first_key_inspected)
break;
350 first_key_inspected = 1;
355 if (common_i >= w->
s_size) {
359 if (call_among_func(z)) {
374 int new_size = n + 20;
378 if (mem == NULL)
return -1;
391 int adjustment =
s_size - (c_ket - c_bra);
392 if (adjustment != 0) {
397 memmove(z->
p + c_ket + adjustment,
404 else if (z->
c > c_bra)
411# define REPLACE_S(Z, B, K, SIZE, S) \
412 SNOWBALL_PROPAGATE_ERR(replace_s(Z, B, K, SIZE, S))
422 fprintf(stderr,
"faulty slice operation:\n");
430# define SLICE_CHECK(Z) SNOWBALL_PROPAGATE_ERR(slice_check(Z))
446 int slice_size = z->
ket - z->
bra;
447 if (slice_size != 0) {
449 memmove(z->
p + z->
bra,
456 else if (z->
c > z->
bra)
467 int adjustment =
s_size - (ket - bra);
468 z->
ket += adjustment;
469 if (bra <= z->bra) z->
bra += adjustment;
506 if (
b >= 0xC0 ||
b < 0x80) ++
len;
#define fprintf(file, fmt, msg)
Datum repeat(PG_FUNCTION_ARGS)
#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 *))