Skip to content

Add $before_needle argument to strrchr() #11430

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 1 commit 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
2 changes: 1 addition & 1 deletion ext/standard/basic_functions.stub.php
Original file line number Diff line number Diff line change
Expand Up @@ -2395,7 +2395,7 @@ function strripos(string $haystack, string $needle, int $offset = 0): int|false
* @compile-time-eval
* @refcount 1
*/
function strrchr(string $haystack, string $needle): string|false {}
function strrchr(string $haystack, string $needle, bool $before_needle = false): string|false {}

/** @compile-time-eval */
function str_contains(string $haystack, string $needle): bool {}
Expand Down
7 changes: 2 additions & 5 deletions ext/standard/basic_functions_arginfo.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 9 additions & 3 deletions ext/standard/string.c
Original file line number Diff line number Diff line change
Expand Up @@ -1656,7 +1656,7 @@ PHP_FUNCTION(stristr)
if (part) {
RETURN_STRINGL(ZSTR_VAL(haystack), found_offset);
}
RETURN_STRINGL(ZSTR_VAL(haystack) + found_offset, ZSTR_LEN(haystack) - found_offset);
RETURN_STRINGL(found, ZSTR_LEN(haystack) - found_offset);
}
/* }}} */

Expand Down Expand Up @@ -1684,7 +1684,7 @@ PHP_FUNCTION(strstr)
if (part) {
RETURN_STRINGL(ZSTR_VAL(haystack), found_offset);
}
RETURN_STRINGL(ZSTR_VAL(haystack) + found_offset, ZSTR_LEN(haystack) - found_offset);
RETURN_STRINGL(found, ZSTR_LEN(haystack) - found_offset);
}
/* }}} */

Expand Down Expand Up @@ -1934,17 +1934,23 @@ PHP_FUNCTION(strrchr)
zend_string *haystack, *needle;
const char *found = NULL;
zend_long found_offset;
bool part = 0;

ZEND_PARSE_PARAMETERS_START(2, 2)
ZEND_PARSE_PARAMETERS_START(2, 3)
Z_PARAM_STR(haystack)
Z_PARAM_STR(needle)
Z_PARAM_OPTIONAL
Z_PARAM_BOOL(part)
ZEND_PARSE_PARAMETERS_END();

found = zend_memrchr(ZSTR_VAL(haystack), *ZSTR_VAL(needle), ZSTR_LEN(haystack));
if (UNEXPECTED(!found)) {
RETURN_FALSE;
}
found_offset = found - ZSTR_VAL(haystack);
if (part) {
RETURN_STRINGL(ZSTR_VAL(haystack), found_offset);
}
RETURN_STRINGL(found, ZSTR_LEN(haystack) - found_offset);
}
/* }}} */
Expand Down
30 changes: 30 additions & 0 deletions ext/standard/tests/strings/strrchr_basic.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -4,49 +4,79 @@ Test strrchr() function : basic functionality
<?php
echo "*** Testing strrchr() function: basic functionality ***\n";
var_dump( strrchr("Hello, World", "H") ); //needle as single char
var_dump( strrchr("Hello, World", "H", true) ); //needle as single char
var_dump( strrchr("Hello, World", "Hello") ); //needle as a first word of haystack
var_dump( strrchr("Hello, World", "Hello", true) ); //needle as a first word of haystack
var_dump( strrchr('Hello, World', 'H') );
var_dump( strrchr('Hello, World', 'H', true) );
var_dump( strrchr('Hello, World', 'Hello') );
var_dump( strrchr('Hello, World', 'Hello', true) );

//considering case
var_dump( strrchr("Hello, World", "h") );
var_dump( strrchr("Hello, World", "h", true) );
var_dump( strrchr("Hello, World", "hello") );
var_dump( strrchr("Hello, World", "hello", true) );

//needle as second word of haystack
var_dump( strrchr("Hello, World", "World") );
var_dump( strrchr("Hello, World", "World", true) );
var_dump( strrchr('Hello, World', 'World') );
var_dump( strrchr('Hello, World', 'World', true) );

//needle as special char
var_dump( strrchr("Hello, World", ",") );
var_dump( strrchr("Hello, World", ",", true) );
var_dump( strrchr('Hello, World', ',') );
var_dump( strrchr('Hello, World', ',', true) );

var_dump( strrchr("Hello, World", "Hello, World") ); //needle as haystack
var_dump( strrchr("Hello, World", "Hello, World", true) ); //needle as haystack

//needle string containing one existing and one non-existing char
var_dump( strrchr("Hello, World", "Hi") );
var_dump( strrchr("Hello, World", "Hi", true) );

//multiple existence of needle in haystack
var_dump( strrchr("Hello, World", "o") );
var_dump( strrchr("Hello, World", "o", true) );
var_dump( strrchr("Hello, World", "ooo") );
var_dump( strrchr("Hello, World", "ooo", true) );

var_dump( strrchr("Hello, World", "Zzzz") ); //non-existent needle in haystack
var_dump( strrchr("Hello, World", "Zzzz", true) ); //non-existent needle in haystack
echo "*** Done ***";
?>
--EXPECT--
*** Testing strrchr() function: basic functionality ***
string(12) "Hello, World"
string(0) ""
string(12) "Hello, World"
string(0) ""
string(12) "Hello, World"
string(0) ""
string(12) "Hello, World"
string(0) ""
bool(false)
bool(false)
bool(false)
bool(false)
string(5) "World"
string(7) "Hello, "
string(5) "World"
string(7) "Hello, "
string(7) ", World"
string(5) "Hello"
string(7) ", World"
string(5) "Hello"
string(12) "Hello, World"
string(0) ""
string(12) "Hello, World"
string(0) ""
string(4) "orld"
string(8) "Hello, W"
string(4) "orld"
string(8) "Hello, W"
bool(false)
bool(false)
*** Done ***