jk-ferret 0.11.8.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (228) hide show
  1. data/CHANGELOG +24 -0
  2. data/MIT-LICENSE +20 -0
  3. data/README +90 -0
  4. data/RELEASE_CHANGES +137 -0
  5. data/RELEASE_NOTES +60 -0
  6. data/Rakefile +443 -0
  7. data/TODO +109 -0
  8. data/TUTORIAL +231 -0
  9. data/bin/ferret-browser +79 -0
  10. data/ext/BZLIB_blocksort.c +1094 -0
  11. data/ext/BZLIB_bzlib.c +1578 -0
  12. data/ext/BZLIB_compress.c +672 -0
  13. data/ext/BZLIB_crctable.c +104 -0
  14. data/ext/BZLIB_decompress.c +626 -0
  15. data/ext/BZLIB_huffman.c +205 -0
  16. data/ext/BZLIB_randtable.c +84 -0
  17. data/ext/STEMMER_api.c +66 -0
  18. data/ext/STEMMER_libstemmer.c +93 -0
  19. data/ext/STEMMER_stem_ISO_8859_1_danish.c +337 -0
  20. data/ext/STEMMER_stem_ISO_8859_1_dutch.c +624 -0
  21. data/ext/STEMMER_stem_ISO_8859_1_english.c +1117 -0
  22. data/ext/STEMMER_stem_ISO_8859_1_finnish.c +762 -0
  23. data/ext/STEMMER_stem_ISO_8859_1_french.c +1246 -0
  24. data/ext/STEMMER_stem_ISO_8859_1_german.c +503 -0
  25. data/ext/STEMMER_stem_ISO_8859_1_hungarian.c +1230 -0
  26. data/ext/STEMMER_stem_ISO_8859_1_italian.c +1065 -0
  27. data/ext/STEMMER_stem_ISO_8859_1_norwegian.c +297 -0
  28. data/ext/STEMMER_stem_ISO_8859_1_porter.c +749 -0
  29. data/ext/STEMMER_stem_ISO_8859_1_portuguese.c +1017 -0
  30. data/ext/STEMMER_stem_ISO_8859_1_spanish.c +1093 -0
  31. data/ext/STEMMER_stem_ISO_8859_1_swedish.c +307 -0
  32. data/ext/STEMMER_stem_ISO_8859_2_romanian.c +998 -0
  33. data/ext/STEMMER_stem_KOI8_R_russian.c +700 -0
  34. data/ext/STEMMER_stem_UTF_8_danish.c +339 -0
  35. data/ext/STEMMER_stem_UTF_8_dutch.c +634 -0
  36. data/ext/STEMMER_stem_UTF_8_english.c +1125 -0
  37. data/ext/STEMMER_stem_UTF_8_finnish.c +768 -0
  38. data/ext/STEMMER_stem_UTF_8_french.c +1256 -0
  39. data/ext/STEMMER_stem_UTF_8_german.c +509 -0
  40. data/ext/STEMMER_stem_UTF_8_hungarian.c +1234 -0
  41. data/ext/STEMMER_stem_UTF_8_italian.c +1073 -0
  42. data/ext/STEMMER_stem_UTF_8_norwegian.c +299 -0
  43. data/ext/STEMMER_stem_UTF_8_porter.c +755 -0
  44. data/ext/STEMMER_stem_UTF_8_portuguese.c +1023 -0
  45. data/ext/STEMMER_stem_UTF_8_romanian.c +1004 -0
  46. data/ext/STEMMER_stem_UTF_8_russian.c +694 -0
  47. data/ext/STEMMER_stem_UTF_8_spanish.c +1097 -0
  48. data/ext/STEMMER_stem_UTF_8_swedish.c +309 -0
  49. data/ext/STEMMER_stem_UTF_8_turkish.c +2205 -0
  50. data/ext/STEMMER_utilities.c +478 -0
  51. data/ext/analysis.c +1710 -0
  52. data/ext/analysis.h +266 -0
  53. data/ext/api.h +26 -0
  54. data/ext/array.c +125 -0
  55. data/ext/array.h +62 -0
  56. data/ext/bitvector.c +96 -0
  57. data/ext/bitvector.h +594 -0
  58. data/ext/bzlib.h +282 -0
  59. data/ext/bzlib_private.h +503 -0
  60. data/ext/compound_io.c +384 -0
  61. data/ext/config.h +52 -0
  62. data/ext/document.c +159 -0
  63. data/ext/document.h +63 -0
  64. data/ext/except.c +102 -0
  65. data/ext/except.h +176 -0
  66. data/ext/extconf.rb +15 -0
  67. data/ext/ferret.c +416 -0
  68. data/ext/ferret.h +94 -0
  69. data/ext/field_index.c +262 -0
  70. data/ext/field_index.h +52 -0
  71. data/ext/filter.c +157 -0
  72. data/ext/fs_store.c +493 -0
  73. data/ext/global.c +458 -0
  74. data/ext/global.h +302 -0
  75. data/ext/hash.c +524 -0
  76. data/ext/hash.h +515 -0
  77. data/ext/hashset.c +192 -0
  78. data/ext/hashset.h +215 -0
  79. data/ext/header.h +58 -0
  80. data/ext/helper.c +63 -0
  81. data/ext/helper.h +21 -0
  82. data/ext/index.c +6804 -0
  83. data/ext/index.h +935 -0
  84. data/ext/internal.h +1019 -0
  85. data/ext/lang.c +10 -0
  86. data/ext/lang.h +68 -0
  87. data/ext/libstemmer.h +79 -0
  88. data/ext/mempool.c +88 -0
  89. data/ext/mempool.h +43 -0
  90. data/ext/modules.h +190 -0
  91. data/ext/multimapper.c +351 -0
  92. data/ext/multimapper.h +60 -0
  93. data/ext/posh.c +1006 -0
  94. data/ext/posh.h +973 -0
  95. data/ext/priorityqueue.c +149 -0
  96. data/ext/priorityqueue.h +155 -0
  97. data/ext/q_boolean.c +1621 -0
  98. data/ext/q_const_score.c +162 -0
  99. data/ext/q_filtered_query.c +212 -0
  100. data/ext/q_fuzzy.c +280 -0
  101. data/ext/q_match_all.c +149 -0
  102. data/ext/q_multi_term.c +673 -0
  103. data/ext/q_parser.c +3103 -0
  104. data/ext/q_phrase.c +1206 -0
  105. data/ext/q_prefix.c +98 -0
  106. data/ext/q_range.c +682 -0
  107. data/ext/q_span.c +2390 -0
  108. data/ext/q_term.c +337 -0
  109. data/ext/q_wildcard.c +167 -0
  110. data/ext/r_analysis.c +2626 -0
  111. data/ext/r_index.c +3468 -0
  112. data/ext/r_qparser.c +635 -0
  113. data/ext/r_search.c +4490 -0
  114. data/ext/r_store.c +513 -0
  115. data/ext/r_utils.c +1131 -0
  116. data/ext/ram_store.c +476 -0
  117. data/ext/scanner.c +895 -0
  118. data/ext/scanner.h +36 -0
  119. data/ext/scanner_mb.c +6701 -0
  120. data/ext/scanner_utf8.c +4415 -0
  121. data/ext/search.c +1864 -0
  122. data/ext/search.h +953 -0
  123. data/ext/similarity.c +151 -0
  124. data/ext/similarity.h +89 -0
  125. data/ext/sort.c +786 -0
  126. data/ext/stem_ISO_8859_1_danish.h +16 -0
  127. data/ext/stem_ISO_8859_1_dutch.h +16 -0
  128. data/ext/stem_ISO_8859_1_english.h +16 -0
  129. data/ext/stem_ISO_8859_1_finnish.h +16 -0
  130. data/ext/stem_ISO_8859_1_french.h +16 -0
  131. data/ext/stem_ISO_8859_1_german.h +16 -0
  132. data/ext/stem_ISO_8859_1_hungarian.h +16 -0
  133. data/ext/stem_ISO_8859_1_italian.h +16 -0
  134. data/ext/stem_ISO_8859_1_norwegian.h +16 -0
  135. data/ext/stem_ISO_8859_1_porter.h +16 -0
  136. data/ext/stem_ISO_8859_1_portuguese.h +16 -0
  137. data/ext/stem_ISO_8859_1_spanish.h +16 -0
  138. data/ext/stem_ISO_8859_1_swedish.h +16 -0
  139. data/ext/stem_ISO_8859_2_romanian.h +16 -0
  140. data/ext/stem_KOI8_R_russian.h +16 -0
  141. data/ext/stem_UTF_8_danish.h +16 -0
  142. data/ext/stem_UTF_8_dutch.h +16 -0
  143. data/ext/stem_UTF_8_english.h +16 -0
  144. data/ext/stem_UTF_8_finnish.h +16 -0
  145. data/ext/stem_UTF_8_french.h +16 -0
  146. data/ext/stem_UTF_8_german.h +16 -0
  147. data/ext/stem_UTF_8_hungarian.h +16 -0
  148. data/ext/stem_UTF_8_italian.h +16 -0
  149. data/ext/stem_UTF_8_norwegian.h +16 -0
  150. data/ext/stem_UTF_8_porter.h +16 -0
  151. data/ext/stem_UTF_8_portuguese.h +16 -0
  152. data/ext/stem_UTF_8_romanian.h +16 -0
  153. data/ext/stem_UTF_8_russian.h +16 -0
  154. data/ext/stem_UTF_8_spanish.h +16 -0
  155. data/ext/stem_UTF_8_swedish.h +16 -0
  156. data/ext/stem_UTF_8_turkish.h +16 -0
  157. data/ext/stopwords.c +410 -0
  158. data/ext/store.c +698 -0
  159. data/ext/store.h +799 -0
  160. data/ext/symbol.c +10 -0
  161. data/ext/symbol.h +23 -0
  162. data/ext/term_vectors.c +73 -0
  163. data/ext/threading.h +31 -0
  164. data/ext/win32.h +62 -0
  165. data/lib/ferret.rb +30 -0
  166. data/lib/ferret/browser.rb +246 -0
  167. data/lib/ferret/browser/s/global.js +192 -0
  168. data/lib/ferret/browser/s/style.css +148 -0
  169. data/lib/ferret/browser/views/document/list.rhtml +49 -0
  170. data/lib/ferret/browser/views/document/show.rhtml +27 -0
  171. data/lib/ferret/browser/views/error/index.rhtml +7 -0
  172. data/lib/ferret/browser/views/help/index.rhtml +8 -0
  173. data/lib/ferret/browser/views/home/index.rhtml +29 -0
  174. data/lib/ferret/browser/views/layout.rhtml +22 -0
  175. data/lib/ferret/browser/views/term-vector/index.rhtml +4 -0
  176. data/lib/ferret/browser/views/term/index.rhtml +199 -0
  177. data/lib/ferret/browser/views/term/termdocs.rhtml +1 -0
  178. data/lib/ferret/browser/webrick.rb +14 -0
  179. data/lib/ferret/document.rb +130 -0
  180. data/lib/ferret/field_infos.rb +44 -0
  181. data/lib/ferret/field_symbol.rb +87 -0
  182. data/lib/ferret/index.rb +973 -0
  183. data/lib/ferret/number_tools.rb +157 -0
  184. data/lib/ferret/version.rb +3 -0
  185. data/setup.rb +1555 -0
  186. data/test/long_running/largefile/tc_largefile.rb +46 -0
  187. data/test/test_all.rb +5 -0
  188. data/test/test_helper.rb +29 -0
  189. data/test/test_installed.rb +1 -0
  190. data/test/threading/number_to_spoken.rb +132 -0
  191. data/test/threading/thread_safety_index_test.rb +88 -0
  192. data/test/threading/thread_safety_read_write_test.rb +73 -0
  193. data/test/threading/thread_safety_test.rb +133 -0
  194. data/test/unit/analysis/tc_analyzer.rb +550 -0
  195. data/test/unit/analysis/tc_token_stream.rb +653 -0
  196. data/test/unit/index/tc_index.rb +867 -0
  197. data/test/unit/index/tc_index_reader.rb +699 -0
  198. data/test/unit/index/tc_index_writer.rb +447 -0
  199. data/test/unit/index/th_doc.rb +332 -0
  200. data/test/unit/query_parser/tc_query_parser.rb +238 -0
  201. data/test/unit/search/tc_filter.rb +156 -0
  202. data/test/unit/search/tc_fuzzy_query.rb +147 -0
  203. data/test/unit/search/tc_index_searcher.rb +67 -0
  204. data/test/unit/search/tc_multi_searcher.rb +128 -0
  205. data/test/unit/search/tc_multiple_search_requests.rb +58 -0
  206. data/test/unit/search/tc_search_and_sort.rb +179 -0
  207. data/test/unit/search/tc_sort.rb +49 -0
  208. data/test/unit/search/tc_sort_field.rb +27 -0
  209. data/test/unit/search/tc_spans.rb +190 -0
  210. data/test/unit/search/tm_searcher.rb +436 -0
  211. data/test/unit/store/tc_fs_store.rb +115 -0
  212. data/test/unit/store/tc_ram_store.rb +35 -0
  213. data/test/unit/store/tm_store.rb +34 -0
  214. data/test/unit/store/tm_store_lock.rb +68 -0
  215. data/test/unit/tc_document.rb +81 -0
  216. data/test/unit/tc_field_symbol.rb +26 -0
  217. data/test/unit/ts_analysis.rb +2 -0
  218. data/test/unit/ts_index.rb +2 -0
  219. data/test/unit/ts_largefile.rb +4 -0
  220. data/test/unit/ts_query_parser.rb +2 -0
  221. data/test/unit/ts_search.rb +2 -0
  222. data/test/unit/ts_store.rb +2 -0
  223. data/test/unit/ts_utils.rb +2 -0
  224. data/test/unit/utils/tc_bit_vector.rb +295 -0
  225. data/test/unit/utils/tc_number_tools.rb +117 -0
  226. data/test/unit/utils/tc_priority_queue.rb +106 -0
  227. data/test/utils/content_generator.rb +226 -0
  228. metadata +319 -0
