Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[8.x] Add multibyte support to string padding helper functions #41899

Merged
merged 4 commits into from
Apr 11, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/Illuminate/Support/Str.php
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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