📄 php_mbregex.c
字号:
} n = regs.end[0]; if (pos < n) { pos = n; } else { if (pos < string_len) { _php_mb_regex_strbuf_ncat(&outdev, (const unsigned char *)&string[pos], 1); } pos++; } } else { /* nomatch */ /* stick that last bit of string on our output */ if (pos < string_len) { _php_mb_regex_strbuf_ncat(&outdev, (const unsigned char *)&string[pos], string_len - pos); } } } if (description) { efree(description); } mbre_free_registers(®s); if (evaldev.buffer) { efree((void*)evaldev.buffer); } n = outdev.pos; _php_mb_regex_strbuf_ncat(&outdev, (const unsigned char *)"\0", 1); if (err <= -2) { if (outdev.buffer) { efree((void*)outdev.buffer); } RETVAL_FALSE; } else { RETVAL_STRINGL((char *)outdev.buffer, n, 0); }}/* }}} *//* {{{ proto string mb_ereg_replace(string pattern, string replacement, string string [, string option]) Replace regular expression for multibyte string */PHP_FUNCTION(mb_ereg_replace){ _php_mb_regex_ereg_replace_exec(INTERNAL_FUNCTION_PARAM_PASSTHRU, 0);}/* }}} *//* {{{ proto string mb_eregi_replace(string pattern, string replacement, string string) Case insensitive replace regular expression for multibyte string */PHP_FUNCTION(mb_eregi_replace){ _php_mb_regex_ereg_replace_exec(INTERNAL_FUNCTION_PARAM_PASSTHRU, MBRE_OPTION_IGNORECASE);}/* }}} *//* {{{ proto array mb_split(string pattern, string string [, int limit]) split multibyte string into array by regular expression */PHP_FUNCTION(mb_split){ char *arg_pattern; int arg_pattern_len; mb_regex_t re; struct mbre_registers regs = {0, 0, 0, 0}; char *string; int string_len; int n, err, pos; long count = -1; if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ss|l", &arg_pattern, &arg_pattern_len, &string, &string_len, &count) == FAILURE) { RETURN_FALSE; } if (count == 0) { count = 1; } if (array_init(return_value) == FAILURE) { RETURN_FALSE; } /* create regex pattern buffer */ err = php_mbregex_compile_pattern( &re, arg_pattern, arg_pattern_len, MBSTRG(regex_default_options), MBSTRG(current_mbctype) TSRMLS_CC); if (err) { RETURN_FALSE; } pos = 0; err = 0; /* churn through str, generating array entries as we go */ while ((--count != 0) && (err = mbre_search(&re, string, string_len, pos, string_len - pos, ®s)) >= 0) { if ( regs.beg[0] == regs.end[0] ) { php_error_docref(NULL TSRMLS_CC, E_WARNING, "Empty regular expression"); break; } /* add it to the array */ if ( regs.beg[0] < string_len && regs.beg[0] >= pos) { add_next_index_stringl(return_value, (char *)&string[pos], regs.beg[0]-pos, 1); } else { err = -2; break; } /* point at our new starting point */ n = regs.end[0]; if (pos < n) { pos = n; } if (count < 0) { count = 0; } } mbre_free_registers(®s); /* see if we encountered an error */ if (err <= -2) { php_error_docref(NULL TSRMLS_CC, E_WARNING, "mbregex search failure in mbsplit()"); zval_dtor(return_value); RETURN_FALSE; } /* otherwise we just have one last element to add to the array */ n = string_len - pos; if (n > 0) { add_next_index_stringl(return_value, (char *)&string[pos], n, 1); } else { add_next_index_stringl(return_value, (char *)empty_string, 0, 1); }}/* }}} *//* {{{ proto bool mb_ereg_match(string pattern, string string [,string option]) Regular expression match for multibyte string */PHP_FUNCTION(mb_ereg_match){ char *arg_pattern; int arg_pattern_len; char *string; int string_len; mb_regex_t re; int option, err; option = 0; { char *option_str = NULL; int option_str_len = 0; if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ss|s", &arg_pattern, &arg_pattern_len, &string, &string_len, &option_str, &option_str_len)==FAILURE) { RETURN_FALSE; } if (option_str != NULL) { _php_mb_regex_init_options(option_str, option_str_len, &option, NULL); } else { option |= MBSTRG(regex_default_options); } } err = php_mbregex_compile_pattern( &re, arg_pattern, arg_pattern_len, option, MBSTRG(current_mbctype) TSRMLS_CC); if (err) { RETURN_FALSE; } /* match */ err = mbre_match(&re, string, string_len, 0, NULL); if (err >= 0) { RETVAL_TRUE; } else { RETVAL_FALSE; }}/* }}} *//* regex search */static void_php_mb_regex_ereg_search_exec(INTERNAL_FUNCTION_PARAMETERS, int mode){ zval **arg_pattern, **arg_options; int n, i, err, pos, len, beg, end, option; unsigned char *str; option = MBSTRG(regex_default_options); switch (ZEND_NUM_ARGS()) { case 0: break; case 1: if (zend_get_parameters_ex(1, &arg_pattern) == FAILURE) { WRONG_PARAM_COUNT; } break; case 2: if (zend_get_parameters_ex(2, &arg_pattern, &arg_options) == FAILURE) { WRONG_PARAM_COUNT; } convert_to_string_ex(arg_options); option = 0; _php_mb_regex_init_options(Z_STRVAL_PP(arg_options), Z_STRLEN_PP(arg_options), &option, NULL); break; default: WRONG_PARAM_COUNT; break; } if (ZEND_NUM_ARGS() > 0) { /* create regex pattern buffer */ convert_to_string_ex(arg_pattern); if (!MBSTRG(search_re)) { MBSTRG(search_re) = (mb_regex_t*)ecalloc(1, sizeof(mb_regex_t)); } err = php_mbregex_compile_pattern( MBSTRG(search_re), Z_STRVAL_PP(arg_pattern), Z_STRLEN_PP(arg_pattern), option, MBSTRG(current_mbctype) TSRMLS_CC); if (err) { efree(MBSTRG(search_re)); MBSTRG(search_re) = (mb_regex_t*)0; RETURN_FALSE; } } pos = MBSTRG(search_pos); str = NULL; len = 0; if (MBSTRG(search_str) != NULL && Z_TYPE_PP(MBSTRG(search_str)) == IS_STRING){ str = (unsigned char *)Z_STRVAL_PP(MBSTRG(search_str)); len = Z_STRLEN_PP(MBSTRG(search_str)); } if (!MBSTRG(search_re)) { php_error_docref(NULL TSRMLS_CC, E_WARNING, "No regex given"); RETURN_FALSE; } if (!str) { php_error_docref(NULL TSRMLS_CC, E_WARNING, "No string given"); RETURN_FALSE; } if (MBSTRG(search_regs)) { mbre_free_registers(MBSTRG(search_regs)); memset(MBSTRG(search_regs), 0, sizeof(struct mbre_registers)); } else { MBSTRG(search_regs) = (struct mbre_registers*)ecalloc(1, sizeof(struct mbre_registers)); } err = mbre_search(MBSTRG(search_re), (const char *)str, len, pos, len - pos, MBSTRG(search_regs)); if (err <= -2) { php_error_docref(NULL TSRMLS_CC, E_WARNING, "mbregex search failure in mbregex_search()"); RETVAL_FALSE; } else if (err < 0) { MBSTRG(search_pos) = len; RETVAL_FALSE; } else { if (MBSTRG(search_regs)->beg[0] == MBSTRG(search_regs)->end[0]) { php_error_docref(NULL TSRMLS_CC, E_WARNING, "Empty regular expression"); } switch (mode) { case 1: if (array_init(return_value) != FAILURE) { beg = MBSTRG(search_regs)->beg[0]; end = MBSTRG(search_regs)->end[0]; add_next_index_long(return_value, beg); add_next_index_long(return_value, end - beg); } else { RETVAL_FALSE; } break; case 2: if (array_init(return_value) != FAILURE) { n = MBSTRG(search_regs)->num_regs; for (i = 0; i < n; i++) { beg = MBSTRG(search_regs)->beg[i]; end = MBSTRG(search_regs)->end[i]; if (beg >= 0 && beg <= end && end <= len) { add_index_stringl(return_value, i, (char *)&str[beg], end - beg, 1); } else { add_index_bool(return_value, i, 0); } } } else { RETVAL_FALSE; } break; default: RETVAL_TRUE; break; } end = MBSTRG(search_regs)->end[0]; if (pos < end) { MBSTRG(search_pos) = end; } else { MBSTRG(search_pos) = pos + 1; } } if (err < 0) { mbre_free_registers(MBSTRG(search_regs)); efree(MBSTRG(search_regs)); MBSTRG(search_regs) = (struct mbre_registers*)0; }}/* }}} *//* {{{ proto bool mb_ereg_search([string pattern[, string option]]) Regular expression search for multibyte string */PHP_FUNCTION(mb_ereg_search){ _php_mb_regex_ereg_search_exec(INTERNAL_FUNCTION_PARAM_PASSTHRU, 0);}/* }}} *//* {{{ proto array mb_ereg_search_pos([string pattern[, string option]]) Regular expression search for multibyte string */PHP_FUNCTION(mb_ereg_search_pos){ _php_mb_regex_ereg_search_exec(INTERNAL_FUNCTION_PARAM_PASSTHRU, 1);}/* }}} *//* {{{ proto array mb_ereg_search_regs([string pattern[, string option]]) Regular expression search for multibyte string */PHP_FUNCTION(mb_ereg_search_regs){ _php_mb_regex_ereg_search_exec(INTERNAL_FUNCTION_PARAM_PASSTHRU, 2);}/* }}} *//* {{{ proto bool mb_ereg_search_init(string string [, string pattern[, string option]]) Initialize string and regular expression for search. */PHP_FUNCTION(mb_ereg_search_init){ zval **arg_str, **arg_pattern, **arg_options; int err, option; option = MBSTRG(regex_default_options); switch (ZEND_NUM_ARGS()) { case 1: if (zend_get_parameters_ex(1, &arg_str) == FAILURE) { WRONG_PARAM_COUNT; } break; case 2: if (zend_get_parameters_ex(2, &arg_str, &arg_pattern) == FAILURE) { WRONG_PARAM_COUNT; } break; case 3: if (zend_get_parameters_ex(3, &arg_str, &arg_pattern, &arg_options) == FAILURE) { WRONG_PARAM_COUNT; } convert_to_string_ex(arg_options); option = 0; _php_mb_regex_init_options(Z_STRVAL_PP(arg_options), Z_STRLEN_PP(arg_options), &option, NULL); break; default: WRONG_PARAM_COUNT; break; } if (ZEND_NUM_ARGS() > 1) { /* create regex pattern buffer */ convert_to_string_ex(arg_pattern); if (!MBSTRG(search_re)) { MBSTRG(search_re) = (mb_regex_t*)ecalloc(1, sizeof(mb_regex_t)); } err = php_mbregex_compile_pattern( MBSTRG(search_re), Z_STRVAL_PP(arg_pattern), Z_STRLEN_PP(arg_pattern), option, MBSTRG(current_mbctype) TSRMLS_CC); if (err) { efree(MBSTRG(search_re)); MBSTRG(search_re) = (mb_regex_t*)0; RETURN_FALSE; } } if (MBSTRG(search_str)) { if (ZVAL_REFCOUNT(*MBSTRG(search_str)) > 1) { ZVAL_DELREF(*MBSTRG(search_str)); } else { zval_dtor(*MBSTRG(search_str)); FREE_ZVAL(*MBSTRG(search_str)); } MBSTRG(search_str) = (zval **)0; MBSTRG(search_str_val) = (zval *)0; } if (PZVAL_IS_REF(*arg_str)) { ZVAL_ADDREF(*arg_str); MBSTRG(search_str_val) = *arg_str; MBSTRG(search_str) = &MBSTRG(search_str_val); } else { MAKE_STD_ZVAL(MBSTRG(search_str_val)); *MBSTRG(search_str_val) = **arg_str; zval_copy_ctor(MBSTRG(search_str_val)); MBSTRG(search_str_val)->refcount = 1; MBSTRG(search_str_val)->is_ref = 0; MBSTRG(search_str) = &MBSTRG(search_str_val); convert_to_string_ex(MBSTRG(search_str)); } MBSTRG(search_pos) = 0; if (MBSTRG(search_regs)) { mbre_free_registers(MBSTRG(search_regs)); efree(MBSTRG(search_regs)); MBSTRG(search_regs) = (struct mbre_registers*)0; } RETURN_TRUE;}/* }}} *//* {{{ proto array mb_ereg_search_getregs(void) Get matched substring of the last time */PHP_FUNCTION(mb_ereg_search_getregs){ int n, i, len, beg, end; unsigned char *str; if (MBSTRG(search_regs) && Z_TYPE_PP(MBSTRG(search_str)) == IS_STRING && Z_STRVAL_PP(MBSTRG(search_str)) && array_init(return_value) != FAILURE) { str = (unsigned char *)Z_STRVAL_PP(MBSTRG(search_str)); len = Z_STRLEN_PP(MBSTRG(search_str)); n = MBSTRG(search_regs)->num_regs; for (i = 0; i < n; i++) { beg = MBSTRG(search_regs)->beg[i]; end = MBSTRG(search_regs)->end[i]; if (beg >= 0 && beg <= end && end <= len) { add_index_stringl(return_value, i, (char *)&str[beg], end - beg, 1); } else { add_index_bool(return_value, i, 0); } } } else { RETVAL_FALSE; }}/* }}} *//* {{{ proto int mb_ereg_search_getpos(void) Get search start position */PHP_FUNCTION(mb_ereg_search_getpos){ RETVAL_LONG(MBSTRG(search_pos));}/* }}} *//* {{{ proto bool mb_ereg_search_setpos(int position) Set search start position */PHP_FUNCTION(mb_ereg_search_setpos){ zval **arg_pos; int n; if (ZEND_NUM_ARGS() != 1 || zend_get_parameters_ex(1, &arg_pos) == FAILURE) { WRONG_PARAM_COUNT; } convert_to_long_ex(arg_pos); n = Z_LVAL_PP(arg_pos); if (n < 0 || ( MBSTRG(search_str) != NULL && *MBSTRG(search_str) != NULL && Z_TYPE_PP(MBSTRG(search_str)) == IS_STRING && n >= Z_STRLEN_PP(MBSTRG(search_str)) ) ) { php_error_docref(NULL TSRMLS_CC, E_WARNING, "Position is out of range"); MBSTRG(search_pos) = 0; RETVAL_FALSE; } else { MBSTRG(search_pos) = n; RETVAL_TRUE; }}/* }}} *//* {{{ php_mb_regex_set_options */int php_mb_regex_set_options( int options TSRMLS_DC) { int prev_opt = MBSTRG(regex_default_options); MBSTRG(regex_default_options) = options; return prev_opt;}/* }}} *//* {{{ php_mb_regex_set_options_by_string */int php_mb_regex_set_options_by_string( const char *opt_str, int len TSRMLS_DC){ int new_opt = 0; _php_mb_regex_init_options( opt_str, len, &new_opt, NULL); return php_mb_regex_set_options( new_opt TSRMLS_CC);}/* }}} *//* {{{ proto string mb_regex_set_options([string options]) Set or get the default options for mbregex functions */PHP_FUNCTION(mb_regex_set_options){ int opt; char *string = NULL; int string_len; char buf[16]; if ( zend_parse_parameters( ZEND_NUM_ARGS() TSRMLS_CC, "|s", &string, &string_len ) == FAILURE ) { RETURN_FALSE; } if (string != NULL) { opt = php_mb_regex_set_options_by_string( (const char*)string, string_len TSRMLS_CC ); } else { opt = MBSTRG(regex_default_options); } _php_mb_regex_get_option_string(buf, sizeof(buf), opt); RETVAL_STRING(buf, 1);}/* }}} */#endif /* HAVE_MBREGEX *//* * Local variables: * tab-width: 4 * c-basic-offset: 4 * End: * vim600: fdm=marker * vim: noet sw=4 ts=4 */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -