isomorfeus-ferret 0.12.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/LICENSE +612 -0
- data/README.md +44 -0
- data/ext/isomorfeus_ferret_ext/benchmark.c +223 -0
- data/ext/isomorfeus_ferret_ext/benchmark.h +45 -0
- data/ext/isomorfeus_ferret_ext/benchmarks_all.h +25 -0
- data/ext/isomorfeus_ferret_ext/bm_bitvector.c +123 -0
- data/ext/isomorfeus_ferret_ext/bm_hash.c +118 -0
- data/ext/isomorfeus_ferret_ext/bm_micro_string.c +40 -0
- data/ext/isomorfeus_ferret_ext/bm_store.c +93 -0
- data/ext/isomorfeus_ferret_ext/email.rl +21 -0
- data/ext/isomorfeus_ferret_ext/extconf.rb +5 -0
- data/ext/isomorfeus_ferret_ext/fio_tmpfile.h +53 -0
- data/ext/isomorfeus_ferret_ext/frb_analysis.c +2577 -0
- data/ext/isomorfeus_ferret_ext/frb_index.c +3457 -0
- data/ext/isomorfeus_ferret_ext/frb_lang.c +9 -0
- data/ext/isomorfeus_ferret_ext/frb_lang.h +17 -0
- data/ext/isomorfeus_ferret_ext/frb_qparser.c +629 -0
- data/ext/isomorfeus_ferret_ext/frb_search.c +4460 -0
- data/ext/isomorfeus_ferret_ext/frb_store.c +515 -0
- data/ext/isomorfeus_ferret_ext/frb_threading.h +30 -0
- data/ext/isomorfeus_ferret_ext/frb_utils.c +1127 -0
- data/ext/isomorfeus_ferret_ext/frt_analysis.c +1644 -0
- data/ext/isomorfeus_ferret_ext/frt_analysis.h +247 -0
- data/ext/isomorfeus_ferret_ext/frt_array.c +124 -0
- data/ext/isomorfeus_ferret_ext/frt_array.h +54 -0
- data/ext/isomorfeus_ferret_ext/frt_bitvector.c +95 -0
- data/ext/isomorfeus_ferret_ext/frt_bitvector.h +586 -0
- data/ext/isomorfeus_ferret_ext/frt_compound_io.c +374 -0
- data/ext/isomorfeus_ferret_ext/frt_config.h +44 -0
- data/ext/isomorfeus_ferret_ext/frt_document.c +134 -0
- data/ext/isomorfeus_ferret_ext/frt_document.h +52 -0
- data/ext/isomorfeus_ferret_ext/frt_except.c +95 -0
- data/ext/isomorfeus_ferret_ext/frt_except.h +188 -0
- data/ext/isomorfeus_ferret_ext/frt_field_index.c +233 -0
- data/ext/isomorfeus_ferret_ext/frt_field_index.h +42 -0
- data/ext/isomorfeus_ferret_ext/frt_filter.c +157 -0
- data/ext/isomorfeus_ferret_ext/frt_fs_store.c +502 -0
- data/ext/isomorfeus_ferret_ext/frt_global.c +427 -0
- data/ext/isomorfeus_ferret_ext/frt_global.h +290 -0
- data/ext/isomorfeus_ferret_ext/frt_hash.c +518 -0
- data/ext/isomorfeus_ferret_ext/frt_hash.h +466 -0
- data/ext/isomorfeus_ferret_ext/frt_hashset.c +191 -0
- data/ext/isomorfeus_ferret_ext/frt_hashset.h +206 -0
- data/ext/isomorfeus_ferret_ext/frt_helper.c +62 -0
- data/ext/isomorfeus_ferret_ext/frt_helper.h +13 -0
- data/ext/isomorfeus_ferret_ext/frt_ind.c +353 -0
- data/ext/isomorfeus_ferret_ext/frt_ind.h +54 -0
- data/ext/isomorfeus_ferret_ext/frt_index.c +6377 -0
- data/ext/isomorfeus_ferret_ext/frt_index.h +880 -0
- data/ext/isomorfeus_ferret_ext/frt_lang.c +104 -0
- data/ext/isomorfeus_ferret_ext/frt_lang.h +44 -0
- data/ext/isomorfeus_ferret_ext/frt_mempool.c +87 -0
- data/ext/isomorfeus_ferret_ext/frt_mempool.h +33 -0
- data/ext/isomorfeus_ferret_ext/frt_multimapper.c +349 -0
- data/ext/isomorfeus_ferret_ext/frt_multimapper.h +52 -0
- data/ext/isomorfeus_ferret_ext/frt_posh.c +1006 -0
- data/ext/isomorfeus_ferret_ext/frt_posh.h +973 -0
- data/ext/isomorfeus_ferret_ext/frt_priorityqueue.c +147 -0
- data/ext/isomorfeus_ferret_ext/frt_priorityqueue.h +147 -0
- data/ext/isomorfeus_ferret_ext/frt_q_boolean.c +1612 -0
- data/ext/isomorfeus_ferret_ext/frt_q_const_score.c +157 -0
- data/ext/isomorfeus_ferret_ext/frt_q_filtered_query.c +209 -0
- data/ext/isomorfeus_ferret_ext/frt_q_fuzzy.c +281 -0
- data/ext/isomorfeus_ferret_ext/frt_q_match_all.c +147 -0
- data/ext/isomorfeus_ferret_ext/frt_q_multi_term.c +672 -0
- data/ext/isomorfeus_ferret_ext/frt_q_parser.c +3084 -0
- data/ext/isomorfeus_ferret_ext/frt_q_phrase.c +1182 -0
- data/ext/isomorfeus_ferret_ext/frt_q_prefix.c +98 -0
- data/ext/isomorfeus_ferret_ext/frt_q_range.c +665 -0
- data/ext/isomorfeus_ferret_ext/frt_q_span.c +2386 -0
- data/ext/isomorfeus_ferret_ext/frt_q_term.c +311 -0
- data/ext/isomorfeus_ferret_ext/frt_q_wildcard.c +166 -0
- data/ext/isomorfeus_ferret_ext/frt_ram_store.c +460 -0
- data/ext/isomorfeus_ferret_ext/frt_scanner.c +899 -0
- data/ext/isomorfeus_ferret_ext/frt_scanner.h +28 -0
- data/ext/isomorfeus_ferret_ext/frt_scanner_mb.c +6705 -0
- data/ext/isomorfeus_ferret_ext/frt_scanner_utf8.c +4419 -0
- data/ext/isomorfeus_ferret_ext/frt_search.c +1824 -0
- data/ext/isomorfeus_ferret_ext/frt_search.h +924 -0
- data/ext/isomorfeus_ferret_ext/frt_similarity.c +150 -0
- data/ext/isomorfeus_ferret_ext/frt_similarity.h +79 -0
- data/ext/isomorfeus_ferret_ext/frt_sort.c +796 -0
- data/ext/isomorfeus_ferret_ext/frt_stopwords.c +395 -0
- data/ext/isomorfeus_ferret_ext/frt_store.c +680 -0
- data/ext/isomorfeus_ferret_ext/frt_store.h +789 -0
- data/ext/isomorfeus_ferret_ext/frt_term_vectors.c +72 -0
- data/ext/isomorfeus_ferret_ext/frt_threading.h +23 -0
- data/ext/isomorfeus_ferret_ext/frt_win32.h +54 -0
- data/ext/isomorfeus_ferret_ext/isomorfeus_ferret.c +409 -0
- data/ext/isomorfeus_ferret_ext/isomorfeus_ferret.h +95 -0
- data/ext/isomorfeus_ferret_ext/libstemmer.c +93 -0
- data/ext/isomorfeus_ferret_ext/libstemmer.h +73 -0
- data/ext/isomorfeus_ferret_ext/q_parser.y +1366 -0
- data/ext/isomorfeus_ferret_ext/scanner.h +28 -0
- data/ext/isomorfeus_ferret_ext/scanner.in +43 -0
- data/ext/isomorfeus_ferret_ext/scanner.rl +84 -0
- data/ext/isomorfeus_ferret_ext/scanner_mb.rl +200 -0
- data/ext/isomorfeus_ferret_ext/scanner_utf8.rl +85 -0
- data/ext/isomorfeus_ferret_ext/stem_ISO_8859_1_danish.c +324 -0
- data/ext/isomorfeus_ferret_ext/stem_ISO_8859_1_danish.h +7 -0
- data/ext/isomorfeus_ferret_ext/stem_ISO_8859_1_dutch.c +610 -0
- data/ext/isomorfeus_ferret_ext/stem_ISO_8859_1_dutch.h +6 -0
- data/ext/isomorfeus_ferret_ext/stem_ISO_8859_1_english.c +1104 -0
- data/ext/isomorfeus_ferret_ext/stem_ISO_8859_1_english.h +6 -0
- data/ext/isomorfeus_ferret_ext/stem_ISO_8859_1_finnish.c +749 -0
- data/ext/isomorfeus_ferret_ext/stem_ISO_8859_1_finnish.h +7 -0
- data/ext/isomorfeus_ferret_ext/stem_ISO_8859_1_french.c +1233 -0
- data/ext/isomorfeus_ferret_ext/stem_ISO_8859_1_french.h +6 -0
- data/ext/isomorfeus_ferret_ext/stem_ISO_8859_1_german.c +490 -0
- data/ext/isomorfeus_ferret_ext/stem_ISO_8859_1_german.h +6 -0
- data/ext/isomorfeus_ferret_ext/stem_ISO_8859_1_hungarian.c +1217 -0
- data/ext/isomorfeus_ferret_ext/stem_ISO_8859_1_hungarian.h +7 -0
- data/ext/isomorfeus_ferret_ext/stem_ISO_8859_1_italian.c +1052 -0
- data/ext/isomorfeus_ferret_ext/stem_ISO_8859_1_italian.h +6 -0
- data/ext/isomorfeus_ferret_ext/stem_ISO_8859_1_norwegian.c +283 -0
- data/ext/isomorfeus_ferret_ext/stem_ISO_8859_1_norwegian.h +6 -0
- data/ext/isomorfeus_ferret_ext/stem_ISO_8859_1_porter.c +735 -0
- data/ext/isomorfeus_ferret_ext/stem_ISO_8859_1_porter.h +6 -0
- data/ext/isomorfeus_ferret_ext/stem_ISO_8859_1_portuguese.c +1003 -0
- data/ext/isomorfeus_ferret_ext/stem_ISO_8859_1_portuguese.h +7 -0
- data/ext/isomorfeus_ferret_ext/stem_ISO_8859_1_spanish.c +1079 -0
- data/ext/isomorfeus_ferret_ext/stem_ISO_8859_1_spanish.h +6 -0
- data/ext/isomorfeus_ferret_ext/stem_ISO_8859_1_swedish.c +293 -0
- data/ext/isomorfeus_ferret_ext/stem_ISO_8859_1_swedish.h +6 -0
- data/ext/isomorfeus_ferret_ext/stem_ISO_8859_2_romanian.c +984 -0
- data/ext/isomorfeus_ferret_ext/stem_ISO_8859_2_romanian.h +6 -0
- data/ext/isomorfeus_ferret_ext/stem_KOI8_R_russian.c +686 -0
- data/ext/isomorfeus_ferret_ext/stem_KOI8_R_russian.h +6 -0
- data/ext/isomorfeus_ferret_ext/stem_UTF_8_danish.c +325 -0
- data/ext/isomorfeus_ferret_ext/stem_UTF_8_danish.h +6 -0
- data/ext/isomorfeus_ferret_ext/stem_UTF_8_dutch.c +620 -0
- data/ext/isomorfeus_ferret_ext/stem_UTF_8_dutch.h +6 -0
- data/ext/isomorfeus_ferret_ext/stem_UTF_8_english.c +1111 -0
- data/ext/isomorfeus_ferret_ext/stem_UTF_8_english.h +6 -0
- data/ext/isomorfeus_ferret_ext/stem_UTF_8_finnish.c +754 -0
- data/ext/isomorfeus_ferret_ext/stem_UTF_8_finnish.h +6 -0
- data/ext/isomorfeus_ferret_ext/stem_UTF_8_french.c +1242 -0
- data/ext/isomorfeus_ferret_ext/stem_UTF_8_french.h +6 -0
- data/ext/isomorfeus_ferret_ext/stem_UTF_8_german.c +495 -0
- data/ext/isomorfeus_ferret_ext/stem_UTF_8_german.h +6 -0
- data/ext/isomorfeus_ferret_ext/stem_UTF_8_hungarian.c +1220 -0
- data/ext/isomorfeus_ferret_ext/stem_UTF_8_hungarian.h +6 -0
- data/ext/isomorfeus_ferret_ext/stem_UTF_8_italian.c +1059 -0
- data/ext/isomorfeus_ferret_ext/stem_UTF_8_italian.h +6 -0
- data/ext/isomorfeus_ferret_ext/stem_UTF_8_norwegian.c +285 -0
- data/ext/isomorfeus_ferret_ext/stem_UTF_8_norwegian.h +6 -0
- data/ext/isomorfeus_ferret_ext/stem_UTF_8_porter.c +741 -0
- data/ext/isomorfeus_ferret_ext/stem_UTF_8_porter.h +6 -0
- data/ext/isomorfeus_ferret_ext/stem_UTF_8_portuguese.c +1009 -0
- data/ext/isomorfeus_ferret_ext/stem_UTF_8_portuguese.h +6 -0
- data/ext/isomorfeus_ferret_ext/stem_UTF_8_romanian.c +990 -0
- data/ext/isomorfeus_ferret_ext/stem_UTF_8_romanian.h +6 -0
- data/ext/isomorfeus_ferret_ext/stem_UTF_8_russian.c +680 -0
- data/ext/isomorfeus_ferret_ext/stem_UTF_8_russian.h +6 -0
- data/ext/isomorfeus_ferret_ext/stem_UTF_8_spanish.c +1083 -0
- data/ext/isomorfeus_ferret_ext/stem_UTF_8_spanish.h +6 -0
- data/ext/isomorfeus_ferret_ext/stem_UTF_8_swedish.c +294 -0
- data/ext/isomorfeus_ferret_ext/stem_UTF_8_swedish.h +6 -0
- data/ext/isomorfeus_ferret_ext/stem_UTF_8_turkish.c +2191 -0
- data/ext/isomorfeus_ferret_ext/stem_UTF_8_turkish.h +6 -0
- data/ext/isomorfeus_ferret_ext/stem_api.c +66 -0
- data/ext/isomorfeus_ferret_ext/stem_api.h +26 -0
- data/ext/isomorfeus_ferret_ext/stem_header.h +57 -0
- data/ext/isomorfeus_ferret_ext/stem_modules.h +190 -0
- data/ext/isomorfeus_ferret_ext/stem_modules.txt +50 -0
- data/ext/isomorfeus_ferret_ext/stem_utilities.c +478 -0
- data/ext/isomorfeus_ferret_ext/test.c +850 -0
- data/ext/isomorfeus_ferret_ext/test.h +416 -0
- data/ext/isomorfeus_ferret_ext/test_1710.c +63 -0
- data/ext/isomorfeus_ferret_ext/test_analysis.c +1221 -0
- data/ext/isomorfeus_ferret_ext/test_array.c +272 -0
- data/ext/isomorfeus_ferret_ext/test_bitvector.c +600 -0
- data/ext/isomorfeus_ferret_ext/test_compound_io.c +170 -0
- data/ext/isomorfeus_ferret_ext/test_document.c +156 -0
- data/ext/isomorfeus_ferret_ext/test_except.c +244 -0
- data/ext/isomorfeus_ferret_ext/test_fields.c +522 -0
- data/ext/isomorfeus_ferret_ext/test_file_deleter.c +185 -0
- data/ext/isomorfeus_ferret_ext/test_filter.c +331 -0
- data/ext/isomorfeus_ferret_ext/test_fs_store.c +25 -0
- data/ext/isomorfeus_ferret_ext/test_global.c +299 -0
- data/ext/isomorfeus_ferret_ext/test_hash.c +485 -0
- data/ext/isomorfeus_ferret_ext/test_hashset.c +288 -0
- data/ext/isomorfeus_ferret_ext/test_helper.c +47 -0
- data/ext/isomorfeus_ferret_ext/test_highlighter.c +548 -0
- data/ext/isomorfeus_ferret_ext/test_index.c +2323 -0
- data/ext/isomorfeus_ferret_ext/test_lang.c +74 -0
- data/ext/isomorfeus_ferret_ext/test_mempool.c +102 -0
- data/ext/isomorfeus_ferret_ext/test_multimapper.c +64 -0
- data/ext/isomorfeus_ferret_ext/test_priorityqueue.c +213 -0
- data/ext/isomorfeus_ferret_ext/test_q_const_score.c +84 -0
- data/ext/isomorfeus_ferret_ext/test_q_filtered.c +61 -0
- data/ext/isomorfeus_ferret_ext/test_q_fuzzy.c +241 -0
- data/ext/isomorfeus_ferret_ext/test_q_parser.c +464 -0
- data/ext/isomorfeus_ferret_ext/test_q_span.c +575 -0
- data/ext/isomorfeus_ferret_ext/test_ram_store.c +77 -0
- data/ext/isomorfeus_ferret_ext/test_search.c +1874 -0
- data/ext/isomorfeus_ferret_ext/test_segments.c +167 -0
- data/ext/isomorfeus_ferret_ext/test_similarity.c +25 -0
- data/ext/isomorfeus_ferret_ext/test_sort.c +333 -0
- data/ext/isomorfeus_ferret_ext/test_store.c +591 -0
- data/ext/isomorfeus_ferret_ext/test_store.h +3 -0
- data/ext/isomorfeus_ferret_ext/test_term.c +351 -0
- data/ext/isomorfeus_ferret_ext/test_term_vectors.c +373 -0
- data/ext/isomorfeus_ferret_ext/test_test.c +83 -0
- data/ext/isomorfeus_ferret_ext/test_threading.c +188 -0
- data/ext/isomorfeus_ferret_ext/testhelper.c +561 -0
- data/ext/isomorfeus_ferret_ext/testhelper.h +25 -0
- data/ext/isomorfeus_ferret_ext/tests_all.h +87 -0
- data/ext/isomorfeus_ferret_ext/uchar-ucs4.rl +1854 -0
- data/ext/isomorfeus_ferret_ext/uchar-utf8.rl +1999 -0
- data/ext/isomorfeus_ferret_ext/url.rl +27 -0
- data/ext/isomorfeus_ferret_ext/word_list.h +15156 -0
- data/lib/isomorfeus/ferret/document.rb +132 -0
- data/lib/isomorfeus/ferret/field_symbol.rb +85 -0
- data/lib/isomorfeus/ferret/index/field_infos.rb +48 -0
- data/lib/isomorfeus/ferret/index/index.rb +970 -0
- data/lib/isomorfeus/ferret/monitor.rb +323 -0
- data/lib/isomorfeus/ferret/stdlib_patches.rb +151 -0
- data/lib/isomorfeus/ferret/version.rb +5 -0
- data/lib/isomorfeus-ferret.rb +8 -0
- metadata +307 -0
@@ -0,0 +1,478 @@
|
|
1
|
+
|
2
|
+
#include <stdio.h>
|
3
|
+
#include <stdlib.h>
|
4
|
+
#include <string.h>
|
5
|
+
|
6
|
+
#include "stem_header.h"
|
7
|
+
|
8
|
+
#define unless(C) if(!(C))
|
9
|
+
|
10
|
+
#define CREATE_SIZE 1
|
11
|
+
|
12
|
+
symbol * create_s(void) {
|
13
|
+
symbol * p;
|
14
|
+
void * mem = malloc(HEAD + (CREATE_SIZE + 1) * sizeof(symbol));
|
15
|
+
if (mem == NULL) return NULL;
|
16
|
+
p = (symbol *) (HEAD + (char *) mem);
|
17
|
+
CAPACITY(p) = CREATE_SIZE;
|
18
|
+
SET_SIZE(p, CREATE_SIZE);
|
19
|
+
return p;
|
20
|
+
}
|
21
|
+
|
22
|
+
void lose_s(symbol * p) {
|
23
|
+
if (p == NULL) return;
|
24
|
+
free((char *) p - HEAD);
|
25
|
+
}
|
26
|
+
|
27
|
+
/*
|
28
|
+
new_p = skip_utf8(p, c, lb, l, n); skips n characters forwards from p + c
|
29
|
+
if n +ve, or n characters backwards from p + c - 1 if n -ve. new_p is the new
|
30
|
+
position, or 0 on failure.
|
31
|
+
|
32
|
+
-- used to implement hop and next in the utf8 case.
|
33
|
+
*/
|
34
|
+
|
35
|
+
int skip_utf8(const symbol * p, int c, int lb, int l, int n) {
|
36
|
+
int b;
|
37
|
+
if (n >= 0) {
|
38
|
+
for (; n > 0; n--) {
|
39
|
+
if (c >= l) return -1;
|
40
|
+
b = p[c++];
|
41
|
+
if (b >= 0xC0) { /* 1100 0000 */
|
42
|
+
while (c < l) {
|
43
|
+
b = p[c];
|
44
|
+
if (b >= 0xC0 || b < 0x80) break;
|
45
|
+
/* break unless b is 10------ */
|
46
|
+
c++;
|
47
|
+
}
|
48
|
+
}
|
49
|
+
}
|
50
|
+
} else {
|
51
|
+
for (; n < 0; n++) {
|
52
|
+
if (c <= lb) return -1;
|
53
|
+
b = p[--c];
|
54
|
+
if (b >= 0x80) { /* 1000 0000 */
|
55
|
+
while (c > lb) {
|
56
|
+
b = p[c];
|
57
|
+
if (b >= 0xC0) break; /* 1100 0000 */
|
58
|
+
c--;
|
59
|
+
}
|
60
|
+
}
|
61
|
+
}
|
62
|
+
}
|
63
|
+
return c;
|
64
|
+
}
|
65
|
+
|
66
|
+
/* Code for character groupings: utf8 cases */
|
67
|
+
|
68
|
+
static int get_utf8(const symbol * p, int c, int l, int * slot) {
|
69
|
+
int b0, b1;
|
70
|
+
if (c >= l) return 0;
|
71
|
+
b0 = p[c++];
|
72
|
+
if (b0 < 0xC0 || c == l) { /* 1100 0000 */
|
73
|
+
* slot = b0; return 1;
|
74
|
+
}
|
75
|
+
b1 = p[c++];
|
76
|
+
if (b0 < 0xE0 || c == l) { /* 1110 0000 */
|
77
|
+
* slot = (b0 & 0x1F) << 6 | (b1 & 0x3F); return 2;
|
78
|
+
}
|
79
|
+
* slot = (b0 & 0xF) << 12 | (b1 & 0x3F) << 6 | (p[c] & 0x3F); return 3;
|
80
|
+
}
|
81
|
+
|
82
|
+
static int get_b_utf8(const symbol * p, int c, int lb, int * slot) {
|
83
|
+
int b0, b1;
|
84
|
+
if (c <= lb) return 0;
|
85
|
+
b0 = p[--c];
|
86
|
+
if (b0 < 0x80 || c == lb) { /* 1000 0000 */
|
87
|
+
* slot = b0; return 1;
|
88
|
+
}
|
89
|
+
b1 = p[--c];
|
90
|
+
if (b1 >= 0xC0 || c == lb) { /* 1100 0000 */
|
91
|
+
* slot = (b1 & 0x1F) << 6 | (b0 & 0x3F); return 2;
|
92
|
+
}
|
93
|
+
* slot = (p[c] & 0xF) << 12 | (b1 & 0x3F) << 6 | (b0 & 0x3F); return 3;
|
94
|
+
}
|
95
|
+
|
96
|
+
int in_grouping_U(struct SN_env * z, const unsigned char * s, int min, int max, int repeat) {
|
97
|
+
do {
|
98
|
+
int ch;
|
99
|
+
int w = get_utf8(z->p, z->c, z->l, & ch);
|
100
|
+
unless (w) return -1;
|
101
|
+
if (ch > max || (ch -= min) < 0 || (s[ch >> 3] & (0X1 << (ch & 0X7))) == 0)
|
102
|
+
return w;
|
103
|
+
z->c += w;
|
104
|
+
} while (repeat);
|
105
|
+
return 0;
|
106
|
+
}
|
107
|
+
|
108
|
+
int in_grouping_b_U(struct SN_env * z, const unsigned char * s, int min, int max, int repeat) {
|
109
|
+
do {
|
110
|
+
int ch;
|
111
|
+
int w = get_b_utf8(z->p, z->c, z->lb, & ch);
|
112
|
+
unless (w) return -1;
|
113
|
+
if (ch > max || (ch -= min) < 0 || (s[ch >> 3] & (0X1 << (ch & 0X7))) == 0)
|
114
|
+
return w;
|
115
|
+
z->c -= w;
|
116
|
+
} while (repeat);
|
117
|
+
return 0;
|
118
|
+
}
|
119
|
+
|
120
|
+
int out_grouping_U(struct SN_env * z, const unsigned char * s, int min, int max, int repeat) {
|
121
|
+
do {
|
122
|
+
int ch;
|
123
|
+
int w = get_utf8(z->p, z->c, z->l, & ch);
|
124
|
+
unless (w) return -1;
|
125
|
+
unless (ch > max || (ch -= min) < 0 || (s[ch >> 3] & (0X1 << (ch & 0X7))) == 0)
|
126
|
+
return w;
|
127
|
+
z->c += w;
|
128
|
+
} while (repeat);
|
129
|
+
return 0;
|
130
|
+
}
|
131
|
+
|
132
|
+
int out_grouping_b_U(struct SN_env * z, const unsigned char * s, int min, int max, int repeat) {
|
133
|
+
do {
|
134
|
+
int ch;
|
135
|
+
int w = get_b_utf8(z->p, z->c, z->lb, & ch);
|
136
|
+
unless (w) return -1;
|
137
|
+
unless (ch > max || (ch -= min) < 0 || (s[ch >> 3] & (0X1 << (ch & 0X7))) == 0)
|
138
|
+
return w;
|
139
|
+
z->c -= w;
|
140
|
+
} while (repeat);
|
141
|
+
return 0;
|
142
|
+
}
|
143
|
+
|
144
|
+
/* Code for character groupings: non-utf8 cases */
|
145
|
+
|
146
|
+
int in_grouping(struct SN_env * z, const unsigned char * s, int min, int max, int repeat) {
|
147
|
+
do {
|
148
|
+
int ch;
|
149
|
+
if (z->c >= z->l) return -1;
|
150
|
+
ch = z->p[z->c];
|
151
|
+
if (ch > max || (ch -= min) < 0 || (s[ch >> 3] & (0X1 << (ch & 0X7))) == 0)
|
152
|
+
return 1;
|
153
|
+
z->c++;
|
154
|
+
} while (repeat);
|
155
|
+
return 0;
|
156
|
+
}
|
157
|
+
|
158
|
+
int in_grouping_b(struct SN_env * z, const unsigned char * s, int min, int max, int repeat) {
|
159
|
+
do {
|
160
|
+
int ch;
|
161
|
+
if (z->c <= z->lb) return -1;
|
162
|
+
ch = z->p[z->c - 1];
|
163
|
+
if (ch > max || (ch -= min) < 0 || (s[ch >> 3] & (0X1 << (ch & 0X7))) == 0)
|
164
|
+
return 1;
|
165
|
+
z->c--;
|
166
|
+
} while (repeat);
|
167
|
+
return 0;
|
168
|
+
}
|
169
|
+
|
170
|
+
int out_grouping(struct SN_env * z, const unsigned char * s, int min, int max, int repeat) {
|
171
|
+
do {
|
172
|
+
int ch;
|
173
|
+
if (z->c >= z->l) return -1;
|
174
|
+
ch = z->p[z->c];
|
175
|
+
unless (ch > max || (ch -= min) < 0 || (s[ch >> 3] & (0X1 << (ch & 0X7))) == 0)
|
176
|
+
return 1;
|
177
|
+
z->c++;
|
178
|
+
} while (repeat);
|
179
|
+
return 0;
|
180
|
+
}
|
181
|
+
|
182
|
+
int out_grouping_b(struct SN_env * z, const unsigned char * s, int min, int max, int repeat) {
|
183
|
+
do {
|
184
|
+
int ch;
|
185
|
+
if (z->c <= z->lb) return -1;
|
186
|
+
ch = z->p[z->c - 1];
|
187
|
+
unless (ch > max || (ch -= min) < 0 || (s[ch >> 3] & (0X1 << (ch & 0X7))) == 0)
|
188
|
+
return 1;
|
189
|
+
z->c--;
|
190
|
+
} while (repeat);
|
191
|
+
return 0;
|
192
|
+
}
|
193
|
+
|
194
|
+
int eq_s(struct SN_env * z, int s_size, const symbol * s) {
|
195
|
+
if (z->l - z->c < s_size || memcmp(z->p + z->c, s, s_size * sizeof(symbol)) != 0) return 0;
|
196
|
+
z->c += s_size; return 1;
|
197
|
+
}
|
198
|
+
|
199
|
+
int eq_s_b(struct SN_env * z, int s_size, const symbol * s) {
|
200
|
+
if (z->c - z->lb < s_size || memcmp(z->p + z->c - s_size, s, s_size * sizeof(symbol)) != 0) return 0;
|
201
|
+
z->c -= s_size; return 1;
|
202
|
+
}
|
203
|
+
|
204
|
+
int eq_v(struct SN_env * z, const symbol * p) {
|
205
|
+
return eq_s(z, SIZE(p), p);
|
206
|
+
}
|
207
|
+
|
208
|
+
int eq_v_b(struct SN_env * z, const symbol * p) {
|
209
|
+
return eq_s_b(z, SIZE(p), p);
|
210
|
+
}
|
211
|
+
|
212
|
+
int find_among(struct SN_env * z, const struct among * v, int v_size) {
|
213
|
+
|
214
|
+
int i = 0;
|
215
|
+
int j = v_size;
|
216
|
+
|
217
|
+
int c = z->c; int l = z->l;
|
218
|
+
symbol * q = z->p + c;
|
219
|
+
|
220
|
+
const struct among * w;
|
221
|
+
|
222
|
+
int common_i = 0;
|
223
|
+
int common_j = 0;
|
224
|
+
|
225
|
+
int first_key_inspected = 0;
|
226
|
+
|
227
|
+
while(1) {
|
228
|
+
int k = i + ((j - i) >> 1);
|
229
|
+
int diff = 0;
|
230
|
+
int common = common_i < common_j ? common_i : common_j; /* smaller */
|
231
|
+
w = v + k;
|
232
|
+
{
|
233
|
+
int i2; for (i2 = common; i2 < w->s_size; i2++) {
|
234
|
+
if (c + common == l) { diff = -1; break; }
|
235
|
+
diff = q[common] - w->s[i2];
|
236
|
+
if (diff != 0) break;
|
237
|
+
common++;
|
238
|
+
}
|
239
|
+
}
|
240
|
+
if (diff < 0) { j = k; common_j = common; }
|
241
|
+
else { i = k; common_i = common; }
|
242
|
+
if (j - i <= 1) {
|
243
|
+
if (i > 0) break; /* v->s has been inspected */
|
244
|
+
if (j == i) break; /* only one item in v */
|
245
|
+
|
246
|
+
/* - but now we need to go round once more to get
|
247
|
+
v->s inspected. This looks messy, but is actually
|
248
|
+
the optimal approach. */
|
249
|
+
|
250
|
+
if (first_key_inspected) break;
|
251
|
+
first_key_inspected = 1;
|
252
|
+
}
|
253
|
+
}
|
254
|
+
while(1) {
|
255
|
+
w = v + i;
|
256
|
+
if (common_i >= w->s_size) {
|
257
|
+
z->c = c + w->s_size;
|
258
|
+
if (w->function == 0) return w->result;
|
259
|
+
{
|
260
|
+
int res = w->function(z);
|
261
|
+
z->c = c + w->s_size;
|
262
|
+
if (res) return w->result;
|
263
|
+
}
|
264
|
+
}
|
265
|
+
i = w->substring_i;
|
266
|
+
if (i < 0) return 0;
|
267
|
+
}
|
268
|
+
}
|
269
|
+
|
270
|
+
/* find_among_b is for backwards processing. Same comments apply */
|
271
|
+
|
272
|
+
int find_among_b(struct SN_env * z, const struct among * v, int v_size) {
|
273
|
+
|
274
|
+
int i = 0;
|
275
|
+
int j = v_size;
|
276
|
+
|
277
|
+
int c = z->c; int lb = z->lb;
|
278
|
+
symbol * q = z->p + c - 1;
|
279
|
+
|
280
|
+
const struct among * w;
|
281
|
+
|
282
|
+
int common_i = 0;
|
283
|
+
int common_j = 0;
|
284
|
+
|
285
|
+
int first_key_inspected = 0;
|
286
|
+
|
287
|
+
while(1) {
|
288
|
+
int k = i + ((j - i) >> 1);
|
289
|
+
int diff = 0;
|
290
|
+
int common = common_i < common_j ? common_i : common_j;
|
291
|
+
w = v + k;
|
292
|
+
{
|
293
|
+
int i2; for (i2 = w->s_size - 1 - common; i2 >= 0; i2--) {
|
294
|
+
if (c - common == lb) { diff = -1; break; }
|
295
|
+
diff = q[- common] - w->s[i2];
|
296
|
+
if (diff != 0) break;
|
297
|
+
common++;
|
298
|
+
}
|
299
|
+
}
|
300
|
+
if (diff < 0) { j = k; common_j = common; }
|
301
|
+
else { i = k; common_i = common; }
|
302
|
+
if (j - i <= 1) {
|
303
|
+
if (i > 0) break;
|
304
|
+
if (j == i) break;
|
305
|
+
if (first_key_inspected) break;
|
306
|
+
first_key_inspected = 1;
|
307
|
+
}
|
308
|
+
}
|
309
|
+
while(1) {
|
310
|
+
w = v + i;
|
311
|
+
if (common_i >= w->s_size) {
|
312
|
+
z->c = c - w->s_size;
|
313
|
+
if (w->function == 0) return w->result;
|
314
|
+
{
|
315
|
+
int res = w->function(z);
|
316
|
+
z->c = c - w->s_size;
|
317
|
+
if (res) return w->result;
|
318
|
+
}
|
319
|
+
}
|
320
|
+
i = w->substring_i;
|
321
|
+
if (i < 0) return 0;
|
322
|
+
}
|
323
|
+
}
|
324
|
+
|
325
|
+
|
326
|
+
/* Increase the size of the buffer pointed to by p to at least n symbols.
|
327
|
+
* If insufficient memory, returns NULL and frees the old buffer.
|
328
|
+
*/
|
329
|
+
static symbol * increase_size(symbol * p, int n) {
|
330
|
+
symbol * q;
|
331
|
+
int new_size = n + 20;
|
332
|
+
void * mem = realloc((char *) p - HEAD,
|
333
|
+
HEAD + (new_size + 1) * sizeof(symbol));
|
334
|
+
if (mem == NULL) {
|
335
|
+
lose_s(p);
|
336
|
+
return NULL;
|
337
|
+
}
|
338
|
+
q = (symbol *) (HEAD + (char *)mem);
|
339
|
+
CAPACITY(q) = new_size;
|
340
|
+
return q;
|
341
|
+
}
|
342
|
+
|
343
|
+
/* to replace symbols between c_bra and c_ket in z->p by the
|
344
|
+
s_size symbols at s.
|
345
|
+
Returns 0 on success, -1 on error.
|
346
|
+
Also, frees z->p (and sets it to NULL) on error.
|
347
|
+
*/
|
348
|
+
int replace_s(struct SN_env * z, int c_bra, int c_ket, int s_size, const symbol * s, int * adjptr)
|
349
|
+
{
|
350
|
+
int adjustment;
|
351
|
+
int len;
|
352
|
+
if (z->p == NULL) {
|
353
|
+
z->p = create_s();
|
354
|
+
if (z->p == NULL) return -1;
|
355
|
+
}
|
356
|
+
adjustment = s_size - (c_ket - c_bra);
|
357
|
+
len = SIZE(z->p);
|
358
|
+
if (adjustment != 0) {
|
359
|
+
if (adjustment + len > CAPACITY(z->p)) {
|
360
|
+
z->p = increase_size(z->p, adjustment + len);
|
361
|
+
if (z->p == NULL) return -1;
|
362
|
+
}
|
363
|
+
memmove(z->p + c_ket + adjustment,
|
364
|
+
z->p + c_ket,
|
365
|
+
(len - c_ket) * sizeof(symbol));
|
366
|
+
SET_SIZE(z->p, adjustment + len);
|
367
|
+
z->l += adjustment;
|
368
|
+
if (z->c >= c_ket)
|
369
|
+
z->c += adjustment;
|
370
|
+
else
|
371
|
+
if (z->c > c_bra)
|
372
|
+
z->c = c_bra;
|
373
|
+
}
|
374
|
+
unless (s_size == 0) memmove(z->p + c_bra, s, s_size * sizeof(symbol));
|
375
|
+
if (adjptr != NULL)
|
376
|
+
*adjptr = adjustment;
|
377
|
+
return 0;
|
378
|
+
}
|
379
|
+
|
380
|
+
static int slice_check(struct SN_env * z) {
|
381
|
+
|
382
|
+
if (z->bra < 0 ||
|
383
|
+
z->bra > z->ket ||
|
384
|
+
z->ket > z->l ||
|
385
|
+
z->p == NULL ||
|
386
|
+
z->l > SIZE(z->p)) /* this line could be removed */
|
387
|
+
{
|
388
|
+
#if 0
|
389
|
+
fprintf(stderr, "faulty slice operation:\n");
|
390
|
+
debug(z, -1, 0);
|
391
|
+
#endif
|
392
|
+
return -1;
|
393
|
+
}
|
394
|
+
return 0;
|
395
|
+
}
|
396
|
+
|
397
|
+
int slice_from_s(struct SN_env * z, int s_size, const symbol * s) {
|
398
|
+
if (slice_check(z)) return -1;
|
399
|
+
return replace_s(z, z->bra, z->ket, s_size, s, NULL);
|
400
|
+
}
|
401
|
+
|
402
|
+
int slice_from_v(struct SN_env * z, const symbol * p) {
|
403
|
+
return slice_from_s(z, SIZE(p), p);
|
404
|
+
}
|
405
|
+
|
406
|
+
int slice_del(struct SN_env * z) {
|
407
|
+
return slice_from_s(z, 0, 0);
|
408
|
+
}
|
409
|
+
|
410
|
+
int insert_s(struct SN_env * z, int bra, int ket, int s_size, const symbol * s) {
|
411
|
+
int adjustment;
|
412
|
+
if (replace_s(z, bra, ket, s_size, s, &adjustment))
|
413
|
+
return -1;
|
414
|
+
if (bra <= z->bra) z->bra += adjustment;
|
415
|
+
if (bra <= z->ket) z->ket += adjustment;
|
416
|
+
return 0;
|
417
|
+
}
|
418
|
+
|
419
|
+
int insert_v(struct SN_env * z, int bra, int ket, const symbol * p) {
|
420
|
+
int adjustment;
|
421
|
+
if (replace_s(z, bra, ket, SIZE(p), p, &adjustment))
|
422
|
+
return -1;
|
423
|
+
if (bra <= z->bra) z->bra += adjustment;
|
424
|
+
if (bra <= z->ket) z->ket += adjustment;
|
425
|
+
return 0;
|
426
|
+
}
|
427
|
+
|
428
|
+
symbol * slice_to(struct SN_env * z, symbol * p) {
|
429
|
+
if (slice_check(z)) {
|
430
|
+
lose_s(p);
|
431
|
+
return NULL;
|
432
|
+
}
|
433
|
+
{
|
434
|
+
int len = z->ket - z->bra;
|
435
|
+
if (CAPACITY(p) < len) {
|
436
|
+
p = increase_size(p, len);
|
437
|
+
if (p == NULL)
|
438
|
+
return NULL;
|
439
|
+
}
|
440
|
+
memmove(p, z->p + z->bra, len * sizeof(symbol));
|
441
|
+
SET_SIZE(p, len);
|
442
|
+
}
|
443
|
+
return p;
|
444
|
+
}
|
445
|
+
|
446
|
+
symbol * assign_to(struct SN_env * z, symbol * p) {
|
447
|
+
int len = z->l;
|
448
|
+
if (CAPACITY(p) < len) {
|
449
|
+
p = increase_size(p, len);
|
450
|
+
if (p == NULL)
|
451
|
+
return NULL;
|
452
|
+
}
|
453
|
+
memmove(p, z->p, len * sizeof(symbol));
|
454
|
+
SET_SIZE(p, len);
|
455
|
+
return p;
|
456
|
+
}
|
457
|
+
|
458
|
+
#if 0
|
459
|
+
void debug(struct SN_env * z, int number, int line_count) {
|
460
|
+
int i;
|
461
|
+
int limit = SIZE(z->p);
|
462
|
+
/*if (number >= 0) printf("%3d (line %4d): '", number, line_count);*/
|
463
|
+
if (number >= 0) printf("%3d (line %4d): [%d]'", number, line_count,limit);
|
464
|
+
for (i = 0; i <= limit; i++) {
|
465
|
+
if (z->lb == i) printf("{");
|
466
|
+
if (z->bra == i) printf("[");
|
467
|
+
if (z->c == i) printf("|");
|
468
|
+
if (z->ket == i) printf("]");
|
469
|
+
if (z->l == i) printf("}");
|
470
|
+
if (i < limit)
|
471
|
+
{ int ch = z->p[i];
|
472
|
+
if (ch == 0) ch = '#';
|
473
|
+
printf("%c", ch);
|
474
|
+
}
|
475
|
+
}
|
476
|
+
printf("'\n");
|
477
|
+
}
|
478
|
+
#endif
|