-
Notifications
You must be signed in to change notification settings - Fork 169
/
trie_filter.c
615 lines (528 loc) · 15 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
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
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
/*
+----------------------------------------------------------------------+
| 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"
#include "php7_wrapper.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_read, 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_delete, NULL)
PHP_FE(trie_filter_save, NULL)
PHP_FE(trie_filter_write, 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()
*/
/* }}} */
#if PHP_MAJOR_VERSION < 7
static void php_trie_filter_dtor(zend_rsrc_list_entry *rsrc TSRMLS_DC)
#else
static void php_trie_filter_dtor(zend_resource *rsrc TSRMLS_DC)
#endif
{
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;
zend_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();
}
TRIE_ZEND_REGISTER_RESOURCE(return_value, trie, le_trie_filter);
}
/* }}} */
/* {{{ proto resource trie_filter_read(string dict_bin)
Returns resource id, or NULL on error*/
PHP_FUNCTION(trie_filter_read)
{
Trie *trie;
char *path;
zend_size_t path_len;
FILE *fp;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &path, &path_len) == FAILURE) {
RETURN_NULL();
}
fp = fmemopen(path, path_len, "rb");
trie = trie_fread(fp);
fclose(fp);
if (!trie) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to read");
RETURN_NULL();
}
TRIE_ZEND_REGISTER_RESOURCE(return_value, 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;
#if PHP_MAJOR_VERSION < 7
zval *word = NULL;
#else
zval word;
#endif
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)) {
#if PHP_MAJOR_VERSION < 7
MAKE_STD_ZVAL(word);
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);
#else
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);
#endif
}
}
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;
#if PHP_MAJOR_VERSION < 7
int text_len;
#else
size_t text_len;
#endif
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;
}
#if PHP_MAJOR_VERSION < 7
ZEND_FETCH_RESOURCE(trie, Trie *, &trie_resource, -1,
PHP_TRIE_FILTER_RES_NAME, le_trie_filter);
#else
trie = zend_fetch_resource(Z_RES_P(trie_resource), PHP_TRIE_FILTER_RES_NAME, le_trie_filter);
#endif
if (trie == NULL) {
RETURN_FALSE;
}
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;
#if PHP_MAJOR_VERSION < 7
int text_len;
#else
size_t text_len;
#endif
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;
}
#if PHP_MAJOR_VERSION < 7
ZEND_FETCH_RESOURCE(trie, Trie *, &trie_resource, -1,
PHP_TRIE_FILTER_RES_NAME, le_trie_filter);
#else
trie = (Trie*) zend_fetch_resource(Z_RES_P(trie_resource), PHP_TRIE_FILTER_RES_NAME, le_trie_filter);
#endif
if (trie == NULL) {
RETURN_FALSE;
}
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();
}
#if PHP_MAJOR_VERSION < 7
ZEND_REGISTER_RESOURCE(return_value, trie, le_trie_filter);
#else
RETURN_RES(zend_register_resource(trie, le_trie_filter));
#endif
}
/* }}} */
#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;
#if PHP_MAJOR_VERSION < 7
int keyword_len, i;
#else
size_t keyword_len;
int i;
#endif
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;
}
#if PHP_MAJOR_VERSION < 7
ZEND_FETCH_RESOURCE(trie, Trie *, &trie_resource, -1, PHP_TRIE_FILTER_RES_NAME, le_trie_filter);
#else
trie = zend_fetch_resource(Z_RES_P(trie_resource), PHP_TRIE_FILTER_RES_NAME, le_trie_filter);
#endif
if (trie == NULL) {
RETURN_FALSE;
}
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_delete(int trie_tree_identifier, string keyword)
Returns true, or false on error*/
PHP_FUNCTION(trie_filter_delete)
{
Trie *trie;
zval *trie_resource;
unsigned char *keyword, *p;
zend_size_t keyword_len;
int 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_ZEND_FETCH_RESOURCE(trie, Trie *, &trie_resource, -1, PHP_TRIE_FILTER_RES_NAME, le_trie_filter);
if (trie == NULL) {
RETURN_FALSE;
}
p = keyword;
i = 0;
while (*p && *p != '\n' && *p != '\r') {
alpha_key[i++] = (AlphaChar)*p;
p++;
}
alpha_key[i] = TRIE_CHAR_TERM;
if (! trie_delete(trie, alpha_key)) {
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;
#if PHP_MAJOR_VERSION < 7
int filename_len;
#else
size_t filename_len;
#endif
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rs",
&trie_resource, &filename, &filename_len) == FAILURE) {
RETURN_FALSE;
}
if (filename_len < 1) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "save path required");
RETURN_FALSE;
}
#if PHP_MAJOR_VERSION < 7
ZEND_FETCH_RESOURCE(trie, Trie *, &trie_resource, -1, PHP_TRIE_FILTER_RES_NAME, le_trie_filter);
#else
trie = zend_fetch_resource(Z_RES_P(trie_resource), PHP_TRIE_FILTER_RES_NAME, le_trie_filter);
#endif
if (trie == NULL) {
RETURN_FALSE;
}
if (trie_save(trie, filename)) {
RETURN_FALSE;
}
RETURN_TRUE;
}
/* }}} */
/* {{{ proto string trie_filter_write(int trie_tree_identifier)
Returns true, or false on error*/
PHP_FUNCTION(trie_filter_write)
{
Trie *trie;
zval *trie_resource;
FILE *fp;
int res = 0;
char *p;
zend_size_t plen;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &trie_resource) == FAILURE) {
RETURN_FALSE;
}
TRIE_ZEND_FETCH_RESOURCE(trie, Trie *, &trie_resource, -1, PHP_TRIE_FILTER_RES_NAME, le_trie_filter);
if (trie == NULL) {
RETURN_FALSE;
}
fp = open_memstream(&p, &plen);
res = trie_fwrite(trie, fp);
fclose(fp);
if (res) {
RETURN_FALSE;
}
RETVAL_STRINGL(p, plen);
free(p);
}
/* }}} */
/* {{{ proto bool trie_filter_free(int trie_tree_identifier)
Returns true, or false on error*/
PHP_FUNCTION(trie_filter_free)
{
Trie *trie;
zval *trie_resource;
#if PHP_MAJOR_VERSION < 7
int resource_id;
#endif
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &trie_resource) == FAILURE) {
RETURN_FALSE;
}
#if PHP_MAJOR_VERSION < 7
ZEND_FETCH_RESOURCE(trie, Trie *, &trie_resource, -1, PHP_TRIE_FILTER_RES_NAME, le_trie_filter);
resource_id = Z_RESVAL_P(trie_resource);
#else
trie = zend_fetch_resource(Z_RES_P(trie_resource), PHP_TRIE_FILTER_RES_NAME, le_trie_filter);
#endif
if (trie == NULL) {
RETURN_FALSE;
}
#if PHP_MAJOR_VERSION < 7
if (zend_list_delete(resource_id) == SUCCESS) {
#else
if (zend_list_close(Z_RES_P(trie_resource)) == SUCCESS) {
#endif
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
*/