summaryrefslogtreecommitdiff
path: root/ext/intl/grapheme/grapheme_string.c
diff options
context:
space:
mode:
Diffstat (limited to 'ext/intl/grapheme/grapheme_string.c')
-rw-r--r--ext/intl/grapheme/grapheme_string.c42
1 files changed, 24 insertions, 18 deletions
diff --git a/ext/intl/grapheme/grapheme_string.c b/ext/intl/grapheme/grapheme_string.c
index b5a1902025..0735a7e822 100644
--- a/ext/intl/grapheme/grapheme_string.c
+++ b/ext/intl/grapheme/grapheme_string.c
@@ -110,7 +110,7 @@ PHP_FUNCTION(grapheme_strpos)
size_t haystack_len, needle_len;
const char *found;
zend_long loffset = 0;
- int32_t offset = 0;
+ int32_t offset = 0, noffset = 0;
zend_long ret_pos;
if (zend_parse_parameters(ZEND_NUM_ARGS(), "ss|l", &haystack, &haystack_len, &needle, &needle_len, &loffset) == FAILURE) {
@@ -126,6 +126,7 @@ PHP_FUNCTION(grapheme_strpos)
/* we checked that it will fit: */
offset = (int32_t) loffset;
+ noffset = offset >= 0 ? offset : haystack_len + offset;
/* the offset is 'grapheme count offset' so it still might be invalid - we'll check it later */
@@ -134,20 +135,21 @@ PHP_FUNCTION(grapheme_strpos)
RETURN_FALSE;
}
+ if (offset >= 0) {
+ /* quick check to see if the string might be there
+ * I realize that 'offset' is 'grapheme count offset' but will work in spite of that
+ */
+ found = php_memnstr(haystack + noffset, needle, needle_len, haystack + haystack_len);
- /* quick check to see if the string might be there
- * I realize that 'offset' is 'grapheme count offset' but will work in spite of that
- */
- found = php_memnstr(haystack + offset, needle, needle_len, haystack + haystack_len);
-
- /* if it isn't there the we are done */
- if (!found) {
- RETURN_FALSE;
- }
+ /* if it isn't there the we are done */
+ if (!found) {
+ RETURN_FALSE;
+ }
- /* if it is there, and if the haystack is ascii, we are all done */
- if ( grapheme_ascii_check((unsigned char *)haystack, haystack_len) >= 0 ) {
- RETURN_LONG(found - haystack);
+ /* if it is there, and if the haystack is ascii, we are all done */
+ if ( grapheme_ascii_check((unsigned char *)haystack, haystack_len) >= 0 ) {
+ RETURN_LONG(found - haystack);
+ }
}
/* do utf16 part of the strpos */
@@ -195,16 +197,16 @@ PHP_FUNCTION(grapheme_stripos)
RETURN_FALSE;
}
-
is_ascii = ( grapheme_ascii_check((unsigned char*)haystack, haystack_len) >= 0 );
if ( is_ascii ) {
+ int32_t noffset = offset >= 0 ? offset : haystack_len + offset;
needle_dup = estrndup(needle, needle_len);
php_strtolower(needle_dup, needle_len);
haystack_dup = estrndup(haystack, haystack_len);
php_strtolower(haystack_dup, haystack_len);
- found = php_memnstr(haystack_dup + offset, needle_dup, needle_len, haystack_dup + haystack_len);
+ found = php_memnstr(haystack_dup + noffset, needle_dup, needle_len, haystack_dup + haystack_len);
efree(haystack_dup);
efree(needle_dup);
@@ -383,9 +385,9 @@ PHP_FUNCTION(grapheme_substr)
UBreakIterator* bi = NULL;
int sub_str_start_pos, sub_str_end_pos;
int32_t (*iter_func)(UBreakIterator *);
- int no_length = 1;
+ zend_bool no_length = 1;
- if (zend_parse_parameters(ZEND_NUM_ARGS(), "sl|l!", (char **)&str, &str_len, &lstart, &length, &no_length) == FAILURE) {
+ if (zend_parse_parameters(ZEND_NUM_ARGS(), "sl|l!", &str, &str_len, &lstart, &length, &no_length) == FAILURE) {
intl_error_set( NULL, U_ILLEGAL_ARGUMENT_ERROR,
"grapheme_substr: unable to parse input param", 0 );
RETURN_FALSE;
@@ -802,6 +804,10 @@ PHP_FUNCTION(grapheme_extract)
RETURN_FALSE;
}
+ if (lstart < 0) {
+ lstart += str_len;
+ }
+
if ( NULL != next ) {
if ( !Z_ISREF_P(next) ) {
intl_error_set( NULL, U_ILLEGAL_ARGUMENT_ERROR,
@@ -810,7 +816,7 @@ PHP_FUNCTION(grapheme_extract)
} else {
ZVAL_DEREF(next);
/* initialize next */
- SEPARATE_ZVAL(next);
+ SEPARATE_ZVAL_NOREF(next);
zval_dtor(next);
ZVAL_LONG(next, lstart);
}