Skip to content

Commit 3cf9598

Browse files
feature #466 [mbstring][PHP 8.4] Add mb_ucfirst and mb_lcfirst to polyfills (Ayesh)
This PR was merged into the 1.x branch. Discussion ---------- [mbstring][PHP 8.4] Add `mb_ucfirst` and `mb_lcfirst` to polyfills Adds polyfills for `mb_ucfirst` and `mb_lcfirst` functions based on the polyfill shown in PHP.Watch. It basically takes the first mb character, calls `mb_convert_case` with `MB_CASE_TITLE` or `MB_CASE_LOWER`, and returns with the concat of the remaining string. The tests are taken from the php-src PR. - [RFC](https://wiki.php.net/rfc/mb_ucfirst) - [php-src PR: php-src#13161](php/php-src#13161) - [PHP.Watch - PHP 8.4: New `mb_ucfirst` and `mb_lcfirst` functions](https://php.watch/versions/8.4/mb_ucfirst-mb_ucfirst) Commits ------- 5bd65b7 [mbstring][PHP 8.4] Add `mb_ucfirst` and `mb_lcfirst` to polyfills
2 parents e5e7ddb + 2b48d52 commit 3cf9598

File tree

3 files changed

+63
-0
lines changed

3 files changed

+63
-0
lines changed

Mbstring.php

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@
4848
* - mb_strstr - Finds first occurrence of a string within another
4949
* - mb_strwidth - Return width of string
5050
* - mb_substr_count - Count the number of substring occurrences
51+
* - mb_ucfirst - Make a string's first character uppercase
52+
* - mb_lcfirst - Make a string's first character lowercase
5153
*
5254
* Not implemented:
5355
* - mb_convert_kana - Convert "kana" one from another ("zen-kaku", "han-kaku" and more)
@@ -871,6 +873,51 @@ public static function mb_str_pad(string $string, int $length, string $pad_strin
871873
}
872874
}
873875

876+
public static function mb_ucfirst(string $string, ?string $encoding = null): string
877+
{
878+
if (null === $encoding) {
879+
$encoding = self::mb_internal_encoding();
880+
}
881+
882+
try {
883+
$validEncoding = @self::mb_check_encoding('', $encoding);
884+
} catch (\ValueError $e) {
885+
throw new \ValueError(sprintf('mb_ucfirst(): Argument #2 ($encoding) must be a valid encoding, "%s" given', $encoding));
886+
}
887+
888+
// BC for PHP 7.3 and lower
889+
if (!$validEncoding) {
890+
throw new \ValueError(sprintf('mb_ucfirst(): Argument #2 ($encoding) must be a valid encoding, "%s" given', $encoding));
891+
}
892+
893+
$firstChar = mb_substr($string, 0, 1, $encoding);
894+
$firstChar = mb_convert_case($firstChar, MB_CASE_TITLE, $encoding);
895+
896+
return $firstChar . mb_substr($string, 1, null, $encoding);
897+
}
898+
899+
public static function mb_lcfirst(string $string, ?string $encoding = null): string
900+
{
901+
if (null === $encoding) {
902+
$encoding = self::mb_internal_encoding();
903+
}
904+
905+
try {
906+
$validEncoding = @self::mb_check_encoding('', $encoding);
907+
} catch (\ValueError $e) {
908+
throw new \ValueError(sprintf('mb_lcfirst(): Argument #2 ($encoding) must be a valid encoding, "%s" given', $encoding));
909+
}
910+
911+
// BC for PHP 7.3 and lower
912+
if (!$validEncoding) {
913+
throw new \ValueError(sprintf('mb_lcfirst(): Argument #2 ($encoding) must be a valid encoding, "%s" given', $encoding));
914+
}
915+
$firstChar = mb_substr($string, 0, 1, $encoding);
916+
$firstChar = mb_convert_case($firstChar, MB_CASE_LOWER, $encoding);
917+
918+
return $firstChar . mb_substr($string, 1, null, $encoding);
919+
}
920+
874921
private static function getSubpart($pos, $part, $haystack, $encoding)
875922
{
876923
if (false === $pos) {

bootstrap.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,14 @@ function mb_str_split($string, $length = 1, $encoding = null) { return p\Mbstrin
136136
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); }
137137
}
138138

139+
if (!function_exists('mb_ucfirst')) {
140+
function mb_ucfirst(string $string, ?string $encoding = null): string { return p\Mbstring::mb_ucfirst($string, $encoding); }
141+
}
142+
143+
if (!function_exists('mb_lcfirst')) {
144+
function mb_lcfirst(string $string, ?string $encoding = null): string { return p\Mbstring::mb_lcfirst($string, $encoding); }
145+
}
146+
139147
if (extension_loaded('mbstring')) {
140148
return;
141149
}

bootstrap80.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,14 @@ function mb_str_split(?string $string, ?int $length = 1, ?string $encoding = nul
132132
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); }
133133
}
134134

135+
if (!function_exists('mb_ucfirst')) {
136+
function mb_ucfirst($string, ?string $encoding = null): string { return p\Mbstring::mb_ucfirst($string, $encoding); }
137+
}
138+
139+
if (!function_exists('mb_lcfirst')) {
140+
function mb_lcfirst($string, ?string $encoding = null): string { return p\Mbstring::mb_lcfirst($string, $encoding); }
141+
}
142+
135143
if (extension_loaded('mbstring')) {
136144
return;
137145
}

0 commit comments

Comments
 (0)