forked from WojciechMula/pyahocorasick
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathAutomatonSearchIter.c
536 lines (438 loc) · 14.1 KB
/
AutomatonSearchIter.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
/*
This is part of pyahocorasick Python module.
AutomatonSearchIter implementation
Author : Wojciech Muła, [email protected]
WWW : http://0x80.pl
License : BSD-3-Clause (see LICENSE)
*/
#include "AutomatonSearchIter.h"
#include <wctype.h>
typedef uint64_t opt_int;
static PyTypeObject automaton_search_iter_type;
#ifdef VARIABLE_LEN_CHARCODES
static int
automaton_search_iter_substring_index(struct Input* input, int position) {
TRIE_LETTER_TYPE letter;
int index = 0;
int i;
for (i=0; i < position; i++) {
letter = input->word[index];
if (UNLIKELY(Py_UNICODE_IS_SURROGATE(letter))) {
if (UNLIKELY(!Py_UNICODE_IS_HIGH_SURROGATE(letter))) {
PyErr_Format(PyExc_ValueError,
"Malformed UCS-2 string: expected a high surrogate at %d, got %04x",
index, letter);
return -1;
}
index += 1;
if (index >= input->wordlen) {
PyErr_Format(PyExc_ValueError,
"Malformed UCS-2 string: unexpected end of string");
return -1;
}
letter = input->word[index];
if (UNLIKELY(!Py_UNICODE_IS_LOW_SURROGATE(letter))) {
PyErr_Format(PyExc_ValueError,
"Malformed UCS-2 string: expected a low surrogate at %d, got %04x",
index, letter);
return -1;
}
index += 1;
} else {
index += 1;
}
}
return index;
}
#endif // VARIABLE_LEN_CHARCODES
static PyObject*
automaton_search_iter_new(
Automaton* automaton,
PyObject* object,
int start,
int end,
bool ignore_white_space
) {
AutomatonSearchIter* iter;
#ifdef VARIABLE_LEN_CHARCODES
int tmp;
#endif
iter = (AutomatonSearchIter*)F(PyObject_New)(AutomatonSearchIter, &automaton_search_iter_type);
if (iter == NULL)
return NULL;
iter->automaton = automaton;
iter->version = automaton->version;
iter->state = automaton->root;
iter->output= NULL;
iter->shift = 0;
iter->ignore_white_space = ignore_white_space;
init_input(&iter->input);
Py_INCREF(iter->automaton);
if (!prepare_input((PyObject*)automaton, object, &iter->input)) {
goto error;
}
#ifdef VARIABLE_LEN_CHARCODES
if (automaton->key_type == KEY_STRING) {
tmp = automaton_search_iter_substring_index(&iter->input, start);
if (tmp >= 0) {
iter->index = tmp - 1;
iter->position = start - 1;
} else {
goto error;
}
tmp = automaton_search_iter_substring_index(&iter->input, end);
if (tmp >= 0) {
iter->end = end;
} else {
goto error;
}
iter->expected = pyaho_UCS2_Any;
} else {
iter->index = start - 1;
iter->end = end;
}
#else
// -1 because the first instruction in next() increments index
iter->index = start - 1;
iter->end = end;
#endif
return (PyObject*)iter;
error:
Py_DECREF(iter);
return NULL;
}
#define iter ((AutomatonSearchIter*)self)
static void
automaton_search_iter_del(PyObject* self) {
Py_DECREF(iter->automaton);
destroy_input(&iter->input);
PyObject_Del(self);
}
static PyObject*
automaton_search_iter_iter(PyObject* self) {
Py_INCREF(self);
return self;
}
enum {
OutputValue,
OutputNone,
OutputError
};
static int
automaton_build_output(PyObject* self, PyObject** result) {
TrieNode* node;
Py_ssize_t idx = 0;
while (iter->output && !iter->output->eow) {
iter->output = iter->output->fail;
}
if (iter->output) {
node = iter->output;
iter->output = iter->output->fail;
#ifdef VARIABLE_LEN_CHARCODES
idx = iter->shift;
if (iter->automaton->key_type == KEY_STRING) {
idx += iter->position;
} else {
idx += iter->index;
}
#else
idx = iter->index + iter->shift;
#endif
switch (iter->automaton->store) {
case STORE_LENGTH:
case STORE_INTS:
*result = F(Py_BuildValue)("ii", idx, node->output.integer);
return OutputValue;
case STORE_ANY:
*result = F(Py_BuildValue)("iO", idx, node->output.object);
return OutputValue;
default:
PyErr_SetString(PyExc_ValueError, "inconsistent internal state!");
return OutputError;
}
}
return OutputNone;
}
static int
automaton_build_output_optimized(PyObject* self, opt_int* results, int results_size) {
TrieNode* node;
int value;
while (iter->output && !iter->output->eow) {
iter->output = iter->output->fail;
}
if (iter->output) {
node = iter->output;
iter->output = iter->output->fail;
value = node->output.integer;
if ((0 <= value) && (value < results_size)) {
results[value]++;
return OutputValue;
} else {
PyErr_SetString(PyExc_ValueError, "results buffer is too small");
return OutputError;
}
} else {
return OutputNone;
}
}
#ifdef VARIABLE_LEN_CHARCODES
static bool
automaton_search_iter_advance_index(PyObject* self) {
TRIE_LETTER_TYPE letter;
iter->index += 1;
if (iter->automaton->key_type == KEY_SEQUENCE) {
return true;
}
letter = iter->input.word[iter->index];
if (iter->expected == pyaho_UCS2_Any) {
if (UNLIKELY(Py_UNICODE_IS_SURROGATE(letter))) {
if (LIKELY(Py_UNICODE_IS_HIGH_SURROGATE(letter))) {
iter->expected = pyaho_UCS2_LowSurrogate;
} else {
PyErr_Format(PyExc_ValueError,
"Malformed UCS-2 string: expected a high surrogate at %d, got %04x",
iter->index, letter);
return false;
}
} else {
iter->position += 1;
}
} else {
assert(iter->expected == pyaho_UCS2_LowSurrogate);
if (LIKELY(Py_UNICODE_IS_LOW_SURROGATE(letter))) {
iter->expected = pyaho_UCS2_Any;
iter->position += 1;
} else {
PyErr_Format(PyExc_ValueError,
"Malformed UCS-2 string: expected a low surrogate at %d, got %04x",
iter->index, letter);
return false;
}
}
return true;
}
#endif
static PyObject*
automaton_search_iter_next(PyObject* self) {
PyObject* output;
if (iter->version != iter->automaton->version) {
PyErr_SetString(PyExc_ValueError, "underlaying automaton has changed, iterator is not valid anymore");
return NULL;
}
return_output:
switch (automaton_build_output(self, &output)) {
case OutputValue:
return output;
case OutputNone:
break;
case OutputError:
return NULL;
}
#ifdef VARIABLE_LEN_CHARCODES
if (!automaton_search_iter_advance_index(self)) {
return NULL;
}
#else
iter->index += 1;
if (iter->ignore_white_space) {
while (iswspace(iter->input.word[iter->index]) && (iter->index < iter->end)) {
iter->index += 1;
}
}
#endif
while (iter->index < iter->end) {
// process single char
iter->state = ahocorasick_next(
iter->state,
iter->automaton->root,
iter->input.word[iter->index]
);
ASSERT(iter->state);
iter->output = iter->state;
goto return_output;
#ifdef VARIABLE_LEN_CHARCODES
if (!automaton_search_iter_advance_index(self)) {
return NULL;
}
#else
iter->index += 1;
#endif
} // while
return NULL; // StopIteration
}
static int
automaton_search_iter_next_optimized(PyObject* self, opt_int* results, int results_size) {
if (iter->version != iter->automaton->version) {
PyErr_SetString(PyExc_ValueError, "underlaying automaton has changed, iterator is not valid anymore");
return 0;
}
if (iter->automaton->store != STORE_INTS) {
PyErr_SetString(PyExc_ValueError, "underlaying automaton is not an integer key store");
return 0;
}
return_output:
switch (automaton_build_output_optimized(self, results, results_size)) {
case OutputValue:
return 1;
case OutputNone:
break;
case OutputError:
return 0;
}
#ifdef VARIABLE_LEN_CHARCODES
if (!automaton_search_iter_advance_index(self)) {
return 0;
}
#else
iter->index += 1;
if (iter->ignore_white_space) {
while (iswspace(iter->input.word[iter->index]) && (iter->index < iter->end)) {
iter->index += 1;
}
}
#endif
while (iter->index < iter->end) {
// process single char
iter->state = ahocorasick_next(
iter->state,
iter->automaton->root,
iter->input.word[iter->index]
);
ASSERT(iter->state);
iter->output = iter->state;
goto return_output;
#ifdef VARIABLE_LEN_CHARCODES
if (!automaton_search_iter_advance_index(self)) {
return 0;
}
#else
iter->index += 1;
#endif
} // while
return 0; // StopIteration
}
static PyObject*
automaton_search_iter_set(PyObject* self, PyObject* args) {
PyObject* object;
PyObject* flag;
Py_ssize_t position;
bool reset;
struct Input new_input;
// first argument - required string or buffer
object = F(PyTuple_GetItem)(args, 0);
if (object) {
if (!prepare_input((PyObject*)iter->automaton, object, &new_input)) {
return NULL;
}
}
else
return NULL;
// second argument - optional bool
flag = F(PyTuple_GetItem)(args, 1);
if (flag) {
switch (PyObject_IsTrue(flag)) {
case 0:
reset = false;
break;
case 1:
reset = true;
break;
default:
return NULL;
}
}
else {
PyErr_Clear();
reset = false;
}
destroy_input(&iter->input);
assign_input(&iter->input, &new_input);
if (!reset) {
position = iter->index;
#ifdef VARIABLE_LEN_CHARCODES
if (iter->automaton->key_type == KEY_STRING) {
position = iter->position;
}
#endif
iter->shift += (position >= 0) ? position : 0;
}
iter->index = -1;
iter->end = new_input.wordlen;
if (reset) {
iter->state = iter->automaton->root;
iter->shift = 0;
iter->output = NULL;
#ifdef VARIABLE_LEN_CHARCODES
iter->position = -1;
iter->expected = pyaho_UCS2_Any;
#endif
}
Py_RETURN_NONE;
}
static PyObject*
automaton_search_iter_all(PyObject* self, PyObject* args) {
Py_buffer buf;
if (!PyArg_ParseTuple(args, "w*", &buf)) {
return NULL;
}
if (buf.itemsize != sizeof(opt_int)) {
PyErr_SetString(PyExc_ValueError, "invalid buffer type (expected int64)");
PyBuffer_Release(&buf);
return NULL;
}
long out = 0;
while (automaton_search_iter_next_optimized(self, (opt_int*) buf.buf, buf.len / buf.itemsize)) {
out++;
}
PyBuffer_Release(&buf);
return PyLong_FromLong(out);
}
#undef iter
#define method(name, kind) {#name, automaton_search_iter_##name, kind, automaton_search_iter_##name##_doc}
static
PyMethodDef automaton_search_iter_methods[] = {
method(all, METH_VARARGS),
method(set, METH_VARARGS),
{NULL, NULL, 0, NULL}
};
#undef method
static PyTypeObject automaton_search_iter_type = {
PY_OBJECT_HEAD_INIT
"ahocorasick.AutomatonSearchIter", /* tp_name */
sizeof(AutomatonSearchIter), /* tp_size */
0, /* tp_itemsize? */
(destructor)automaton_search_iter_del, /* tp_dealloc */
0, /* tp_print */
0, /* tp_getattr */
0, /* tp_setattr */
0, /* tp_reserved */
0, /* tp_repr */
0, /* tp_as_number */
0, /* tp_as_sequence */
0, /* tp_as_mapping */
0, /* tp_hash */
0, /* tp_call */
0, /* tp_str */
PyObject_GenericGetAttr, /* tp_getattro */
0, /* tp_setattro */
0, /* tp_as_buffer */
Py_TPFLAGS_DEFAULT, /* tp_flags */
automaton_search_iter_doc, /* tp_doc */
0, /* tp_traverse */
0, /* tp_clear */
0, /* tp_richcompare */
0, /* tp_weaklistoffset */
automaton_search_iter_iter, /* tp_iter */
automaton_search_iter_next, /* tp_iternext */
automaton_search_iter_methods, /* tp_methods */
0, /* tp_members */
0, /* tp_getset */
0, /* tp_base */
0, /* tp_dict */
0, /* tp_descr_get */
0, /* tp_descr_set */
0, /* tp_dictoffset */
0, /* tp_init */
0, /* tp_alloc */
0, /* tp_new */
};