-
Notifications
You must be signed in to change notification settings - Fork 7.9k
Patch add str_starts_with and str_ends_with #5300
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
Changes from 15 commits
ab7b464
a709095
401b5a0
0375e0c
21810c5
85c0572
4e94af7
7ba82ca
9dedd4a
321267e
31ba825
30a9eae
4403607
0ea413f
8292205
bf63e96
2509782
075993f
1b42e9c
303b48c
52efdbf
109afd0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1866,6 +1866,42 @@ PHP_FUNCTION(str_contains) | |
} | ||
/* }}} */ | ||
|
||
/* {{{ proto bool str_starts_with(string haystack, string needle) | ||
Checks if haystack starts with needle */ | ||
PHP_FUNCTION(str_starts_with) { | ||
zend_string *haystack, *needle; | ||
|
||
ZEND_PARSE_PARAMETERS_START(2, 2) | ||
Z_PARAM_STR(haystack) | ||
Z_PARAM_STR(needle) | ||
ZEND_PARSE_PARAMETERS_END(); | ||
|
||
if (ZSTR_LEN(needle) > ZSTR_LEN(haystack)) { | ||
RETURN_FALSE; | ||
} | ||
|
||
RETURN_BOOL(memcmp(ZSTR_VAL(haystack), ZSTR_VAL(needle), ZSTR_LEN(needle)) == 0); | ||
} | ||
|
||
/* {{{ proto bool str_ends_with(string haystack, string needle) | ||
Checks if haystack ends with needle */ | ||
PHP_FUNCTION(str_ends_with) { | ||
zend_string *haystack, *needle; | ||
int k; | ||
|
||
ZEND_PARSE_PARAMETERS_START(2, 2) | ||
Z_PARAM_STR(haystack) | ||
Z_PARAM_STR(needle) | ||
ZEND_PARSE_PARAMETERS_END(); | ||
|
||
if (ZSTR_LEN(needle) > ZSTR_LEN(haystack)) { | ||
RETURN_FALSE; | ||
} | ||
|
||
k = ZSTR_LEN(haystack) - ZSTR_LEN(needle); | ||
RETURN_BOOL(memcmp(&(ZSTR_VAL(haystack))[k], ZSTR_VAL(needle), ZSTR_LEN(needle)) == 0); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm alright with changing it, I don't do a lot of C so I had to play around some to figure that one out. More than happy to use a more concise or idiomatic way. |
||
} | ||
|
||
/* {{{ proto string strchr(string haystack, string needle) | ||
An alias for strstr */ | ||
/* }}} */ | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
--TEST-- | ||
str_ends_with() function - unit tests for str_ends_with() | ||
--FILE-- | ||
<?php | ||
/* Prototype: bool str_ends_with (string $haystack, string $needle); | ||
Description: Determine if $haystack ends with $needle | ||
*/ | ||
$testStr = "beginningMiddleEnd"; | ||
var_dump(str_ends_with($testStr, "End")); | ||
var_dump(str_ends_with($testStr, "end")); | ||
var_dump(str_ends_with($testStr, "en")); | ||
var_dump(str_ends_with($testStr, $testStr."a")); | ||
wkhudgins92 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
var_dump(str_ends_with("a".$testStr, $testStr)); | ||
wkhudgins92 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
var_dump(str_ends_with($testStr, "")); | ||
var_dump(str_ends_with("", "")); | ||
var_dump(str_ends_with("", " ")); | ||
var_dump(str_ends_with("\x00", "")); | ||
wkhudgins92 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
var_dump(str_ends_with("\x00", "\x00")); | ||
var_dump(str_ends_with("a", "\x00")); | ||
wkhudgins92 marked this conversation as resolved.
Show resolved
Hide resolved
wkhudgins92 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
var_dump(str_ends_with("a\x00", "\x00")); | ||
var_dump(str_ends_with("ab\x00c", "b\x00c")); | ||
var_dump(str_ends_with("a\x00b", "d\x00b")); | ||
var_dump(str_ends_with("a\x00b", "a\x00z")); | ||
var_dump(str_ends_with("a", "\x00a")); | ||
var_dump(str_ends_with("a", "a\x00")); | ||
?> | ||
--EXPECT-- | ||
bool(true) | ||
bool(false) | ||
bool(false) | ||
bool(false) | ||
bool(true) | ||
bool(true) | ||
bool(true) | ||
bool(false) | ||
bool(true) | ||
bool(true) | ||
bool(false) | ||
wkhudgins92 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
bool(true) | ||
bool(true) | ||
bool(false) | ||
bool(false) | ||
bool(false) | ||
bool(false) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
--TEST-- | ||
str_starts_with() function - unit tests for str_starts_with() | ||
--FILE-- | ||
<?php | ||
/* Prototype: bool str_starts_with (string $haystack, string $needle); | ||
Description: Determine if $haystack begins with $needle | ||
*/ | ||
$testStr = "beginningMiddleEnd"; | ||
var_dump(str_starts_with($testStr, "beginning")); | ||
var_dump(str_starts_with($testStr, "Beginning")); | ||
var_dump(str_starts_with($testStr, "eginning")); | ||
var_dump(str_starts_with($testStr, $testStr."a")); | ||
wkhudgins92 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
var_dump(str_starts_with($testStr, "")); | ||
var_dump(str_starts_with("", "")); | ||
var_dump(str_starts_with("", " ")); | ||
var_dump(str_starts_with($testStr, "\x00")); | ||
var_dump(str_starts_with("\x00", "")); | ||
var_dump(str_starts_with("\x00", "\x00")); | ||
var_dump(str_starts_with("\x00a", "\x00")); | ||
var_dump(str_starts_with("a\x00bc", "a\x00b")); | ||
var_dump(str_starts_with("a\x00b", "a\x00d")); | ||
var_dump(str_starts_with("a\x00b", "z\x00b")); | ||
var_dump(str_starts_with("a", "a\x00")); | ||
var_dump(str_starts_with("a", "\x00a")); | ||
?> | ||
--EXPECT-- | ||
bool(true) | ||
bool(false) | ||
bool(false) | ||
bool(false) | ||
wkhudgins92 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
bool(true) | ||
bool(true) | ||
bool(false) | ||
bool(false) | ||
bool(true) | ||
bool(true) | ||
bool(true) | ||
bool(true) | ||
bool(false) | ||
bool(false) | ||
bool(false) | ||
bool(false) | ||
|
Uh oh!
There was an error while loading. Please reload this page.