Skip to content

Commit 42292d9

Browse files
feature #435 [PHP 8.3] Polyfill mb_str_pad() (IonBazan)
This PR was squashed before being merged into the 1.28-dev branch. Discussion ---------- [PHP 8.3] Polyfill mb_str_pad() Polyfills the `mb_str_pad()` function added in PHP 8.3: https://wiki.php.net/rfc/mb_str_pad Test cases were taken from the RFC implementation and adapted to PHPUnit. Commits ------- 7ccd416 [PHP 8.3] Polyfill mb_str_pad()
2 parents 46180f4 + 54b4e19 commit 42292d9

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
@@ -827,6 +827,50 @@ public static function mb_ord($s, $encoding = null)
827827
return $code;
828828
}
829829

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