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