Skip to content
Merged
Show file tree
Hide file tree
Changes from 13 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, LamentXU)
Comment thread
TimWolla marked this conversation as resolved.
Outdated
. 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
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
17 changes: 16 additions & 1 deletion tests/classes/tostring_001.phpt
Comment thread
LamentXU123 marked this conversation as resolved.
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,16 @@ class test3
return [];
}
}

class test4
{
function __toString()
{
echo __METHOD__ . "()\n";
return "Converted\f";
}
}

echo "====test1====\n";
$o = new test1;
print_r($o);
Expand Down Expand Up @@ -77,7 +87,9 @@ try {
} catch (Error $e) {
echo $e->getMessage(), "\n";
}

echo "====test11====\n";
$o = new test4();
var_dump(trim($o));
?>
====DONE====
--EXPECT--
Expand Down Expand Up @@ -132,4 +144,7 @@ object(test3)#2 (0) {
}
test3::__toString()
test3::__toString(): Return value must be of type string, array returned
====test11====
test4::__toString()
string(9) "Converted"
====DONE====
Loading