Skip to content

Commit

Permalink
Create str_contains php function
Browse files Browse the repository at this point in the history
  • Loading branch information
philippta committed Feb 14, 2020
1 parent a957e84 commit 5994551
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 0 deletions.
1 change: 1 addition & 0 deletions ext/standard/basic_functions.c
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,7 @@ static const zend_function_entry basic_functions[] = { /* {{{ */
PHP_FE(strtok, arginfo_strtok)
PHP_FE(strtoupper, arginfo_strtoupper)
PHP_FE(strtolower, arginfo_strtolower)
PHP_FE(str_contains, arginfo_str_contains)
PHP_FE(strpos, arginfo_strpos)
PHP_FE(stripos, arginfo_stripos)
PHP_FE(strrpos, arginfo_strrpos)
Expand Down
2 changes: 2 additions & 0 deletions ext/standard/basic_functions.stub.php
Original file line number Diff line number Diff line change
Expand Up @@ -553,6 +553,8 @@ function stristr(string $haystack, string $needle, bool $before_needle = false):

function strstr(string $haystack, string $needle, bool $before_needle = false): string|false {}

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

function strpos(string $haystack, string $needle, int $offset = 0): int|false {}

function stripos(string $haystack, string $needle, int $offset = 0): int|false {}
Expand Down
5 changes: 5 additions & 0 deletions ext/standard/basic_functions_arginfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -877,6 +877,11 @@ ZEND_END_ARG_INFO()

#define arginfo_strstr arginfo_stristr

ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_str_contains, 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_MASK_EX(arginfo_strpos, 0, 2, MAY_BE_LONG|MAY_BE_FALSE)
ZEND_ARG_TYPE_INFO(0, haystack, IS_STRING, 0)
ZEND_ARG_TYPE_INFO(0, needle, IS_STRING, 0)
Expand Down
1 change: 1 addition & 0 deletions ext/standard/php_string.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ PHP_FUNCTION(basename);
PHP_FUNCTION(dirname);
PHP_FUNCTION(pathinfo);
PHP_FUNCTION(strstr);
PHP_FUNCTION(str_contains);
PHP_FUNCTION(strpos);
PHP_FUNCTION(stripos);
PHP_FUNCTION(strrpos);
Expand Down
15 changes: 15 additions & 0 deletions ext/standard/string.c
Original file line number Diff line number Diff line change
Expand Up @@ -1851,6 +1851,21 @@ PHP_FUNCTION(strstr)
}
/* }}} */

/* {{{ proto bool str_contains(string haystack, string needle)
Checks if a string contains another */
PHP_FUNCTION(str_contains)
{
zend_string *haystack, *needle;

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

RETURN_BOOL(php_memnstr(ZSTR_VAL(haystack), ZSTR_VAL(needle), ZSTR_LEN(needle), ZSTR_VAL(haystack) + ZSTR_LEN(haystack)));
}
/* }}} */

/* {{{ proto string strchr(string haystack, string needle)
An alias for strstr */
/* }}} */
Expand Down

0 comments on commit 5994551

Please sign in to comment.