From b4aa2111a95e7cd5f5c92119562bc9d26e1976fa Mon Sep 17 00:00:00 2001 From: Hugo Clarke-Wing <7689302+clarkewing@users.noreply.github.com> Date: Sat, 9 Apr 2022 12:58:19 +0200 Subject: [PATCH 1/4] Add multibyte support to string padding helper functions --- src/Illuminate/Support/Str.php | 27 ++++++++++++++++++++++--- tests/Support/SupportStrTest.php | 3 +++ tests/Support/SupportStringableTest.php | 3 +++ 3 files changed, 30 insertions(+), 3 deletions(-) diff --git a/src/Illuminate/Support/Str.php b/src/Illuminate/Support/Str.php index 2ffea75d16b8..d45f168bdcf7 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 static::mbStrPad($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 static::mbStrPad($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 static::mbStrPad($value, $length, $pad, STR_PAD_RIGHT); } /** @@ -1022,4 +1022,25 @@ public static function flushCache() static::$camelCache = []; static::$studlyCache = []; } + + /** + * Multibyte alternative to native `str_pad` function. + * Adapted from https://www.php.net/manual/en/ref.mbstring.php#90611 + * + * @param string $value + * @param int $length + * @param string $pad + * @param int $pad_type + * @param string|null $encoding + * @return string + */ + public static function mbStrPad($value, $length, $pad = ' ', $pad_type = STR_PAD_RIGHT, $encoding = null) + { + return str_pad( + $value, + strlen($value) - mb_strlen($value, $encoding) + $length, + $pad, + $pad_type, + ); + } } 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() From ece7730ae52c915cc5f59adeea9521ff61697442 Mon Sep 17 00:00:00 2001 From: Hugo Clarke-Wing <7689302+clarkewing@users.noreply.github.com> Date: Sat, 9 Apr 2022 13:09:35 +0200 Subject: [PATCH 2/4] Remove non-private `mbStrPad` method --- src/Illuminate/Support/Str.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Illuminate/Support/Str.php b/src/Illuminate/Support/Str.php index d45f168bdcf7..15487d5de908 100644 --- a/src/Illuminate/Support/Str.php +++ b/src/Illuminate/Support/Str.php @@ -1034,7 +1034,7 @@ public static function flushCache() * @param string|null $encoding * @return string */ - public static function mbStrPad($value, $length, $pad = ' ', $pad_type = STR_PAD_RIGHT, $encoding = null) + protected static function mbStrPad($value, $length, $pad = ' ', $pad_type = STR_PAD_RIGHT, $encoding = null) { return str_pad( $value, From a2019c2f71eec2601b4f22580691a30d35b9a881 Mon Sep 17 00:00:00 2001 From: Hugo Clarke-Wing <7689302+clarkewing@users.noreply.github.com> Date: Sat, 9 Apr 2022 13:11:18 +0200 Subject: [PATCH 3/4] Fix code style --- src/Illuminate/Support/Str.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Illuminate/Support/Str.php b/src/Illuminate/Support/Str.php index 15487d5de908..a894c27661d1 100644 --- a/src/Illuminate/Support/Str.php +++ b/src/Illuminate/Support/Str.php @@ -1025,7 +1025,7 @@ public static function flushCache() /** * Multibyte alternative to native `str_pad` function. - * Adapted from https://www.php.net/manual/en/ref.mbstring.php#90611 + * Adapted from https://www.php.net/manual/en/ref.mbstring.php#90611. * * @param string $value * @param int $length From 454876bc8e7016ec2a0d6bfff051c3781082866e Mon Sep 17 00:00:00 2001 From: Hugo Clarke-Wing <7689302+clarkewing@users.noreply.github.com> Date: Sat, 9 Apr 2022 13:23:55 +0200 Subject: [PATCH 4/4] Inline `str_pad` multibyte fix --- src/Illuminate/Support/Str.php | 27 +++------------------------ 1 file changed, 3 insertions(+), 24 deletions(-) diff --git a/src/Illuminate/Support/Str.php b/src/Illuminate/Support/Str.php index a894c27661d1..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 static::mbStrPad($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 static::mbStrPad($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 static::mbStrPad($value, $length, $pad, STR_PAD_RIGHT); + return str_pad($value, strlen($value) - mb_strlen($value) + $length, $pad, STR_PAD_RIGHT); } /** @@ -1022,25 +1022,4 @@ public static function flushCache() static::$camelCache = []; static::$studlyCache = []; } - - /** - * Multibyte alternative to native `str_pad` function. - * Adapted from https://www.php.net/manual/en/ref.mbstring.php#90611. - * - * @param string $value - * @param int $length - * @param string $pad - * @param int $pad_type - * @param string|null $encoding - * @return string - */ - protected static function mbStrPad($value, $length, $pad = ' ', $pad_type = STR_PAD_RIGHT, $encoding = null) - { - return str_pad( - $value, - strlen($value) - mb_strlen($value, $encoding) + $length, - $pad, - $pad_type, - ); - } }