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