Skip to content

Commit bad9a75

Browse files
committed
Add before_needle argument to strrchr()
1 parent 49fbbea commit bad9a75

File tree

4 files changed

+42
-9
lines changed

4 files changed

+42
-9
lines changed

ext/standard/basic_functions.stub.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2389,7 +2389,7 @@ function strripos(string $haystack, string $needle, int $offset = 0): int|false
23892389
* @compile-time-eval
23902390
* @refcount 1
23912391
*/
2392-
function strrchr(string $haystack, string $needle): string|false {}
2392+
function strrchr(string $haystack, string $needle, bool $before_needle = false): string|false {}
23932393

23942394
/** @compile-time-eval */
23952395
function str_contains(string $haystack, string $needle): bool {}

ext/standard/basic_functions_arginfo.h

Lines changed: 2 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ext/standard/string.c

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1544,7 +1544,7 @@ PHP_FUNCTION(stristr)
15441544
if (part) {
15451545
RETURN_STRINGL(ZSTR_VAL(haystack), found_offset);
15461546
}
1547-
RETURN_STRINGL(ZSTR_VAL(haystack) + found_offset, ZSTR_LEN(haystack) - found_offset);
1547+
RETURN_STRINGL(found, ZSTR_LEN(haystack) - found_offset);
15481548
}
15491549
/* }}} */
15501550

@@ -1572,7 +1572,7 @@ PHP_FUNCTION(strstr)
15721572
if (part) {
15731573
RETURN_STRINGL(ZSTR_VAL(haystack), found_offset);
15741574
}
1575-
RETURN_STRINGL(ZSTR_VAL(haystack) + found_offset, ZSTR_LEN(haystack) - found_offset);
1575+
RETURN_STRINGL(found, ZSTR_LEN(haystack) - found_offset);
15761576
}
15771577
/* }}} */
15781578

@@ -1822,17 +1822,23 @@ PHP_FUNCTION(strrchr)
18221822
zend_string *haystack, *needle;
18231823
const char *found = NULL;
18241824
zend_long found_offset;
1825+
bool part = 0;
18251826

1826-
ZEND_PARSE_PARAMETERS_START(2, 2)
1827+
ZEND_PARSE_PARAMETERS_START(2, 3)
18271828
Z_PARAM_STR(haystack)
18281829
Z_PARAM_STR(needle)
1830+
Z_PARAM_OPTIONAL
1831+
Z_PARAM_BOOL(part)
18291832
ZEND_PARSE_PARAMETERS_END();
18301833

18311834
found = zend_memrchr(ZSTR_VAL(haystack), *ZSTR_VAL(needle), ZSTR_LEN(haystack));
18321835
if (UNEXPECTED(!found)) {
18331836
RETURN_FALSE;
18341837
}
18351838
found_offset = found - ZSTR_VAL(haystack);
1839+
if (part) {
1840+
RETURN_STRINGL(ZSTR_VAL(haystack), found_offset);
1841+
}
18361842
RETURN_STRINGL(found, ZSTR_LEN(haystack) - found_offset);
18371843
}
18381844
/* }}} */

ext/standard/tests/strings/strrchr_basic.phpt

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,49 +4,79 @@ Test strrchr() function : basic functionality
44
<?php
55
echo "*** Testing strrchr() function: basic functionality ***\n";
66
var_dump( strrchr("Hello, World", "H") ); //needle as single char
7+
var_dump( strrchr("Hello, World", "H", true) ); //needle as single char
78
var_dump( strrchr("Hello, World", "Hello") ); //needle as a first word of haystack
9+
var_dump( strrchr("Hello, World", "Hello", true) ); //needle as a first word of haystack
810
var_dump( strrchr('Hello, World', 'H') );
11+
var_dump( strrchr('Hello, World', 'H', true) );
912
var_dump( strrchr('Hello, World', 'Hello') );
13+
var_dump( strrchr('Hello, World', 'Hello', true) );
1014

1115
//considering case
1216
var_dump( strrchr("Hello, World", "h") );
17+
var_dump( strrchr("Hello, World", "h", true) );
1318
var_dump( strrchr("Hello, World", "hello") );
19+
var_dump( strrchr("Hello, World", "hello", true) );
1420

1521
//needle as second word of haystack
1622
var_dump( strrchr("Hello, World", "World") );
23+
var_dump( strrchr("Hello, World", "World", true) );
1724
var_dump( strrchr('Hello, World', 'World') );
25+
var_dump( strrchr('Hello, World', 'World', true) );
1826

1927
//needle as special char
2028
var_dump( strrchr("Hello, World", ",") );
29+
var_dump( strrchr("Hello, World", ",", true) );
2130
var_dump( strrchr('Hello, World', ',') );
31+
var_dump( strrchr('Hello, World', ',', true) );
2232

2333
var_dump( strrchr("Hello, World", "Hello, World") ); //needle as haystack
34+
var_dump( strrchr("Hello, World", "Hello, World", true) ); //needle as haystack
2435

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

2840
//multiple existence of needle in haystack
2941
var_dump( strrchr("Hello, World", "o") );
42+
var_dump( strrchr("Hello, World", "o", true) );
3043
var_dump( strrchr("Hello, World", "ooo") );
44+
var_dump( strrchr("Hello, World", "ooo", true) );
3145

3246
var_dump( strrchr("Hello, World", "Zzzz") ); //non-existent needle in haystack
47+
var_dump( strrchr("Hello, World", "Zzzz", true) ); //non-existent needle in haystack
3348
echo "*** Done ***";
3449
?>
3550
--EXPECT--
3651
*** Testing strrchr() function: basic functionality ***
3752
string(12) "Hello, World"
53+
string(0) ""
3854
string(12) "Hello, World"
55+
string(0) ""
3956
string(12) "Hello, World"
57+
string(0) ""
4058
string(12) "Hello, World"
59+
string(0) ""
60+
bool(false)
61+
bool(false)
4162
bool(false)
4263
bool(false)
4364
string(5) "World"
65+
string(7) "Hello, "
4466
string(5) "World"
67+
string(7) "Hello, "
4568
string(7) ", World"
69+
string(5) "Hello"
4670
string(7) ", World"
71+
string(5) "Hello"
4772
string(12) "Hello, World"
73+
string(0) ""
4874
string(12) "Hello, World"
75+
string(0) ""
4976
string(4) "orld"
77+
string(8) "Hello, W"
5078
string(4) "orld"
79+
string(8) "Hello, W"
80+
bool(false)
5181
bool(false)
5282
*** Done ***

0 commit comments

Comments
 (0)