Skip to content

Update for PHP 7 + tiny optimization #1

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
45 changes: 31 additions & 14 deletions stem.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,10 @@
#include "php.h"
#include "php_ini.h"
#include "ext/standard/info.h"
#include "ext/standard/php_string.h"
#include "php_stem.h"


/* {{{ stem_functions[]
*/
zend_function_entry stem_functions[] = {
Expand All @@ -45,7 +47,7 @@ zend_module_entry stem_module_entry = {
STANDARD_MODULE_HEADER,
"stem",
stem_functions,
NULL,
PHP_MINIT(stem),
NULL,
NULL,
NULL,
Expand Down Expand Up @@ -76,6 +78,20 @@ PHP_MINFO_FUNCTION(stem)
}
/* }}} */

/* {{{ PHP_MINIT_FUNCTION
*/
PHP_MINIT_FUNCTION(stem)
{
long stemmer_id = 0;

#define STEMMER(php_func, c_func, constant, name) \
REGISTER_LONG_CONSTANT("STEM_" # constant, stemmer_id++, CONST_CS|CONST_PERSISTENT);
#include "stemmers.def"
#undef STEMMER
}

/* }}} */

/* {{{ string stem(string word, string algo)
word is a string to be stemmed, algo is the stemming algorithm to be used.
Returns false on Snowball error or the stemmed word on success.
Expand All @@ -86,20 +102,20 @@ PHP_FUNCTION(stem)
struct SN_env* (*create_env)(void);
void (*close_env)(struct SN_env*);
int (*stem)(struct SN_env*);
long stemmer_id = 0;

char * word;
char * algo;
int word_len;
int algo_len;
zend_string* word;
long algo_id;

short int algo_found = 0;

if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ss", &algo, &algo_len, &word, &word_len) == FAILURE) {
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "Sl", &word, &algo_id) == FAILURE) {
return;
}

/* Empty string */
if (word_len <= 0) {
RETURN_STRINGL(word, word_len, 1);
if (word->len <= 0) {
RETURN_STRINGL(word->val, word->len);
}

#ifdef PHP_WIN32
Expand All @@ -114,10 +130,11 @@ PHP_FUNCTION(stem)
close_env = x ## _close_env;
#endif
#define STEMMER(php_func, c_func, constant, name) \
if (strcmp(name, algo) == 0) { \
if (stemmer_id == algo_id) { \
INIT_FUNCS(c_func) \
algo_found = 1; \
}
} \
stemmer_id++;
#include "stemmers.def"
#undef STEMMER
#undef INIT_FUNCS
Expand All @@ -129,12 +146,12 @@ PHP_FUNCTION(stem)
}

z = create_env();
SN_set_current(z, word_len, word);
php_strtolower(z->p, word_len);
SN_set_current(z, word->len, word->val);
php_strtolower(z->p, word->len);
stem(z);
z->p[z->l]= '\0';

RETVAL_STRINGL(z->p, z->l, 1);
RETVAL_STRINGL(z->p, z->l);
close_env(z);
}
/* }}} */
Expand All @@ -145,7 +162,7 @@ PHP_FUNCTION(stem_algos)
{
array_init(return_value);
# define STEMMER(php_func, c_func, constant, name) \
add_next_index_stringl(return_value, name, sizeof(name)-1, 1);
add_next_index_stringl(return_value, name, sizeof(name)-1);
# include "stemmers.def"
# undef STEMMER
}
Expand Down