Skip to content

Commit 53c7425

Browse files
authored
[8.x] Add multibyte support to string padding helper functions (#41899)
* Add multibyte support to string padding helper functions * Remove non-private `mbStrPad` method * Fix code style * Inline `str_pad` multibyte fix
1 parent 76a7b3c commit 53c7425

File tree

3 files changed

+9
-3
lines changed

3 files changed

+9
-3
lines changed

src/Illuminate/Support/Str.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -489,7 +489,7 @@ public static function matchAll($pattern, $subject)
489489
*/
490490
public static function padBoth($value, $length, $pad = ' ')
491491
{
492-
return str_pad($value, $length, $pad, STR_PAD_BOTH);
492+
return str_pad($value, strlen($value) - mb_strlen($value) + $length, $pad, STR_PAD_BOTH);
493493
}
494494

495495
/**
@@ -502,7 +502,7 @@ public static function padBoth($value, $length, $pad = ' ')
502502
*/
503503
public static function padLeft($value, $length, $pad = ' ')
504504
{
505-
return str_pad($value, $length, $pad, STR_PAD_LEFT);
505+
return str_pad($value, strlen($value) - mb_strlen($value) + $length, $pad, STR_PAD_LEFT);
506506
}
507507

508508
/**
@@ -515,7 +515,7 @@ public static function padLeft($value, $length, $pad = ' ')
515515
*/
516516
public static function padRight($value, $length, $pad = ' ')
517517
{
518-
return str_pad($value, $length, $pad, STR_PAD_RIGHT);
518+
return str_pad($value, strlen($value) - mb_strlen($value) + $length, $pad, STR_PAD_RIGHT);
519519
}
520520

521521
/**

tests/Support/SupportStrTest.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -609,18 +609,21 @@ public function testPadBoth()
609609
{
610610
$this->assertSame('__Alien___', Str::padBoth('Alien', 10, '_'));
611611
$this->assertSame(' Alien ', Str::padBoth('Alien', 10));
612+
$this->assertSame(' ❤MultiByte☆ ', Str::padBoth('❤MultiByte☆', 16));
612613
}
613614

614615
public function testPadLeft()
615616
{
616617
$this->assertSame('-=-=-Alien', Str::padLeft('Alien', 10, '-='));
617618
$this->assertSame(' Alien', Str::padLeft('Alien', 10));
619+
$this->assertSame(' ❤MultiByte☆', Str::padLeft('❤MultiByte☆', 16));
618620
}
619621

620622
public function testPadRight()
621623
{
622624
$this->assertSame('Alien-----', Str::padRight('Alien', 10, '-'));
623625
$this->assertSame('Alien ', Str::padRight('Alien', 10));
626+
$this->assertSame('❤MultiByte☆ ', Str::padRight('❤MultiByte☆', 16));
624627
}
625628

626629
public function testSwapKeywords(): void

tests/Support/SupportStringableTest.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -828,18 +828,21 @@ public function testPadBoth()
828828
{
829829
$this->assertSame('__Alien___', (string) $this->stringable('Alien')->padBoth(10, '_'));
830830
$this->assertSame(' Alien ', (string) $this->stringable('Alien')->padBoth(10));
831+
$this->assertSame(' ❤MultiByte☆ ', (string) $this->stringable('❤MultiByte☆')->padBoth(16));
831832
}
832833

833834
public function testPadLeft()
834835
{
835836
$this->assertSame('-=-=-Alien', (string) $this->stringable('Alien')->padLeft(10, '-='));
836837
$this->assertSame(' Alien', (string) $this->stringable('Alien')->padLeft(10));
838+
$this->assertSame(' ❤MultiByte☆', (string) $this->stringable('❤MultiByte☆')->padLeft(16));
837839
}
838840

839841
public function testPadRight()
840842
{
841843
$this->assertSame('Alien-----', (string) $this->stringable('Alien')->padRight(10, '-'));
842844
$this->assertSame('Alien ', (string) $this->stringable('Alien')->padRight(10));
845+
$this->assertSame('❤MultiByte☆ ', (string) $this->stringable('❤MultiByte☆')->padRight(16));
843846
}
844847

845848
public function testChunk()

0 commit comments

Comments
 (0)