17

Add str_contains() function · php/php-src@1668ad7 · GitHub

 4 years ago
source link: https://github.com/php/php-src/commit/1668ad7cb17213e67e42994e0c6911e302a3c3c5
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Add str_contains() function · php/php-src@1668ad7 · GitHubPermalink

Browse files

Add str_contains() function

philippta

authored and nikic

committed on 16 Mar

1 parent 934e60b commit 1668ad7cb17213e67e42994e0c6911e302a3c3c5
Showing with 62 additions and 1 deletion.
@@ -481,7 +481,10 @@ PHP 8.0 UPGRADE NOTES
PR: https://github.com/php/php-src/pull/4797
- Standard:
. Added fdiv() method, which performs a floating-point division under
. Added str_contains($haystack, $needle) function, which checks whether
$haystack contains $needle as a sub-string. It is equivalent to writing
strpos($haystack, $needle) !== false.
. Added fdiv() function, which performs a floating-point division under
IEEE 754 semantics. Division by zero is considered well-defined and
will return one of Inf, -Inf or NaN.
@@ -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)
@@ -586,6 +586,8 @@ function strripos(string $haystack, string $needle, int $offset = 0): int|false
function strrchr(string $haystack, string $needle): string|false {}
function str_contains(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 {}
@@ -918,6 +918,11 @@ ZEND_BEGIN_ARG_WITH_RETURN_TYPE_MASK_EX(arginfo_strrchr, 0, 2, MAY_BE_STRING|MAY
ZEND_ARG_TYPE_INFO(0, needle, IS_STRING, 0)
ZEND_END_ARG_INFO()
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_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)
@@ -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);
@@ -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 */
/* }}} */
@@ -0,0 +1,34 @@
--TEST--
Test str_contains() function
--FILE--
<?php
/* Prototype: str_contains ( string $haystack , string $needle ) : bool
Description: Check if a string contains another string
Source code: ext/standard/string.c
*/
var_dump(str_contains("test string", "test"));
var_dump(str_contains("test string", "string"));
var_dump(str_contains("test string", "strin"));
var_dump(str_contains("test string", "t s"));
var_dump(str_contains("test string", "g"));
var_dump(str_contains("te".chr(0)."st", chr(0)));
var_dump(str_contains("tEst", "test"));
var_dump(str_contains("teSt", "test"));
var_dump(str_contains("", ""));
var_dump(str_contains("a", ""));
var_dump(str_contains("", "a"));
var_dump(str_contains("\\\\a", "\\a"));
?>
--EXPECT--
bool(true)
bool(true)
bool(true)
bool(true)
bool(true)
bool(true)
bool(false)
bool(false)
bool(true)
bool(true)
bool(false)
bool(true)

0 comments on commit 1668ad7

Please sign in to comment.


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK