Skip to content

Commit 54b4e19

Browse files
IonBazannicolas-grekas
authored andcommitted
[PHP 8.3] Polyfill mb_str_pad()
1 parent f9c7aff commit 54b4e19

File tree

3 files changed

+52
-0
lines changed

3 files changed

+52
-0
lines changed

Mbstring.php

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -818,6 +818,50 @@ public static function mb_ord($s, $encoding = null)
818818
return $code;
819819
}
820820

821+
public static function mb_str_pad(string $string, int $length, string $pad_string = ' ', int $pad_type = \STR_PAD_RIGHT, string $encoding = null): string
822+
{
823+
if (!\in_array($pad_type, [\STR_PAD_RIGHT, \STR_PAD_LEFT, \STR_PAD_BOTH], true)) {
824+
throw new \ValueError('mb_str_pad(): Argument #4 ($pad_type) must be STR_PAD_LEFT, STR_PAD_RIGHT, or STR_PAD_BOTH');
825+
}
826+
827+
if (null === $encoding) {
828+
$encoding = self::mb_internal_encoding();
829+
}
830+
831+
try {
832+
$validEncoding = @self::mb_check_encoding('', $encoding);
833+
} catch (\ValueError $e) {
834+
throw new \ValueError(sprintf('mb_str_pad(): Argument #5 ($encoding) must be a valid encoding, "%s" given', $encoding));
835+
}
836+
837+
// BC for PHP 7.3 and lower
838+
if (!$validEncoding) {
839+
throw new \ValueError(sprintf('mb_str_pad(): Argument #5 ($encoding) must be a valid encoding, "%s" given', $encoding));
840+
}
841+
842+
if (self::mb_strlen($pad_string, $encoding) <= 0) {
843+
throw new \ValueError('mb_str_pad(): Argument #3 ($pad_string) must be a non-empty string');
844+
}
845+
846+
$paddingRequired = $length - self::mb_strlen($string, $encoding);
847+
848+
if ($paddingRequired < 1) {
849+
return $string;
850+
}
851+
852+
switch ($pad_type) {
853+
case \STR_PAD_LEFT:
854+
return self::mb_substr(str_repeat($pad_string, $paddingRequired), 0, $paddingRequired, $encoding).$string;
855+
case \STR_PAD_RIGHT:
856+
return $string.self::mb_substr(str_repeat($pad_string, $paddingRequired), 0, $paddingRequired, $encoding);
857+
default:
858+
$leftPaddingLength = floor($paddingRequired / 2);
859+
$rightPaddingLength = $paddingRequired - $leftPaddingLength;
860+
861+
return self::mb_substr(str_repeat($pad_string, $leftPaddingLength), 0, $leftPaddingLength, $encoding).$string.self::mb_substr(str_repeat($pad_string, $rightPaddingLength), 0, $rightPaddingLength, $encoding);
862+
}
863+
}
864+
821865
private static function getSubpart($pos, $part, $haystack, $encoding)
822866
{
823867
if (false === $pos) {

bootstrap.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,10 @@ function mb_scrub($string, $encoding = null) { $encoding = null === $encoding ?
132132
function mb_str_split($string, $length = 1, $encoding = null) { return p\Mbstring::mb_str_split($string, $length, $encoding); }
133133
}
134134

135+
if (!function_exists('mb_str_pad')) {
136+
function mb_str_pad(string $string, int $length, string $pad_string = ' ', int $pad_type = STR_PAD_RIGHT, ?string $encoding = null): string { return p\Mbstring::mb_str_pad($string, $length, $pad_string, $pad_type, $encoding); }
137+
}
138+
135139
if (extension_loaded('mbstring')) {
136140
return;
137141
}

bootstrap80.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,10 @@ function mb_scrub(?string $string, ?string $encoding = null): string { $encoding
128128
function mb_str_split(?string $string, ?int $length = 1, ?string $encoding = null): array { return p\Mbstring::mb_str_split((string) $string, (int) $length, $encoding); }
129129
}
130130

131+
if (!function_exists('mb_str_pad')) {
132+
function mb_str_pad(string $string, int $length, string $pad_string = ' ', int $pad_type = STR_PAD_RIGHT, ?string $encoding = null): string { return p\Mbstring::mb_str_pad($string, $length, $pad_string, $pad_type, $encoding); }
133+
}
134+
131135
if (extension_loaded('mbstring')) {
132136
return;
133137
}

0 commit comments

Comments
 (0)