Skip to content

Commit da1e89f

Browse files
authored
RFC: Add Form Feed in Trim Functions (#20788)
RFC: https://wiki.php.net/rfc/trim_form_feed Resolves GH-20783.
1 parent 0155b50 commit da1e89f

File tree

7 files changed

+29
-20
lines changed

7 files changed

+29
-20
lines changed

NEWS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,8 @@ PHP NEWS
122122
- Standard:
123123
. Fixed bug GH-19926 (reset internal pointer earlier while splicing array
124124
while COW violation flag is still set). (alexandre-daubois)
125+
. Added form feed (\f) in the default trimmed characters of trim(), rtrim()
126+
and ltrim(). (Weilin Du)
125127
. Invalid mode values now throw in array_filter() instead of being silently
126128
defaulted to 0. (Jorg Sowa)
127129
. Fixed bug GH-21058 (error_log() crashes with message_type 3 and

UPGRADING

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ PHP 8.6 UPGRADE NOTES
4747
- Standard:
4848
. Invalid mode values now throw in array_filter() instead of being silently
4949
defaulted to 0.
50+
. Form feed (\f) is now added in the default trimmed characters of trim(),
51+
rtrim() and ltrim(). RFC: https://wiki.php.net/rfc/trim_form_feed
5052

5153
========================================
5254
2. New Features

ext/standard/basic_functions.stub.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2318,16 +2318,16 @@ function strcoll(string $string1, string $string2): int {}
23182318
* @frameless-function {"arity": 1}
23192319
* @frameless-function {"arity": 2}
23202320
*/
2321-
function trim(string $string, string $characters = " \n\r\t\v\0"): string {}
2321+
function trim(string $string, string $characters = " \f\n\r\t\v\0"): string {}
23222322

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

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

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

23322332
/**
23332333
* @compile-time-eval

ext/standard/basic_functions_arginfo.h

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ext/standard/basic_functions_decl.h

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ext/standard/string.c

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -515,11 +515,16 @@ static inline zend_result php_charmask(const unsigned char *input, size_t len, c
515515
}
516516
/* }}} */
517517

518+
static zend_always_inline bool php_is_whitespace(unsigned char c)
519+
{
520+
return c <= ' ' && (c == ' ' || c == '\f' || c == '\n' || c == '\r' || c == '\t' || c == '\v' || c == '\0');
521+
}
522+
518523
/* {{{ php_trim_int()
519524
* mode 1 : trim left
520525
* mode 2 : trim right
521526
* mode 3 : trim left and right
522-
* what indicates which chars are to be trimmed. NULL->default (' \t\n\r\v\0')
527+
* what indicates which chars are to be trimmed. NULL->default (' \f\t\n\r\v\0')
523528
*/
524529
static zend_always_inline zend_string *php_trim_int(zend_string *str, const char *what, size_t what_len, int mode)
525530
{
@@ -573,10 +578,7 @@ static zend_always_inline zend_string *php_trim_int(zend_string *str, const char
573578
} else {
574579
if (mode & 1) {
575580
while (start != end) {
576-
unsigned char c = (unsigned char)*start;
577-
578-
if (c <= ' ' &&
579-
(c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == '\v' || c == '\0')) {
581+
if (php_is_whitespace((unsigned char)*start)) {
580582
start++;
581583
} else {
582584
break;
@@ -585,10 +587,7 @@ static zend_always_inline zend_string *php_trim_int(zend_string *str, const char
585587
}
586588
if (mode & 2) {
587589
while (start != end) {
588-
unsigned char c = (unsigned char)*(end-1);
589-
590-
if (c <= ' ' &&
591-
(c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == '\v' || c == '\0')) {
590+
if (php_is_whitespace((unsigned char)*(end-1))) {
592591
end--;
593592
} else {
594593
break;
@@ -611,7 +610,7 @@ static zend_always_inline zend_string *php_trim_int(zend_string *str, const char
611610
* mode 1 : trim left
612611
* mode 2 : trim right
613612
* mode 3 : trim left and right
614-
* what indicates which chars are to be trimmed. NULL->default (' \t\n\r\v\0')
613+
* what indicates which chars are to be trimmed. NULL->default (' \f\t\n\r\v\0')
615614
*/
616615
PHPAPI zend_string *php_trim(zend_string *str, const char *what, size_t what_len, int mode)
617616
{

ext/standard/tests/strings/trim.phpt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ var_dump("ABC" === trim("ABC\x50\xC1\x60\x90","\x50..\xC1"));
1818
var_dump("ABC\x50\xC1" === trim("ABC\x50\xC1\x60\x90","\x51..\xC0"));
1919
var_dump("ABC\x50" === trim("ABC\x50\xC1\x60\x90","\x51..\xC1"));
2020
var_dump("ABC" === trim("ABC\x50\xC1\x60\x90","\x50..\xC1"));
21+
var_dump("ABC" === trim("\fABC\f"));
22+
var_dump("ABC" === ltrim("\fABC"));
23+
var_dump("ABC" === rtrim("ABC\f"));
2124

2225
?>
2326
--EXPECT--
@@ -36,3 +39,6 @@ bool(true)
3639
bool(true)
3740
bool(true)
3841
bool(true)
42+
bool(true)
43+
bool(true)
44+
bool(true)

0 commit comments

Comments
 (0)