Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

支持PHP7 #7

Merged
merged 2 commits into from
Feb 13, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
137 changes: 114 additions & 23 deletions trie_filter.c
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,11 @@ PHP_INI_BEGIN()
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);
Expand All @@ -90,7 +93,7 @@ static void php_trie_filter_dtor(zend_rsrc_list_entry *rsrc TSRMLS_DC)
PHP_MINIT_FUNCTION(trie_filter)
{
le_trie_filter = zend_register_list_destructors_ex(
php_trie_filter_dtor,
php_trie_filter_dtor,
NULL, PHP_TRIE_FILTER_RES_NAME, module_number);
return SUCCESS;
}
Expand Down Expand Up @@ -120,21 +123,28 @@ PHP_FUNCTION(trie_filter_load)
{
Trie *trie;
char *path;
#if PHP_MAJOR_VERSION < 7
int path_len;
#else
size_t path_len;
#endif

if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s",
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,
php_error_docref(NULL TSRMLS_CC, E_WARNING,
"Unable to load %s", path);
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
}
/* }}} */

Expand All @@ -149,7 +159,7 @@ static int trie_search_one(Trie *trie, const AlphaChar *text, int *offset, TrieD
return -1;
}

while (*text) {
while (*text) {
p = text;
if (! trie_state_is_walkable(s, *p)) {
trie_state_rewind(s);
Expand All @@ -166,7 +176,7 @@ static int trie_search_one(Trie *trie, const AlphaChar *text, int *offset, TrieD
*offset = text - base;
*length = p - text;
trie_state_free(s);

return 1;
}

Expand All @@ -183,14 +193,18 @@ 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) {
while (*text) {
p = text;
if(! trie_state_is_walkable(s, *p)) {
trie_state_rewind(s);
Expand All @@ -199,14 +213,21 @@ static int trie_search_all(Trie *trie, const AlphaChar *text, zval *data)
}

while(*p && trie_state_is_walkable(s, *p) && ! trie_state_is_leaf(s)) {
trie_state_walk(s, *p++);
if (trie_state_is_terminal(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);
}
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++;
Expand All @@ -223,14 +244,18 @@ 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",
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rs",
&trie_resource, &text, &text_len) == FAILURE) {
RETURN_FALSE;
}
Expand All @@ -241,8 +266,16 @@ PHP_FUNCTION(trie_filter_search)
return;
}

ZEND_FETCH_RESOURCE(trie, Trie *, &trie_resource, -1,
#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));

Expand Down Expand Up @@ -272,13 +305,17 @@ 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",
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rs",
&trie_resource, &text, &text_len) == FAILURE) {
RETURN_FALSE;
}
Expand All @@ -289,8 +326,15 @@ PHP_FUNCTION(trie_filter_search_all)
return;
}

ZEND_FETCH_RESOURCE(trie, Trie *, &trie_resource, -1,
#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));

Expand Down Expand Up @@ -331,10 +375,14 @@ PHP_FUNCTION(trie_filter_new)

trie = trie_new(alpha_map);
alpha_map_free(alpha_map);
if (! trie) {
if (! trie) {
RETURN_NULL();
}
ZEND_REGISTER_RESOURCE(return_value, trie, le_trie_filter);
#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
}
/* }}} */

Expand All @@ -346,25 +394,40 @@ 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",
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;
}
Expand All @@ -379,17 +442,30 @@ 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",
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;
}
ZEND_FETCH_RESOURCE(trie, Trie *, &trie_resource, -1, PHP_TRIE_FILTER_RES_NAME, le_trie_filter);
#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;
}
Expand All @@ -403,17 +479,32 @@ 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;
}
ZEND_FETCH_RESOURCE(trie, Trie *, &trie_resource, -1, PHP_TRIE_FILTER_RES_NAME, le_trie_filter);
#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;
RETURN_FALSE;
}
/* }}} */

Expand Down