Skip to content

Commit

Permalink
Added Str trim methods (#6652)
Browse files Browse the repository at this point in the history
  • Loading branch information
huangdijia authored Apr 3, 2024
1 parent a42618e commit 5579ba1
Show file tree
Hide file tree
Showing 4 changed files with 133 additions and 4 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG-3.1.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# v3.1.17 - TBD

## Added

- [#6652](https://github.com/hyperf/hyperf/pull/6652) Added Str trim methods.

# v3.1.16 - 2024-04-02

## Added
Expand Down
50 changes: 49 additions & 1 deletion src/stringable/src/Str.php
Original file line number Diff line number Diff line change
Expand Up @@ -1085,9 +1085,57 @@ public static function reverse($value): string
return implode(array_reverse(mb_str_split($value)));
}

/**
* Remove all whitespace from both ends of a string.
*
* @param string $value
* @param null|string $charlist
* @return 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);
}

return trim($value, $charlist);
}

/**
* Remove all whitespace from the beginning of a string.
*
* @param string $value
* @param null|string $charlist
* @return string
*/
public static function ltrim($value, $charlist = null)
{
if ($charlist === null) {
return preg_replace('~^[\s\x{FEFF}\x{200B}\x{200E}]+~u', '', $value) ?? ltrim($value);
}

return ltrim($value, $charlist);
}

/**
* Remove all whitespace from the end of a string.
*
* @param string $value
* @param null|string $charlist
* @return string
*/
public static function rtrim($value, $charlist = null)
{
if ($charlist === null) {
return preg_replace('~[\s\x{FEFF}\x{200B}\x{200E}]+$~u', '', $value) ?? rtrim($value);
}

return rtrim($value, $charlist);
}

public static function squish($value): null|array|string
{
return preg_replace('~(\s|\x{3164}|\x{1160})+~u', ' ', preg_replace('~^[\s\x{FEFF}]+|[\s\x{FEFF}]+$~u', '', $value));
return preg_replace('~(\s|\x{3164}|\x{1160})+~u', ' ', static::trim($value));
}

public static function substrReplace($string, $replace, $offset = 0, $length = null): array|string
Expand Down
6 changes: 3 additions & 3 deletions src/stringable/src/Stringable.php
Original file line number Diff line number Diff line change
Expand Up @@ -741,7 +741,7 @@ public function substrCount($needle, $offset = null, $length = null)
*/
public function trim($characters = null)
{
return new static(trim(...array_merge([$this->value], func_get_args())));
return new static(Str::trim(...array_merge([$this->value], func_get_args())));
}

/**
Expand All @@ -752,7 +752,7 @@ public function trim($characters = null)
*/
public function ltrim($characters = null)
{
return new static(ltrim(...array_merge([$this->value], func_get_args())));
return new static(Str::ltrim(...array_merge([$this->value], func_get_args())));
}

/**
Expand All @@ -763,7 +763,7 @@ public function ltrim($characters = null)
*/
public function rtrim($characters = null)
{
return new static(rtrim(...array_merge([$this->value], func_get_args())));
return new static(Str::rtrim(...array_merge([$this->value], func_get_args())));
}

/**
Expand Down
77 changes: 77 additions & 0 deletions src/stringable/tests/StrTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -862,6 +862,83 @@ public function testReverse()
}
}

public function testTrim()
{
$this->assertSame('foo bar', Str::trim(' foo bar '));
$this->assertSame('foo bar', Str::trim('foo bar '));
$this->assertSame('foo bar', Str::trim(' foo bar'));
$this->assertSame('foo bar', Str::trim('foo bar'));
$this->assertSame(' foo bar ', Str::trim(' foo bar ', ''));
$this->assertSame('foo bar', Str::trim(' foo bar ', ' '));
$this->assertSame('foo bar', Str::trim('-foo bar_', '-_'));

$this->assertSame('foo bar', Str::trim(' foo bar '));

$this->assertSame('123', Str::trim(' 123 '));
$this->assertSame('', Str::trim(''));
$this->assertSame('', Str::trim(''));
$this->assertSame('', Str::trim(''));
$this->assertSame('', Str::trim(''));

$this->assertSame(
'foo bar',
Str::trim('
foo bar
')
);
$this->assertSame(
'foo
bar',
Str::trim('
foo
bar
')
);

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

public function testLtrim()
{
$this->assertSame('foo bar ', Str::ltrim(' foo bar '));

$this->assertSame('123 ', Str::ltrim(' 123 '));
$this->assertSame('', Str::ltrim(''));
$this->assertSame('', Str::ltrim(''));
$this->assertSame('', Str::ltrim(''));
$this->assertSame('', Str::ltrim(''));

$this->assertSame(
'foo bar
',
Str::ltrim('
foo bar
')
);
$this->assertSame("\xE9 ", Str::ltrim(" \xE9 "));
}

public function testRtrim()
{
$this->assertSame(' foo bar', Str::rtrim(' foo bar '));

$this->assertSame(' 123', Str::rtrim(' 123 '));
$this->assertSame('', Str::rtrim(''));
$this->assertSame('', Str::rtrim(''));
$this->assertSame('', Str::rtrim(''));
$this->assertSame('', Str::rtrim(''));

$this->assertSame(
'
foo bar',
Str::rtrim('
foo bar
')
);

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

public function testSquish()
{
$data = [
Expand Down

0 comments on commit 5579ba1

Please sign in to comment.