@@ -0,0 +1,478 @@
1
+
2
+ #include <stdio.h>
3
+ #include <stdlib.h>
4
+ #include <string.h>
5
+
6
+ #include "header.h"
7
+
8
+ #define unless(C) if(!(C))
9
+
10
+ #define CREATE_SIZE 1
11
+
12
+ extern 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
+ extern 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
+ extern 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
+ extern 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
+ extern 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
+ extern 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
+ extern 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
+ extern 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
+ extern 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
+ extern 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
+ extern 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
+ extern 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
+ extern 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
+ extern int eq_v(struct SN_env * z, const symbol * p) {
205
+ return eq_s(z, SIZE(p), p);
206
+ }
207
+
208
+ extern int eq_v_b(struct SN_env * z, const symbol * p) {
209
+ return eq_s_b(z, SIZE(p), p);
210
+ }
211
+
212
+ extern 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
+ extern 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
+ extern 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
+ extern 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
+ extern int slice_from_v(struct SN_env * z, const symbol * p) {
403
+ return slice_from_s(z, SIZE(p), p);
404
+ }
405
+
406
+ extern int slice_del(struct SN_env * z) {
407
+ return slice_from_s(z, 0, 0);
408
+ }
409
+
410
+ extern 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
+ extern 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
+ extern 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
+ extern 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
+ extern 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