Skip to content

Commit d37a820

Browse files
authored
zend_string: Add zend_string_ends_with*() (php#22819)
1 parent b1a471a commit d37a820

3 files changed

Lines changed: 28 additions & 7 deletions

File tree

UPGRADING.INTERNALS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,7 @@ PHP 8.6 INTERNALS UPGRADE NOTES
145145
(sendfile, splice, copy_file_range, TransmitFile), and is now used by
146146
php_stream_copy_to_stream_ex(). The mmap-based copy fallback was removed.
147147
. Added zend_string_equals_cstr_ci().
148+
. Added zend_string_ends_with() and related variants.
148149

149150
========================
150151
2. Build system changes

Zend/zend_string.h

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -493,6 +493,32 @@ static zend_always_inline bool zend_string_starts_with_ci(const zend_string *str
493493
#define zend_string_starts_with_literal_ci(str, prefix) \
494494
zend_string_starts_with_cstr_ci(str, "" prefix, sizeof(prefix) - 1)
495495

496+
static zend_always_inline bool zend_string_ends_with_cstr(const zend_string *str, const char *suffix, size_t suffix_length)
497+
{
498+
return ZSTR_LEN(str) >= suffix_length && !memcmp(ZSTR_VAL(str) + ZSTR_LEN(str) - suffix_length, suffix, suffix_length);
499+
}
500+
501+
static zend_always_inline bool zend_string_ends_with(const zend_string *str, const zend_string *suffix)
502+
{
503+
return zend_string_ends_with_cstr(str, ZSTR_VAL(suffix), ZSTR_LEN(suffix));
504+
}
505+
506+
#define zend_string_ends_with_literal(str, suffix) \
507+
zend_string_ends_with_cstr(str, "" suffix, sizeof(suffix) - 1)
508+
509+
static zend_always_inline bool zend_string_ends_with_cstr_ci(const zend_string *str, const char *suffix, size_t suffix_length)
510+
{
511+
return ZSTR_LEN(str) >= suffix_length && !strncasecmp(ZSTR_VAL(str) + ZSTR_LEN(str) - suffix_length, suffix, suffix_length);
512+
}
513+
514+
static zend_always_inline bool zend_string_ends_with_ci(const zend_string *str, const zend_string *suffix)
515+
{
516+
return zend_string_ends_with_cstr_ci(str, ZSTR_VAL(suffix), ZSTR_LEN(suffix));
517+
}
518+
519+
#define zend_string_ends_with_literal_ci(str, suffix) \
520+
zend_string_ends_with_cstr_ci(str, "" suffix, sizeof(suffix) - 1)
521+
496522
/*
497523
* DJBX33A (Daniel J. Bernstein, Times 33 with Addition)
498524
*

ext/standard/string.c

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1861,13 +1861,7 @@ PHP_FUNCTION(str_ends_with)
18611861
Z_PARAM_STR(needle)
18621862
ZEND_PARSE_PARAMETERS_END();
18631863

1864-
if (ZSTR_LEN(needle) > ZSTR_LEN(haystack)) {
1865-
RETURN_FALSE;
1866-
}
1867-
1868-
RETURN_BOOL(memcmp(
1869-
ZSTR_VAL(haystack) + ZSTR_LEN(haystack) - ZSTR_LEN(needle),
1870-
ZSTR_VAL(needle), ZSTR_LEN(needle)) == 0);
1864+
RETURN_BOOL(zend_string_ends_with(haystack, needle));
18711865
}
18721866
/* }}} */
18731867

0 commit comments

Comments
 (0)