RETURN_THROWS();
}
- if (offset >= 0) {
+ if (offset >= 0 && grapheme_ascii_check((unsigned char *)haystack, haystack_len) >= 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);
/* 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 ) {
+ if (found) {
RETURN_LONG(found - haystack);
}
+ RETURN_FALSE;
}
/* do utf16 part of the strpos */
if ( !f_ignore_case ) {
- /* ASCII optimization: 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
- */
+ /* ASCII optimization: quick check to see if the string might be there */
found = php_memnstr(haystack, needle, needle_len, haystack + haystack_len);
/* if it isn't there the we are done */
}
/* }}} */
-#define STRPOS_CHECK_STATUS(status, error) \
- if ( U_FAILURE( (status) ) ) { \
- intl_error_set_code( NULL, (status) ); \
- intl_error_set_custom_msg( NULL, (error), 0 ); \
- if (uhaystack) { \
- efree( uhaystack ); \
- } \
- if (uneedle) { \
- efree( uneedle ); \
- } \
- if(bi) { \
- ubrk_close (bi); \
- } \
- if(src) { \
- usearch_close(src); \
- } \
- return -1; \
+#define STRPOS_CHECK_STATUS(status, error) \
+ if ( U_FAILURE( (status) ) ) { \
+ intl_error_set_code( NULL, (status) ); \
+ intl_error_set_custom_msg( NULL, (error), 0 ); \
+ ret_pos = -1; \
+ goto finish; \
}
if(offset != 0) {
offset_pos = grapheme_get_haystack_offset(bi, offset);
- if(offset_pos == -1) {
- status = U_ILLEGAL_ARGUMENT_ERROR;
- STRPOS_CHECK_STATUS(status, "Invalid search offset");
+ if (offset_pos == -1) {
+ zend_argument_value_error(3, "must be contained in argument #1 ($haystack)");
+ ret_pos = -1;
+ goto finish;
}
status = U_ZERO_ERROR;
usearch_setOffset(src, offset_pos, &status);
ret_pos = -1;
}
+finish:
if (uhaystack) {
efree( uhaystack );
}
if (uneedle) {
efree( uneedle );
}
- ubrk_close (bi);
- usearch_close (src);
+ if (bi) {
+ ubrk_close (bi);
+ }
+ if (src) {
+ usearch_close (src);
+ }
return ret_pos;
}
var_dump(grapheme_stripos("foo", "bar", 3));
var_dump(grapheme_strrpos("foo", "bar", 3));
var_dump(grapheme_strripos("foo", "bar", 3));
+var_dump(grapheme_strpos("äöü", "bar", 3));
+var_dump(grapheme_stripos("äöü", "bar", 3));
+var_dump(grapheme_strrpos("äöü", "bar", 3));
+var_dump(grapheme_strripos("äöü", "bar", 3));
echo "\n";
// Offset == -Length is legal.
var_dump(grapheme_stripos("foo", "bar", -3));
var_dump(grapheme_strrpos("foo", "bar", -3));
var_dump(grapheme_strripos("foo", "bar", -3));
+var_dump(grapheme_strpos("äöü", "bar", -3));
+var_dump(grapheme_stripos("äöü", "bar", -3));
+var_dump(grapheme_strrpos("äöü", "bar", -3));
+var_dump(grapheme_strripos("äöü", "bar", -3));
+echo "\n";
+
+// Offset == Length is legal.
+var_dump(grapheme_strpos("", "bar", 0));
+var_dump(grapheme_stripos("", "bar", 0));
+var_dump(grapheme_strrpos("", "bar", 0));
+var_dump(grapheme_strripos("", "bar", 0));
echo "\n";
// Positive out of bounds.
} catch (ValueError $e) {
echo $e->getMessage(), "\n";
}
+try {
+ var_dump(grapheme_strpos("äöü", "bar", 4));
+} catch (ValueError $e) {
+ echo $e->getMessage(), "\n";
+}
+try {
+ var_dump(grapheme_stripos("äöü", "bar", 4));
+} catch (ValueError $e) {
+ echo $e->getMessage(), "\n";
+}
+try {
+ var_dump(grapheme_strrpos("äöü", "bar", 4));
+} catch (ValueError $e) {
+ echo $e->getMessage(), "\n";
+}
+try {
+ var_dump(grapheme_strripos("äöü", "bar", 4));
+} catch (ValueError $e) {
+ echo $e->getMessage(), "\n";
+}
echo "\n";
// Negative out of bounds.
} catch (ValueError $e) {
echo $e->getMessage(), "\n";
}
+try {
+ var_dump(grapheme_strpos("äöü", "bar", -4));
+} catch (ValueError $e) {
+ echo $e->getMessage(), "\n";
+}
+try {
+ var_dump(grapheme_stripos("äöü", "bar", -4));
+} catch (ValueError $e) {
+ echo $e->getMessage(), "\n";
+}
+try {
+ var_dump(grapheme_strrpos("äöü", "bar", -4));
+} catch (ValueError $e) {
+ echo $e->getMessage(), "\n";
+}
+try {
+ var_dump(grapheme_strripos("äöü", "bar", -4));
+} catch (ValueError $e) {
+ echo $e->getMessage(), "\n";
+}
echo "\n";
// TODO: substr is special.
bool(false)
bool(false)
bool(false)
+bool(false)
+bool(false)
+bool(false)
+bool(false)
+
+bool(false)
+bool(false)
+bool(false)
+bool(false)
+bool(false)
+bool(false)
+bool(false)
+bool(false)
bool(false)
bool(false)
bool(false)
bool(false)
+grapheme_strpos(): Argument #3 ($offset) must be contained in argument #1 ($haystack)
+grapheme_stripos(): Argument #3 ($offset) must be contained in argument #1 ($haystack)
+grapheme_strrpos(): Argument #3 ($offset) must be contained in argument #1 ($haystack)
+grapheme_strripos(): Argument #3 ($offset) must be contained in argument #1 ($haystack)
grapheme_strpos(): Argument #3 ($offset) must be contained in argument #1 ($haystack)
grapheme_stripos(): Argument #3 ($offset) must be contained in argument #1 ($haystack)
grapheme_strrpos(): Argument #3 ($offset) must be contained in argument #1 ($haystack)
grapheme_strripos(): Argument #3 ($offset) must be contained in argument #1 ($haystack)
+grapheme_strpos(): Argument #3 ($offset) must be contained in argument #1 ($haystack)
+grapheme_stripos(): Argument #3 ($offset) must be contained in argument #1 ($haystack)
+grapheme_strrpos(): Argument #3 ($offset) must be contained in argument #1 ($haystack)
+grapheme_strripos(): Argument #3 ($offset) must be contained in argument #1 ($haystack)
grapheme_strpos(): Argument #3 ($offset) must be contained in argument #1 ($haystack)
grapheme_stripos(): Argument #3 ($offset) must be contained in argument #1 ($haystack)
grapheme_strrpos(): Argument #3 ($offset) must be contained in argument #1 ($haystack)