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 5 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
2 changes: 2 additions & 0 deletions ext/standard/basic_functions.c
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,8 @@ static const zend_function_entry basic_functions[] = { /* {{{ */
PHP_FE(strtoupper, arginfo_strtoupper)
PHP_FE(strtolower, arginfo_strtolower)
PHP_FE(str_contains, arginfo_str_contains)
PHP_FE(str_starts_with, arginfo_str_starts_with)
PHP_FE(str_ends_with, arginfo_str_ends_with)
PHP_FE(strpos, arginfo_strpos)
PHP_FE(stripos, arginfo_stripos)
PHP_FE(strrpos, arginfo_strrpos)
Expand Down
4 changes: 4 additions & 0 deletions ext/standard/basic_functions.stub.php
Original file line number Diff line number Diff line change
Expand Up @@ -588,6 +588,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
10 changes: 10 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
2 changes: 2 additions & 0 deletions ext/standard/php_string.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ PHP_FUNCTION(dirname);
PHP_FUNCTION(pathinfo);
PHP_FUNCTION(strstr);
PHP_FUNCTION(str_contains);
PHP_FUNCTION(str_starts_with);
PHP_FUNCTION(str_ends_with);
PHP_FUNCTION(strpos);
PHP_FUNCTION(stripos);
PHP_FUNCTION(strrpos);
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 boolean str_starts_with(string haystack, string needle)
Checks if haystack strats 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 (needle->len > haystack->len) {
RETURN_FALSE;
}

RETURN_BOOL(memcmp(ZSTR_VAL(haystack), ZSTR_VAL(needle), ZSTR_LEN(needle)) == 0);
}

/* {{{ proto boolean str_ends_with(string haystack, string needle)
Checks if haystack ends with needle */
PHP_FUNCTION(str_ends_with) {
zend_string *haystack, *needle;
int i, j;

ZEND_PARSE_PARAMETERS_START(2, 2)
Z_PARAM_STR(haystack)
Z_PARAM_STR(needle)
ZEND_PARSE_PARAMETERS_END();

if (needle->len > haystack->len) {
RETURN_FALSE;
}

for (i = haystack->len - 1, j = needle->len - 1; j >= 0; i--, j--)
if (haystack->val[i] != needle->val[j])
RETURN_FALSE;
RETURN_TRUE;
}

/* {{{ proto string strchr(string haystack, string needle)
An alias for strstr */
/* }}} */
Expand Down
19 changes: 19 additions & 0 deletions ext/standard/tests/strings/str_ends_with.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
--TEST--
str_ends_with() function - unit tests for str_ends_with()
--FILE--
<?php
/* Prototype: boolean 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"));
?>
--EXPECT--
bool(true)
bool(false)
bool(false)
bool(false)

19 changes: 19 additions & 0 deletions ext/standard/tests/strings/str_starts_with.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
--TEST--
str_starts_with() function - unit tests for str_starts_with()
--FILE--
<?php
/* Prototype: boolean 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"));
?>
--EXPECT--
bool(true)
bool(false)
bool(false)
bool(false)