Skip to content

Commit

Permalink
Fixed bug Str::trim cannot support the default rules "\n\r\t\v" for…
Browse files Browse the repository at this point in the history
… `trim/ltrim/rtim`. (#7068)
  • Loading branch information
guandeng authored Oct 9, 2024
1 parent b01dcfc commit 5467fc8
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 3 deletions.
12 changes: 9 additions & 3 deletions src/Str.php
Original file line number Diff line number Diff line change
Expand Up @@ -1251,7 +1251,9 @@ public static function reverse($value): string
public static function trim($value, $charlist = null)
{
if ($charlist === null) {
return preg_replace('~^[\s\x{FEFF}\x{200B}\x{200E}]+|[\s\x{FEFF}\x{200B}\x{200E}]+$~u', '', $value) ?? trim($value);
$trimDefaultCharacters = " \n\r\t\v";

return preg_replace('~^[\s\x{FEFF}\x{200B}\x{200E}' . $trimDefaultCharacters . ']+|[\s\x{FEFF}\x{200B}\x{200E}' . $trimDefaultCharacters . ']+$~u', '', $value) ?? trim($value);
}

return trim($value, $charlist);
Expand All @@ -1267,7 +1269,9 @@ public static function trim($value, $charlist = null)
public static function ltrim($value, $charlist = null)
{
if ($charlist === null) {
return preg_replace('~^[\s\x{FEFF}\x{200B}\x{200E}]+~u', '', $value) ?? ltrim($value);
$ltrimDefaultCharacters = " \n\r\t\v";

return preg_replace('~^[\s\x{FEFF}\x{200B}\x{200E}' . $ltrimDefaultCharacters . ']+~u', '', $value) ?? ltrim($value);
}

return ltrim($value, $charlist);
Expand All @@ -1283,7 +1287,9 @@ public static function ltrim($value, $charlist = null)
public static function rtrim($value, $charlist = null)
{
if ($charlist === null) {
return preg_replace('~[\s\x{FEFF}\x{200B}\x{200E}]+$~u', '', $value) ?? rtrim($value);
$rtrimDefaultCharacters = " \n\r\t\v";

return preg_replace('~[\s\x{FEFF}\x{200B}\x{200E}' . $rtrimDefaultCharacters . ']+$~u', '', $value) ?? rtrim($value);
}

return rtrim($value, $charlist);
Expand Down
27 changes: 27 additions & 0 deletions tests/StrTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -979,6 +979,15 @@ public function testTrim()
);

$this->assertSame("\xE9", Str::trim(" \xE9 "));

$trimDefaultChars = [' ', "\n", "\r", "\t", "\v"];

foreach ($trimDefaultChars as $char) {
$this->assertSame('', Str::trim(" {$char} "));
$this->assertSame(trim(" {$char} "), Str::trim(" {$char} "));
$this->assertSame('foo bar', Str::trim("{$char} foo bar {$char}"));
$this->assertSame(trim("{$char} foo bar {$char}"), Str::trim("{$char} foo bar {$char}"));
}
}

public function testLtrim()
Expand All @@ -999,6 +1008,15 @@ public function testLtrim()
')
);
$this->assertSame("\xE9 ", Str::ltrim(" \xE9 "));

$ltrimDefaultChars = [' ', "\n", "\r", "\t", "\v"];

foreach ($ltrimDefaultChars as $char) {
$this->assertSame('', Str::ltrim(" {$char} "));
$this->assertSame(ltrim(" {$char} "), Str::ltrim(" {$char} "));
$this->assertSame("foo bar {$char}", Str::ltrim("{$char} foo bar {$char}"));
$this->assertSame(ltrim("{$char} foo bar {$char}"), Str::ltrim("{$char} foo bar {$char}"));
}
}

public function testRtrim()
Expand All @@ -1020,6 +1038,15 @@ public function testRtrim()
);

$this->assertSame(" \xE9", Str::rtrim(" \xE9 "));

$rtrimDefaultChars = [' ', "\n", "\r", "\t", "\v"];

foreach ($rtrimDefaultChars as $char) {
$this->assertSame('', Str::rtrim(" {$char} "));
$this->assertSame(rtrim(" {$char} "), Str::rtrim(" {$char} "));
$this->assertSame("{$char} foo bar", Str::rtrim("{$char} foo bar {$char}"));
$this->assertSame(rtrim("{$char} foo bar {$char}"), Str::rtrim("{$char} foo bar {$char}"));
}
}

public function testSquish()
Expand Down

0 comments on commit 5467fc8

Please sign in to comment.