1
0
mirror of https://github.com/php/php-src.git synced 2026-03-24 00:02:20 +01:00

RFC: Add Form Feed in Trim Functions (#20788)

RFC: https://wiki.php.net/rfc/trim_form_feed

Resolves GH-20783.
This commit is contained in:
Weilin Du
2026-03-10 05:28:50 +08:00
committed by GitHub
parent 0155b50984
commit da1e89fd3d
7 changed files with 29 additions and 20 deletions

2
NEWS
View File

@@ -122,6 +122,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

View File

@@ -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

View File

@@ -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

View File

@@ -1,5 +1,5 @@
/* This is a generated file, edit basic_functions.stub.php instead.
* Stub hash: 8d1c2a735f412f8571675c6b025c3a418b68fb65
* Stub hash: f5583557f058e4862750d1262296d7f59cb0eed0
* Has decl header: yes */
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_set_time_limit, 0, 1, _IS_BOOL, 0)
@@ -839,7 +839,7 @@ ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_trim, 0, 1, IS_STRING, 0)
ZEND_ARG_TYPE_INFO(0, string, IS_STRING, 0)
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, characters, IS_STRING, 0, "\" \\n\\r\\t\\v\\x00\"")
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, characters, IS_STRING, 0, "\" \\f\\n\\r\\t\\v\\x00\"")
ZEND_END_ARG_INFO()
#define arginfo_rtrim arginfo_trim

View File

@@ -1,8 +1,8 @@
/* This is a generated file, edit basic_functions.stub.php instead.
* Stub hash: 8d1c2a735f412f8571675c6b025c3a418b68fb65 */
* Stub hash: f5583557f058e4862750d1262296d7f59cb0eed0 */
#ifndef ZEND_BASIC_FUNCTIONS_DECL_8d1c2a735f412f8571675c6b025c3a418b68fb65_H
#define ZEND_BASIC_FUNCTIONS_DECL_8d1c2a735f412f8571675c6b025c3a418b68fb65_H
#ifndef ZEND_BASIC_FUNCTIONS_DECL_f5583557f058e4862750d1262296d7f59cb0eed0_H
#define ZEND_BASIC_FUNCTIONS_DECL_f5583557f058e4862750d1262296d7f59cb0eed0_H
typedef enum zend_enum_RoundingMode {
ZEND_ENUM_RoundingMode_HalfAwayFromZero = 1,
@@ -15,4 +15,4 @@ typedef enum zend_enum_RoundingMode {
ZEND_ENUM_RoundingMode_PositiveInfinity = 8,
} zend_enum_RoundingMode;
#endif /* ZEND_BASIC_FUNCTIONS_DECL_8d1c2a735f412f8571675c6b025c3a418b68fb65_H */
#endif /* ZEND_BASIC_FUNCTIONS_DECL_f5583557f058e4862750d1262296d7f59cb0eed0_H */

View File

@@ -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)
{
@@ -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;
@@ -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;
@@ -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)
{

View File

@@ -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--
@@ -36,3 +39,6 @@ bool(true)
bool(true)
bool(true)
bool(true)
bool(true)
bool(true)
bool(true)