Debian Codegraph @

mkdocs/contrib/search/lunr-language/lunr.stemmer.support.js

raw
1 /*!
2 * Snowball JavaScript Library v0.3
3 * http://code.google.com/p/urim/
4 * http://snowball.tartarus.org/
5 *
6 * Copyright 2010, Oleg Mazko
7 * http://www.mozilla.org/MPL/
8 */
9
10 /**
11 * export the module via AMD, CommonJS or as a browser global
12 * Export code from https://github.com/umdjs/umd/blob/master/returnExports.js
13 */
14 ;(function (root, factory) {
15 if (typeof define === 'function' && define.amd) {
16 // AMD. Register as an anonymous module.
17 define(factory)
18 } else if (typeof exports === 'object') {
19 /**
20 * Node. Does not work with strict CommonJS, but
21 * only CommonJS-like environments that support module.exports,
22 * like Node.
23 */
24 module.exports = factory()
25 } else {
26 // Browser globals (root is window)
27 factory()(root.lunr);
28 }
29 }(this, function () {
30 /**
31 * Just return a value to define the module export.
32 * This example returns an object, but the module
33 * can return a function as the exported value.
34 */
35 return function(lunr) {
36 /* provides utilities for the included stemmers */
37 lunr.stemmerSupport = {
38 Among: function(s, substring_i, result, method) {
39 this.toCharArray = function(s) {
40 var sLength = s.length, charArr = new Array(sLength);
41 for (var i = 0; i < sLength; i++)
42 charArr[i] = s.charCodeAt(i);
43 return charArr;
44 };
45
46 if ((!s && s != "") || (!substring_i && (substring_i != 0)) || !result)
47 throw ("Bad Among initialisation: s:" + s + ", substring_i: "
48 + substring_i + ", result: " + result);
49 this.s_size = s.length;
50 this.s = this.toCharArray(s);
51 this.substring_i = substring_i;
52 this.result = result;
53 this.method = method;
54 },
55 SnowballProgram: function() {
56 var current;
57 return {
58 bra : 0,
59 ket : 0,
60 limit : 0,
61 cursor : 0,
62 limit_backward : 0,
63 setCurrent : function(word) {
64 current = word;
65 this.cursor = 0;
66 this.limit = word.length;
67 this.limit_backward = 0;
68 this.bra = this.cursor;
69 this.ket = this.limit;
70 },
71 getCurrent : function() {
72 var result = current;
73 current = null;
74 return result;
75 },
76 in_grouping : function(s, min, max) {
77 if (this.cursor < this.limit) {
78 var ch = current.charCodeAt(this.cursor);
79 if (ch <= max && ch >= min) {
80 ch -= min;
81 if (s[ch >> 3] & (0X1 << (ch & 0X7))) {
82 this.cursor++;
83 return true;
84 }
85 }
86 }
87 return false;
88 },
89 in_grouping_b : function(s, min, max) {
90 if (this.cursor > this.limit_backward) {
91 var ch = current.charCodeAt(this.cursor - 1);
92 if (ch <= max && ch >= min) {
93 ch -= min;
94 if (s[ch >> 3] & (0X1 << (ch & 0X7))) {
95 this.cursor--;
96 return true;
97 }
98 }
99 }
100 return false;
101 },
102 out_grouping : function(s, min, max) {
103 if (this.cursor < this.limit) {
104 var ch = current.charCodeAt(this.cursor);
105 if (ch > max || ch < min) {
106 this.cursor++;
107 return true;
108 }
109 ch -= min;
110 if (!(s[ch >> 3] & (0X1 << (ch & 0X7)))) {
111 this.cursor++;
112 return true;
113 }
114 }
115 return false;
116 },
117 out_grouping_b : function(s, min, max) {
118 if (this.cursor > this.limit_backward) {
119 var ch = current.charCodeAt(this.cursor - 1);
120 if (ch > max || ch < min) {
121 this.cursor--;
122 return true;
123 }
124 ch -= min;
125 if (!(s[ch >> 3] & (0X1 << (ch & 0X7)))) {
126 this.cursor--;
127 return true;
128 }
129 }
130 return false;
131 },
132 eq_s : function(s_size, s) {
133 if (this.limit - this.cursor < s_size)
134 return false;
135 for (var i = 0; i < s_size; i++)
136 if (current.charCodeAt(this.cursor + i) != s.charCodeAt(i))
137 return false;
138 this.cursor += s_size;
139 return true;
140 },
141 eq_s_b : function(s_size, s) {
142 if (this.cursor - this.limit_backward < s_size)
143 return false;
144 for (var i = 0; i < s_size; i++)
145 if (current.charCodeAt(this.cursor - s_size + i) != s
146 .charCodeAt(i))
147 return false;
148 this.cursor -= s_size;
149 return true;
150 },
151 find_among : function(v, v_size) {
152 var i = 0, j = v_size, c = this.cursor, l = this.limit, common_i = 0, common_j = 0, first_key_inspected = false;
153 while (true) {
154 var k = i + ((j - i) >> 1), diff = 0, common = common_i < common_j
155 ? common_i
156 : common_j, w = v[k];
157 for (var i2 = common; i2 < w.s_size; i2++) {
158 if (c + common == l) {
159 diff = -1;
160 break;
161 }
162 diff = current.charCodeAt(c + common) - w.s[i2];
163 if (diff)
164 break;
165 common++;
166 }
167 if (diff < 0) {
168 j = k;
169 common_j = common;
170 } else {
171 i = k;
172 common_i = common;
173 }
174 if (j - i <= 1) {
175 if (i > 0 || j == i || first_key_inspected)
176 break;
177 first_key_inspected = true;
178 }
179 }
180 while (true) {
181 var w = v[i];
182 if (common_i >= w.s_size) {
183 this.cursor = c + w.s_size;
184 if (!w.method)
185 return w.result;
186 var res = w.method();
187 this.cursor = c + w.s_size;
188 if (res)
189 return w.result;
190 }
191 i = w.substring_i;
192 if (i < 0)
193 return 0;
194 }
195 },
196 find_among_b : function(v, v_size) {
197 var i = 0, j = v_size, c = this.cursor, lb = this.limit_backward, common_i = 0, common_j = 0, first_key_inspected = false;
198 while (true) {
199 var k = i + ((j - i) >> 1), diff = 0, common = common_i < common_j
200 ? common_i
201 : common_j, w = v[k];
202 for (var i2 = w.s_size - 1 - common; i2 >= 0; i2--) {
203 if (c - common == lb) {
204 diff = -1;
205 break;
206 }
207 diff = current.charCodeAt(c - 1 - common) - w.s[i2];
208 if (diff)
209 break;
210 common++;
211 }
212 if (diff < 0) {
213 j = k;
214 common_j = common;
215 } else {
216 i = k;
217 common_i = common;
218 }
219 if (j - i <= 1) {
220 if (i > 0 || j == i || first_key_inspected)
221 break;
222 first_key_inspected = true;
223 }
224 }
225 while (true) {
226 var w = v[i];
227 if (common_i >= w.s_size) {
228 this.cursor = c - w.s_size;
229 if (!w.method)
230 return w.result;
231 var res = w.method();
232 this.cursor = c - w.s_size;
233 if (res)
234 return w.result;
235 }
236 i = w.substring_i;
237 if (i < 0)
238 return 0;
239 }
240 },
241 replace_s : function(c_bra, c_ket, s) {
242 var adjustment = s.length - (c_ket - c_bra), left = current
243 .substring(0, c_bra), right = current.substring(c_ket);
244 current = left + s + right;
245 this.limit += adjustment;
246 if (this.cursor >= c_ket)
247 this.cursor += adjustment;
248 else if (this.cursor > c_bra)
249 this.cursor = c_bra;
250 return adjustment;
251 },
252 slice_check : function() {
253 if (this.bra < 0 || this.bra > this.ket || this.ket > this.limit
254 || this.limit > current.length)
255 throw ("faulty slice operation");
256 },
257 slice_from : function(s) {
258 this.slice_check();
259 this.replace_s(this.bra, this.ket, s);
260 },
261 slice_del : function() {
262 this.slice_from("");
263 },
264 insert : function(c_bra, c_ket, s) {
265 var adjustment = this.replace_s(c_bra, c_ket, s);
266 if (c_bra <= this.bra)
267 this.bra += adjustment;
268 if (c_bra <= this.ket)
269 this.ket += adjustment;
270 },
271 slice_to : function() {
272 this.slice_check();
273 return current.substring(this.bra, this.ket);
274 },
275 eq_v_b : function(s) {
276 return this.eq_s_b(s.length, s);
277 }
278 };
279 }
280 };
281
282 lunr.trimmerSupport = {
283 generateTrimmer: function(wordCharacters) {
284 var startRegex = new RegExp("^[^" + wordCharacters + "]+")
285 var endRegex = new RegExp("[^" + wordCharacters + "]+$")
286
287 return function(token) {
288 // for lunr version 2
289 if (typeof token.update === "function") {
290 return token.update(function (s) {
291 return s
292 .replace(startRegex, '')
293 .replace(endRegex, '');
294 })
295 } else { // for lunr version 1
296 return token
297 .replace(startRegex, '')
298 .replace(endRegex, '');
299 }
300 };
301 }
302 }
303 }
304 }));