Skip to content

Allow empty needle in grapheme_str*pos, grapheme_str*str #6245

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

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
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
25 changes: 0 additions & 25 deletions ext/intl/grapheme/grapheme_string.c
Original file line number Diff line number Diff line change
Expand Up @@ -124,11 +124,6 @@ PHP_FUNCTION(grapheme_strpos)

/* the offset is 'grapheme count offset' so it still might be invalid - we'll check it later */

if (needle_len == 0) {
zend_argument_value_error(2, "cannot be empty");
RETURN_THROWS();
}

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
Expand Down Expand Up @@ -178,11 +173,6 @@ PHP_FUNCTION(grapheme_stripos)

/* the offset is 'grapheme count offset' so it still might be invalid - we'll check it later */

if (needle_len == 0) {
zend_argument_value_error(2, "cannot be empty");
RETURN_THROWS();
}

is_ascii = ( grapheme_ascii_check((unsigned char*)haystack, haystack_len) >= 0 );

if ( is_ascii ) {
Expand Down Expand Up @@ -244,11 +234,6 @@ PHP_FUNCTION(grapheme_strrpos)

/* the offset is 'grapheme count offset' so it still might be invalid - we'll check it later */

if (needle_len == 0) {
zend_argument_value_error(2, "cannot be empty");
RETURN_THROWS();
}

is_ascii = grapheme_ascii_check((unsigned char *)haystack, haystack_len) >= 0;

if ( is_ascii ) {
Expand Down Expand Up @@ -304,11 +289,6 @@ PHP_FUNCTION(grapheme_strripos)

/* the offset is 'grapheme count offset' so it still might be invalid - we'll check it later */

if (needle_len == 0) {
zend_argument_value_error(2, "cannot be empty");
RETURN_THROWS();
}

is_ascii = grapheme_ascii_check((unsigned char *)haystack, haystack_len) >= 0;

if ( is_ascii ) {
Expand Down Expand Up @@ -572,11 +552,6 @@ static void strstr_common_handler(INTERNAL_FUNCTION_PARAMETERS, int f_ignore_cas
RETURN_THROWS();
}

if (needle_len == 0) {
zend_argument_value_error(2, "cannot be empty");
RETURN_THROWS();
}

if ( !f_ignore_case ) {

/* ASCII optimization: quick check to see if the string might be there */
Expand Down
11 changes: 11 additions & 0 deletions ext/intl/grapheme/grapheme_util.c
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,17 @@ int32_t grapheme_strpos_utf16(char *haystack, size_t haystack_len, char *needle,
ubrk_setText(bi, uhaystack, uhaystack_len, &status);
STRPOS_CHECK_STATUS(status, "Failed to set up iterator");

if (uneedle_len == 0) {
offset_pos = grapheme_get_haystack_offset(bi, last && offset >= 0 ? uhaystack_len : offset);
if (offset_pos == -1) {
zend_argument_value_error(3, "must be contained in argument #1 ($haystack)");
ret_pos = -1;
goto finish;
}
ret_pos = offset_pos;
goto finish;
}

status = U_ZERO_ERROR;
src = usearch_open(uneedle, uneedle_len, uhaystack, uhaystack_len, "", bi, &status);
STRPOS_CHECK_STATUS(status, "Error creating search object");
Expand Down
124 changes: 48 additions & 76 deletions ext/intl/tests/grapheme_empty.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -7,82 +7,54 @@ Test grapheme_strpos-alike functions with empty needle

ini_set("intl.error_level", E_WARNING);

try {
var_dump(grapheme_strpos("abc", "", -1));
} catch (ValueError $exception) {
echo $exception->getMessage() . "\n";
}

try {
var_dump(grapheme_strpos("abc", ""));
} catch (ValueError $exception) {
echo $exception->getMessage() . "\n";
}

try {
var_dump(grapheme_strpos("abc", "", -1));
} catch (ValueError $exception) {
echo $exception->getMessage() . "\n";
}

try {
var_dump(grapheme_stripos("abc", ""));
} catch (ValueError $exception) {
echo $exception->getMessage() . "\n";
}

try {
var_dump(grapheme_stripos("abc", "", -1));
} catch (ValueError $exception) {
echo $exception->getMessage() . "\n";
}

try {
var_dump(grapheme_strrpos("abc", ""));
} catch (ValueError $exception) {
echo $exception->getMessage() . "\n";
}

try {
var_dump(grapheme_strrpos("abc", "", -1));
} catch (ValueError $exception) {
echo $exception->getMessage() . "\n";
}

try {
var_dump(grapheme_strripos("abc", ""));
} catch (ValueError $exception) {
echo $exception->getMessage() . "\n";
}

try {
var_dump(grapheme_strripos("abc", "", 1));
} catch (ValueError $exception) {
echo $exception->getMessage() . "\n";
}

try {
var_dump(grapheme_strstr("abc", ""));
} catch (ValueError $exception) {
echo $exception->getMessage() . "\n";
}

try {
var_dump(grapheme_stristr("abc", ""));
} catch (ValueError $exception) {
echo $exception->getMessage() . "\n";
}
var_dump(grapheme_strpos("abc", ""));
var_dump(grapheme_strpos("abc", "", -1));
var_dump(grapheme_stripos("abc", ""));
var_dump(grapheme_stripos("abc", "", -1));
var_dump(grapheme_strrpos("abc", ""));
var_dump(grapheme_strrpos("abc", "", -1));
var_dump(grapheme_strripos("abc", ""));
var_dump(grapheme_strripos("abc", "", 1));
var_dump(grapheme_strstr("abc", ""));
var_dump(grapheme_strstr("abc", "", true));
var_dump(grapheme_stristr("abc", ""));
var_dump(grapheme_stristr("abc", "", true));
var_dump(grapheme_strpos("äbc", ""));
var_dump(grapheme_strpos("äbc", "", -1));
var_dump(grapheme_stripos("äbc", ""));
var_dump(grapheme_stripos("äbc", "", -1));
var_dump(grapheme_strrpos("äbc", ""));
var_dump(grapheme_strrpos("äbc", "", -1));
var_dump(grapheme_strripos("äbc", ""));
var_dump(grapheme_strripos("äbc", "", 1));
var_dump(grapheme_strstr("äbc", ""));
var_dump(grapheme_strstr("äbc", "", true));
var_dump(grapheme_stristr("äbc", ""));
var_dump(grapheme_stristr("äbc", "", true));

?>
--EXPECT--
grapheme_strpos(): Argument #2 ($needle) cannot be empty
grapheme_strpos(): Argument #2 ($needle) cannot be empty
grapheme_strpos(): Argument #2 ($needle) cannot be empty
grapheme_stripos(): Argument #2 ($needle) cannot be empty
grapheme_stripos(): Argument #2 ($needle) cannot be empty
grapheme_strrpos(): Argument #2 ($needle) cannot be empty
grapheme_strrpos(): Argument #2 ($needle) cannot be empty
grapheme_strripos(): Argument #2 ($needle) cannot be empty
grapheme_strripos(): Argument #2 ($needle) cannot be empty
grapheme_strstr(): Argument #2 ($needle) cannot be empty
grapheme_stristr(): Argument #2 ($needle) cannot be empty
int(0)
int(2)
int(0)
int(2)
int(3)
int(2)
int(3)
int(3)
string(3) "abc"
string(0) ""
string(3) "abc"
string(0) ""
int(0)
int(2)
int(0)
int(2)
int(3)
int(2)
int(3)
int(3)
string(4) "äbc"
string(0) ""
string(4) "äbc"
string(0) ""