forked from WojciechMula/pyahocorasick
/
AutomatonSearchIter.c
258 lines (215 loc) · 6.39 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
/*
This is part of pyahocorasick Python module.
AutomatonSearchIter implementation
Author : Wojciech Mu³a, wojciech_mula@poczta.onet.pl
WWW : http://0x80.pl/proj/pyahocorasick/
License : 3-clauses BSD (see LICENSE)
Date : $Date$
$Id$
*/
#include "AutomatonSearchIter.h"
static PyTypeObject automaton_search_iter_type;
static PyObject*
automaton_search_iter_new(
Automaton* automaton,
PyObject* object,
int start,
int end
) {
AutomatonSearchIter* iter;
iter = (AutomatonSearchIter*)PyObject_New(AutomatonSearchIter, &automaton_search_iter_type);
if (iter == NULL)
return NULL;
iter->automaton = automaton;
iter->version = automaton->version;
iter->object = object;
#ifdef AHOCORASICK_UNICODE
iter->data = PyUnicode_AS_UNICODE(object);
#else
iter->data = (uint8_t*)PyBytes_AS_STRING(object);
#endif
iter->state = automaton->root;
iter->output= NULL;
iter->shift = 0;
iter->index = start - 1; // -1 because first instruction in next() increments index
iter->end = end;
Py_INCREF(iter->automaton);
Py_INCREF(iter->object);
return (PyObject*)iter;
}
#define iter ((AutomatonSearchIter*)self)
static void
automaton_search_iter_del(PyObject* self) {
Py_DECREF(iter->automaton);
Py_DECREF(iter->object);
PyObject_Del(self);
}
static PyObject*
automaton_search_iter_iter(PyObject* self) {
Py_INCREF(self);
return self;
}
static PyObject*
automaton_search_iter_next(PyObject* self) {
if (iter->version != iter->automaton->version) {
PyErr_SetString(PyExc_ValueError, "underlaying automaton has changed, iterator is not valid anymore");
return NULL;
}
return_output:
if (iter->output and iter->output->eow) {
TrieNode* node = iter->output;
PyObject* tuple;
switch (iter->automaton->store) {
case STORE_LENGTH:
case STORE_INTS:
tuple = Py_BuildValue("ii",
iter->index + iter->shift,
node->output.integer);
break;
case STORE_ANY:
tuple = Py_BuildValue("iO",
iter->index + iter->shift,
node->output.object);
break;
default:
PyErr_SetString(PyExc_ValueError, "inconsistent internal state!");
return NULL;
}
// next element to output
iter->output = iter->output->fail;
// yield value
return tuple;
}
else
iter->index += 1;
while (iter->index < iter->end) {
#define NEXT(byte) ahocorasick_next(iter->state, iter->automaton->root, (byte))
// process single char
iter->state = ahocorasick_next(
iter->state,
iter->automaton->root,
iter->data[iter->index]
);
ASSERT(iter->state);
if (iter->state->eow) {
iter->output = iter->state;
goto return_output;
}
else
iter->index += 1;
} // while
return NULL; // StopIteration
}
static PyObject*
automaton_search_iter_set(PyObject* self, PyObject* args) {
PyObject* object;
PyObject* flag;
ssize_t len;
bool reset;
// first argument - required string or buffer
object = PyTuple_GetItem(args, 0);
if (object) {
#ifdef AHOCORASICK_UNICODE
if (PyUnicode_Check(object))
len = PyUnicode_GET_SIZE(object);
else {
PyErr_SetString(PyExc_TypeError, "string required");
return NULL;
}
#else
if (PyBytes_Check(object))
len = PyBytes_GET_SIZE(object);
else {
PyErr_SetString(PyExc_TypeError, "string or bytes object required");
return NULL;
}
#endif
}
else
return NULL;
// second argument - optional bool
flag = 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;
}
// update internal state
Py_XDECREF(iter->object);
Py_INCREF(object);
iter->object = object;
#ifdef AHOCORASICK_UNICODE
iter->data = PyUnicode_AS_UNICODE(object);
#else
iter->data = (uint8_t*)PyBytes_AS_STRING(object);
#endif
if (!reset)
iter->shift += (iter->index >= 0) ? iter->index : 0;
iter->index = -1;
iter->end = len;
if (reset) {
iter->state = iter->automaton->root;
iter->shift = 0;
iter->output = NULL;
}
Py_RETURN_NONE;
}
#undef iter
#define method(name, kind) {#name, automaton_search_iter_##name, kind, ""}
static
PyMethodDef automaton_search_iter_methods[] = {
method(set, METH_VARARGS),
{NULL, NULL, 0, NULL}
};
#undef method
static PyTypeObject automaton_search_iter_type = {
PyVarObject_HEAD_INIT(&PyType_Type, 0)
"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 */
0, /* 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 */
};