Skip to content

Commit 321267e

Browse files
committed
Reworked str_ends_with to use memcmp
1 parent 9dedd4a commit 321267e

File tree

1 file changed

+3
-5
lines changed

1 file changed

+3
-5
lines changed

ext/standard/string.c

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1887,7 +1887,7 @@ PHP_FUNCTION(str_starts_with) {
18871887
Checks if haystack ends with needle */
18881888
PHP_FUNCTION(str_ends_with) {
18891889
zend_string *haystack, *needle;
1890-
int i, j;
1890+
int k;
18911891

18921892
ZEND_PARSE_PARAMETERS_START(2, 2)
18931893
Z_PARAM_STR(haystack)
@@ -1898,10 +1898,8 @@ PHP_FUNCTION(str_ends_with) {
18981898
RETURN_FALSE;
18991899
}
19001900

1901-
for (i = haystack->len - 1, j = needle->len - 1; j >= 0; i--, j--)
1902-
if (haystack->val[i] != needle->val[j])
1903-
RETURN_FALSE;
1904-
RETURN_TRUE;
1901+
k = ZSTR_LEN(haystack) - ZSTR_LEN(needle);
1902+
RETURN_BOOL(memcmp(&(ZSTR_VAL(haystack))[k], &(ZSTR_VAL(needle))[k], ZSTR_LEN(needle)) == 0);
19051903
}
19061904

19071905
/* {{{ proto string strchr(string haystack, string needle)

0 commit comments

Comments
 (0)