Skip to content

Commit

Permalink
[8.x] Add multibyte support to string padding helper functions (#41899)
Browse files Browse the repository at this point in the history
* Add multibyte support to string padding helper functions

* Remove non-private `mbStrPad` method

* Fix code style

* Inline `str_pad` multibyte fix
  • Loading branch information
clarkewing committed Apr 11, 2022
1 parent 76a7b3c commit 53c7425
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 3 deletions.
6 changes: 3 additions & 3 deletions src/Illuminate/Support/Str.php
Expand Up @@ -489,7 +489,7 @@ public static function matchAll($pattern, $subject)
*/
public static function padBoth($value, $length, $pad = ' ')
{
return str_pad($value, $length, $pad, STR_PAD_BOTH);
return str_pad($value, strlen($value) - mb_strlen($value) + $length, $pad, STR_PAD_BOTH);
}

/**
Expand All @@ -502,7 +502,7 @@ public static function padBoth($value, $length, $pad = ' ')
*/
public static function padLeft($value, $length, $pad = ' ')
{
return str_pad($value, $length, $pad, STR_PAD_LEFT);
return str_pad($value, strlen($value) - mb_strlen($value) + $length, $pad, STR_PAD_LEFT);
}

/**
Expand All @@ -515,7 +515,7 @@ public static function padLeft($value, $length, $pad = ' ')
*/
public static function padRight($value, $length, $pad = ' ')
{
return str_pad($value, $length, $pad, STR_PAD_RIGHT);
return str_pad($value, strlen($value) - mb_strlen($value) + $length, $pad, STR_PAD_RIGHT);
}

/**
Expand Down
3 changes: 3 additions & 0 deletions tests/Support/SupportStrTest.php
Expand Up @@ -609,18 +609,21 @@ public function testPadBoth()
{
$this->assertSame('__Alien___', Str::padBoth('Alien', 10, '_'));
$this->assertSame(' Alien ', Str::padBoth('Alien', 10));
$this->assertSame(' ❤MultiByte☆ ', Str::padBoth('❤MultiByte☆', 16));
}

public function testPadLeft()
{
$this->assertSame('-=-=-Alien', Str::padLeft('Alien', 10, '-='));
$this->assertSame(' Alien', Str::padLeft('Alien', 10));
$this->assertSame(' ❤MultiByte☆', Str::padLeft('❤MultiByte☆', 16));
}

public function testPadRight()
{
$this->assertSame('Alien-----', Str::padRight('Alien', 10, '-'));
$this->assertSame('Alien ', Str::padRight('Alien', 10));
$this->assertSame('❤MultiByte☆ ', Str::padRight('❤MultiByte☆', 16));
}

public function testSwapKeywords(): void
Expand Down
3 changes: 3 additions & 0 deletions tests/Support/SupportStringableTest.php
Expand Up @@ -828,18 +828,21 @@ public function testPadBoth()
{
$this->assertSame('__Alien___', (string) $this->stringable('Alien')->padBoth(10, '_'));
$this->assertSame(' Alien ', (string) $this->stringable('Alien')->padBoth(10));
$this->assertSame(' ❤MultiByte☆ ', (string) $this->stringable('❤MultiByte☆')->padBoth(16));
}

public function testPadLeft()
{
$this->assertSame('-=-=-Alien', (string) $this->stringable('Alien')->padLeft(10, '-='));
$this->assertSame(' Alien', (string) $this->stringable('Alien')->padLeft(10));
$this->assertSame(' ❤MultiByte☆', (string) $this->stringable('❤MultiByte☆')->padLeft(16));
}

public function testPadRight()
{
$this->assertSame('Alien-----', (string) $this->stringable('Alien')->padRight(10, '-'));
$this->assertSame('Alien ', (string) $this->stringable('Alien')->padRight(10));
$this->assertSame('❤MultiByte☆ ', (string) $this->stringable('❤MultiByte☆')->padRight(16));
}

public function testChunk()
Expand Down

0 comments on commit 53c7425

Please sign in to comment.