Skip to content

Commit

Permalink
Fixing Str::trim to remove the default trim/ltrim/rtim characters " \…
Browse files Browse the repository at this point in the history
…n\r\t\v\0" (#52684)

* Fixing Str::trim to remove the default characters which are " \n\r\t\v\0"
Fixes #52681 (#52681)

* Fixing Str::trim to remove the default characters which are " \n\r\t\v\0"
Fixes #52681 (#52681)
  • Loading branch information
mathiasgrimm authored Sep 6, 2024
1 parent 455598c commit 167d554
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 3 deletions.
12 changes: 9 additions & 3 deletions src/Illuminate/Support/Str.php
Original file line number Diff line number Diff line change
Expand Up @@ -1497,7 +1497,9 @@ public static function snake($value, $delimiter = '_')
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\0";

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 @@ -1513,7 +1515,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\0";

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

return ltrim($value, $charlist);
Expand All @@ -1529,7 +1533,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\0";

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

return rtrim($value, $charlist);
Expand Down
30 changes: 30 additions & 0 deletions tests/Support/SupportStrTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -861,6 +861,16 @@ public function testTrim()
);

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

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

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 @@ -881,6 +891,16 @@ public function testLtrim()
')
);
$this->assertSame("\xE9 ", Str::ltrim(" \xE9 "));

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

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 @@ -902,6 +922,16 @@ public function testRtrim()
);

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

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

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 167d554

Please sign in to comment.