Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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 NEWS
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,8 @@ PHP NEWS
- Standard:
. Fixed bug GH-19926 (reset internal pointer earlier while splicing array
while COW violation flag is still set). (alexandre-daubois)
. Added form feed (\f) in the default trimmed characters of trim(), rtrim()
and ltrim(). (Weilin Du)
. Invalid mode values now throw in array_filter() instead of being silently
defaulted to 0. (Jorg Sowa)
. Fixed bug GH-21058 (error_log() crashes with message_type 3 and
Expand Down
2 changes: 2 additions & 0 deletions UPGRADING
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ PHP 8.6 UPGRADE NOTES
- Standard:
. Invalid mode values now throw in array_filter() instead of being silently
defaulted to 0.
. Form feed (\f) is now added in the default trimmed characters of trim(),
rtrim() and ltrim(). RFC: https://wiki.php.net/rfc/trim_form_feed

========================================
2. New Features
Expand Down
8 changes: 4 additions & 4 deletions ext/standard/basic_functions.stub.php
Original file line number Diff line number Diff line change
Expand Up @@ -2318,16 +2318,16 @@ function strcoll(string $string1, string $string2): int {}
* @frameless-function {"arity": 1}
* @frameless-function {"arity": 2}
*/
function trim(string $string, string $characters = " \n\r\t\v\0"): string {}
function trim(string $string, string $characters = " \f\n\r\t\v\0"): string {}

/** @compile-time-eval */
function rtrim(string $string, string $characters = " \n\r\t\v\0"): string {}
function rtrim(string $string, string $characters = " \f\n\r\t\v\0"): string {}

/** @alias rtrim */
function chop(string $string, string $characters = " \n\r\t\v\0"): string {}
function chop(string $string, string $characters = " \f\n\r\t\v\0"): string {}

/** @compile-time-eval */
function ltrim(string $string, string $characters = " \n\r\t\v\0"): string {}
function ltrim(string $string, string $characters = " \f\n\r\t\v\0"): string {}

/**
* @compile-time-eval
Expand Down
4 changes: 2 additions & 2 deletions ext/standard/basic_functions_arginfo.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions ext/standard/basic_functions_decl.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 9 additions & 10 deletions ext/standard/string.c
Original file line number Diff line number Diff line change
Expand Up @@ -515,11 +515,16 @@ static inline zend_result php_charmask(const unsigned char *input, size_t len, c
}
/* }}} */

static zend_always_inline bool php_is_whitespace(unsigned char c)
{
return c <= ' ' && (c == ' ' || c == '\f' || c == '\n' || c == '\r' || c == '\t' || c == '\v' || c == '\0');
}

/* {{{ php_trim_int()
* mode 1 : trim left
* mode 2 : trim right
* mode 3 : trim left and right
* what indicates which chars are to be trimmed. NULL->default (' \t\n\r\v\0')
* what indicates which chars are to be trimmed. NULL->default (' \f\t\n\r\v\0')
*/
static zend_always_inline zend_string *php_trim_int(zend_string *str, const char *what, size_t what_len, int mode)
{
Expand Down Expand Up @@ -573,10 +578,7 @@ static zend_always_inline zend_string *php_trim_int(zend_string *str, const char
} else {
if (mode & 1) {
while (start != end) {
unsigned char c = (unsigned char)*start;

if (c <= ' ' &&
(c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == '\v' || c == '\0')) {
if (php_is_whitespace((unsigned char)*start)) {
start++;
} else {
break;
Expand All @@ -585,10 +587,7 @@ static zend_always_inline zend_string *php_trim_int(zend_string *str, const char
}
if (mode & 2) {
while (start != end) {
unsigned char c = (unsigned char)*(end-1);

if (c <= ' ' &&
(c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == '\v' || c == '\0')) {
if (php_is_whitespace((unsigned char)*(end-1))) {
end--;
} else {
break;
Expand All @@ -611,7 +610,7 @@ static zend_always_inline zend_string *php_trim_int(zend_string *str, const char
* mode 1 : trim left
* mode 2 : trim right
* mode 3 : trim left and right
* what indicates which chars are to be trimmed. NULL->default (' \t\n\r\v\0')
* what indicates which chars are to be trimmed. NULL->default (' \f\t\n\r\v\0')
*/
PHPAPI zend_string *php_trim(zend_string *str, const char *what, size_t what_len, int mode)
{
Expand Down
6 changes: 6 additions & 0 deletions ext/standard/tests/strings/trim.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ var_dump("ABC" === trim("ABC\x50\xC1\x60\x90","\x50..\xC1"));
var_dump("ABC\x50\xC1" === trim("ABC\x50\xC1\x60\x90","\x51..\xC0"));
var_dump("ABC\x50" === trim("ABC\x50\xC1\x60\x90","\x51..\xC1"));
var_dump("ABC" === trim("ABC\x50\xC1\x60\x90","\x50..\xC1"));
var_dump("ABC" === trim("\fABC\f"));
var_dump("ABC" === ltrim("\fABC"));
var_dump("ABC" === rtrim("ABC\f"));

?>
--EXPECT--
Expand All @@ -36,3 +39,6 @@ bool(true)
bool(true)
bool(true)
bool(true)
bool(true)
bool(true)
bool(true)
Loading