forked from wulijun/php-ext-trie-filter
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtrie_filter.c
425 lines (360 loc) · 10.7 KB
/
trie_filter.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
/*
+----------------------------------------------------------------------+
| PHP Version 5 |
+----------------------------------------------------------------------+
| Copyright (c) 1997-2010 The PHP Group |
+----------------------------------------------------------------------+
| This source file is subject to version 3.01 of the PHP license, |
| that is bundled with this package in the file LICENSE, and is |
| available through the world-wide-web at the following url: |
| http://www.php.net/license/3_01.txt |
| If you did not receive a copy of the PHP license and are unable to |
| obtain it through the world-wide-web, please send a note to |
| [email protected] so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
| Author: Lijun Wu <[email protected]> |
+----------------------------------------------------------------------+
*/
/* $Id$ */
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "php.h"
#include "php_ini.h"
#include "ext/standard/info.h"
#include "php_trie_filter.h"
/* True global resources - no need for thread safety here */
static int le_trie_filter;
/* {{{ trie_filter_functions[]
*
* Every user visible function must have an entry in trie_filter_functions[].
*/
zend_function_entry trie_filter_functions[] = {
PHP_FE(trie_filter_load, NULL)
PHP_FE(trie_filter_search, NULL)
PHP_FE(trie_filter_search_all, NULL)
PHP_FE(trie_filter_new, NULL)
PHP_FE(trie_filter_store, NULL)
PHP_FE(trie_filter_save, NULL)
PHP_FE(trie_filter_free, NULL)
{NULL, NULL, NULL} /* Must be the last line in trie_filter_functions[] */
};
/* }}} */
/* {{{ trie_filter_module_entry
*/
zend_module_entry trie_filter_module_entry = {
#if ZEND_MODULE_API_NO >= 20010901
STANDARD_MODULE_HEADER,
#endif
"trie_filter",
trie_filter_functions,
PHP_MINIT(trie_filter),
PHP_MSHUTDOWN(trie_filter),
NULL,
NULL,
PHP_MINFO(trie_filter),
#if ZEND_MODULE_API_NO >= 20010901
"0.1", /* Replace with version number for your extension */
#endif
STANDARD_MODULE_PROPERTIES
};
/* }}} */
#ifdef COMPILE_DL_TRIE_FILTER
ZEND_GET_MODULE(trie_filter)
#endif
/* {{{ PHP_INI
*/
/*
PHP_INI_BEGIN()
PHP_INI_ENTRY("trie_filter.dict_charset", "utf-8", PHP_INI_ALL, NULL)
PHP_INI_END()
*/
/* }}} */
static void php_trie_filter_dtor(zend_resource *rsrc TSRMLS_DC)
{
Trie *trie = (Trie *)rsrc->ptr;
trie_free(trie);
}
/* {{{ PHP_MINIT_FUNCTION
*/
PHP_MINIT_FUNCTION(trie_filter)
{
le_trie_filter = zend_register_list_destructors_ex(
php_trie_filter_dtor,
NULL, PHP_TRIE_FILTER_RES_NAME, module_number);
return SUCCESS;
}
/* }}} */
/* {{{ PHP_MSHUTDOWN_FUNCTION
*/
PHP_MSHUTDOWN_FUNCTION(trie_filter)
{
return SUCCESS;
}
/* }}} */
/* {{{ PHP_MINFO_FUNCTION
*/
PHP_MINFO_FUNCTION(trie_filter)
{
php_info_print_table_start();
php_info_print_table_header(2, "trie_filter support", "enabled");
php_info_print_table_end();
}
/* }}} */
/* {{{ proto resource trie_filter_load(string dict_file_path)
Returns resource id, or NULL on error*/
PHP_FUNCTION(trie_filter_load)
{
Trie *trie;
char *path;
size_t path_len;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s",
&path, &path_len) == FAILURE) {
RETURN_NULL();
}
trie = trie_new_from_file(path);
if (!trie) {
php_error_docref(NULL TSRMLS_CC, E_WARNING,
"Unable to load %s", path);
RETURN_NULL();
}
RETURN_RES(zend_register_resource(trie, le_trie_filter));
}
/* }}} */
static int trie_search_one(Trie *trie, const AlphaChar *text, int *offset, TrieData *length)
{
TrieState *s;
const AlphaChar *p;
const AlphaChar *base;
base = text;
if (! (s = trie_root(trie))) {
return -1;
}
while (*text) {
p = text;
if (! trie_state_is_walkable(s, *p)) {
trie_state_rewind(s);
text++;
continue;
} else {
trie_state_walk(s, *p++);
}
while (trie_state_is_walkable(s, *p) && ! trie_state_is_terminal(s))
trie_state_walk(s, *p++);
if (trie_state_is_terminal(s)) {
*offset = text - base;
*length = p - text;
trie_state_free(s);
return 1;
}
trie_state_rewind(s);
text++;
}
trie_state_free(s);
return 0;
}
static int trie_search_all(Trie *trie, const AlphaChar *text, zval *data)
{
TrieState *s;
const AlphaChar *p;
const AlphaChar *base;
zval word;
base = text;
if (! (s = trie_root(trie))) {
return -1;
}
while (*text) {
p = text;
if(! trie_state_is_walkable(s, *p)) {
trie_state_rewind(s);
text++;
continue;
}
while(*p && trie_state_is_walkable(s, *p) && ! trie_state_is_leaf(s)) {
trie_state_walk(s, *p++);
if (trie_state_is_terminal(s)) {
array_init_size(&word, 3);
add_next_index_long(&word, text - base);
add_next_index_long(&word, p - text);
add_next_index_zval(data, &word);
}
}
trie_state_rewind(s);
text++;
}
trie_state_free(s);
return 0;
}
/* {{{ proto array trie_filter_search(int trie_tree_identifier, string centent)
Returns info about first keyword, or false on error*/
PHP_FUNCTION(trie_filter_search)
{
Trie *trie;
zval *trie_resource;
unsigned char *text;
size_t text_len;
int offset = -1, i, ret;
TrieData length = 0;
AlphaChar *alpha_text;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rs",
&trie_resource, &text, &text_len) == FAILURE) {
RETURN_FALSE;
}
array_init(return_value);
if (text_len < 1 || strlen(text) != text_len) {
php_error_docref(NULL TSRMLS_CC, E_NOTICE, "input is empty");
return;
}
trie = (Trie *)zend_fetch_resource(Z_RES_P(trie_resource), PHP_TRIE_FILTER_RES_NAME, le_trie_filter);
alpha_text = emalloc(sizeof(AlphaChar) * (text_len + 1));
for (i = 0; i < text_len; i++) {
alpha_text[i] = (AlphaChar) text[i];
}
alpha_text[text_len] = TRIE_CHAR_TERM;
ret = trie_search_one(trie, alpha_text, &offset, &length);
efree(alpha_text);
if (ret == 0) {
return;
} else if (ret == 1) {
add_next_index_long(return_value, offset);
add_next_index_long(return_value, length);
} else {
RETURN_FALSE;
}
}
/* }}} */
/* {{{ proto array trie_filter_search_all(int trie_tree_identifier, string centent)
Returns info about all keywords, or false on error*/
PHP_FUNCTION(trie_filter_search_all)
{
Trie *trie;
zval *trie_resource;
unsigned char *text;
size_t text_len;
int i, ret;
AlphaChar *alpha_text;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rs",
&trie_resource, &text, &text_len) == FAILURE) {
RETURN_FALSE;
}
array_init(return_value);
if (text_len < 1 || strlen(text) != text_len) {
php_error_docref(NULL TSRMLS_CC, E_NOTICE, "input is empty");
return;
}
trie = (Trie *)zend_fetch_resource(Z_RES_P(trie_resource), PHP_TRIE_FILTER_RES_NAME, le_trie_filter);
alpha_text = emalloc(sizeof(AlphaChar) * (text_len + 1));
for (i = 0; i < text_len; i++) {
alpha_text[i] = (AlphaChar) text[i];
}
alpha_text[text_len] = TRIE_CHAR_TERM;
ret = trie_search_all(trie, alpha_text, return_value);
efree(alpha_text);
if (ret == 0) {
return;
} else {
RETURN_FALSE;
}
}
/* }}} */
/* {{{ proto resource trie_filter_new()
Returns resource id, or NULL on error*/
PHP_FUNCTION(trie_filter_new)
{
Trie *trie;
AlphaMap *alpha_map;
int ret;
alpha_map = alpha_map_new();
if (! alpha_map) {
RETURN_NULL();
}
if (alpha_map_add_range(alpha_map, 0x00, 0xff) != 0) {
/* treat all strings as byte stream */
alpha_map_free(alpha_map);
RETURN_NULL();
}
trie = trie_new(alpha_map);
alpha_map_free(alpha_map);
if (! trie) {
RETURN_NULL();
}
RETURN_RES(zend_register_resource(trie, le_trie_filter));
}
/* }}} */
#define KEYWORD_MAX_LEN 1024
/* {{{ proto bool trie_filter_store(int trie_tree_identifier, string keyword)
Returns true, or false on error*/
PHP_FUNCTION(trie_filter_store)
{
Trie *trie;
zval *trie_resource;
unsigned char *keyword, *p;
size_t keyword_len, i;
AlphaChar alpha_key[KEYWORD_MAX_LEN+1];
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rs",
&trie_resource, &keyword, &keyword_len) == FAILURE) {
RETURN_FALSE;
}
if (keyword_len > KEYWORD_MAX_LEN || keyword_len < 1) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "keyword should has [1, %d] bytes", KEYWORD_MAX_LEN);
RETURN_FALSE;
}
trie = (Trie *)zend_fetch_resource(Z_RES_P(trie_resource), PHP_TRIE_FILTER_RES_NAME, le_trie_filter);
p = keyword;
i = 0;
while (*p && *p != '\n' && *p != '\r') {
alpha_key[i++] = (AlphaChar)*p;
p++;
}
alpha_key[i] = TRIE_CHAR_TERM;
if (! trie_store(trie, alpha_key, -1)) {
RETURN_FALSE;
}
RETURN_TRUE;
}
/* }}} */
/* {{{ proto bool trie_filter_save(int trie_tree_identifier, string dict_path)
Returns true, or false on error*/
PHP_FUNCTION(trie_filter_save)
{
Trie *trie;
zval *trie_resource;
unsigned char *filename;
int filename_len;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rs",
&trie_resource, &filename, &filename_len) == FAILURE) {
RETURN_FALSE;
}
if (filename_len < 1 || strlen(filename) != filename_len) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "save path required");
RETURN_FALSE;
}
trie = (Trie *)zend_fetch_resource(Z_RES_P(trie_resource), PHP_TRIE_FILTER_RES_NAME, le_trie_filter);
if (trie_save(trie, filename)) {
RETURN_FALSE;
}
RETURN_TRUE;
}
/* }}} */
/* {{{ proto bool trie_filter_free(int trie_tree_identifier)
Returns true, or false on error*/
PHP_FUNCTION(trie_filter_free)
{
zval *trie_resource;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &trie_resource) == FAILURE) {
RETURN_FALSE;
}
if (zend_list_close(Z_RES_P(trie_resource)) == SUCCESS) {
RETURN_TRUE;
}
RETURN_FALSE;
}
/* }}} */
/*
* Local variables:
* tab-width: 4
* c-basic-offset: 4
* End:
* vim600: noet sw=4 ts=4 fdm=marker
* vim<600: noet sw=4 ts=4
*/