From 53c742531fe49f8a7c2cd2083e50164bf8d9c644 Mon Sep 17 00:00:00 2001 From: Hugo Clarke-Wing <7689302+clarkewing@users.noreply.github.com> Date: Mon, 11 Apr 2022 16:26:37 +0200 Subject: [PATCH] [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 --- src/Illuminate/Support/Str.php | 6 +++--- tests/Support/SupportStrTest.php | 3 +++ tests/Support/SupportStringableTest.php | 3 +++ 3 files changed, 9 insertions(+), 3 deletions(-) diff --git a/src/Illuminate/Support/Str.php b/src/Illuminate/Support/Str.php index 2ffea75d16b8..66028c8beecc 100644 --- a/src/Illuminate/Support/Str.php +++ b/src/Illuminate/Support/Str.php @@ -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); } /** @@ -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); } /** @@ -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); } /** diff --git a/tests/Support/SupportStrTest.php b/tests/Support/SupportStrTest.php index 1886bc69fd5b..0daa6248c939 100755 --- a/tests/Support/SupportStrTest.php +++ b/tests/Support/SupportStrTest.php @@ -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 diff --git a/tests/Support/SupportStringableTest.php b/tests/Support/SupportStringableTest.php index c73db92398ee..4c813c3d57a5 100644 --- a/tests/Support/SupportStringableTest.php +++ b/tests/Support/SupportStringableTest.php @@ -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()