Skip to content

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

Closed
Show file tree
Hide file tree
Changes from 20 commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
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
4 changes: 4 additions & 0 deletions ext/standard/basic_functions.stub.php
Original file line number Diff line number Diff line change
Expand Up @@ -608,6 +608,10 @@ function strrchr(string $haystack, string $needle): string|false {}

function str_contains(string $haystack, string $needle): bool {}

function str_starts_with(string $haystack, string $needle): bool {}

function str_ends_with(string $haystack, string $needle): bool {}

function chunk_split(string $str, int $chunklen = 76, string $ending = "\r\n"): string {}

function substr(string $str, int $start, ?int $length = null): string|false {}
Expand Down
14 changes: 14 additions & 0 deletions ext/standard/basic_functions_arginfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -923,6 +923,16 @@ ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_str_contains, 0, 2, _IS_BOOL, 0)
ZEND_ARG_TYPE_INFO(0, needle, IS_STRING, 0)
ZEND_END_ARG_INFO()

ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_str_starts_with, 0, 2, _IS_BOOL, 0)
ZEND_ARG_TYPE_INFO(0, haystack, IS_STRING, 0)
ZEND_ARG_TYPE_INFO(0, needle, IS_STRING, 0)
ZEND_END_ARG_INFO()

ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_str_ends_with, 0, 2, _IS_BOOL, 0)
ZEND_ARG_TYPE_INFO(0, haystack, IS_STRING, 0)
ZEND_ARG_TYPE_INFO(0, needle, IS_STRING, 0)
ZEND_END_ARG_INFO()

ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_chunk_split, 0, 1, IS_STRING, 0)
ZEND_ARG_TYPE_INFO(0, str, IS_STRING, 0)
ZEND_ARG_TYPE_INFO(0, chunklen, IS_LONG, 0)
Expand Down Expand Up @@ -2496,6 +2506,8 @@ ZEND_FUNCTION(strrpos);
ZEND_FUNCTION(strripos);
ZEND_FUNCTION(strrchr);
ZEND_FUNCTION(str_contains);
ZEND_FUNCTION(str_starts_with);
ZEND_FUNCTION(str_ends_with);
ZEND_FUNCTION(chunk_split);
ZEND_FUNCTION(substr);
ZEND_FUNCTION(substr_replace);
Expand Down Expand Up @@ -3128,6 +3140,8 @@ static const zend_function_entry ext_functions[] = {
ZEND_FE(strripos, arginfo_strripos)
ZEND_FE(strrchr, arginfo_strrchr)
ZEND_FE(str_contains, arginfo_str_contains)
ZEND_FE(str_starts_with, arginfo_str_starts_with)
ZEND_FE(str_ends_with, arginfo_str_ends_with)
ZEND_FE(chunk_split, arginfo_chunk_split)
ZEND_FE(substr, arginfo_substr)
ZEND_FE(substr_replace, arginfo_substr_replace)
Expand Down
38 changes: 38 additions & 0 deletions ext/standard/string.c
Original file line number Diff line number Diff line change
Expand Up @@ -1866,6 +1866,44 @@ 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;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This needs to use size_t, otherwise it may truncate.


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);
}

/* {{{ proto string strchr(string haystack, string needle)
An alias for strstr */
/* }}} */
Expand Down
45 changes: 45 additions & 0 deletions ext/standard/tests/strings/str_ends_with.phpt
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));
var_dump(str_ends_with($testStr, $testStr.$testStr));
var_dump(str_ends_with($testStr, ""));
var_dump(str_ends_with("", ""));
var_dump(str_ends_with("", " "));
var_dump(str_ends_with($testStr, "\x00"));
var_dump(str_ends_with("\x00", ""));
var_dump(str_ends_with("\x00", "\x00"));
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(true)
bool(false)
bool(true)
bool(true)
bool(false)
bool(false)
bool(true)
bool(true)
bool(false)
bool(true)
bool(false)
bool(false)
bool(false)
bool(false)

43 changes: 43 additions & 0 deletions ext/standard/tests/strings/str_starts_with.phpt
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));
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(true)
bool(true)
bool(true)
bool(false)
bool(false)
bool(true)
bool(true)
bool(true)
bool(true)
bool(false)
bool(false)
bool(false)
bool(false)