Skip to content

Commit 959f9f5

Browse files
committed
Add mb_ucfirst and mb_lcfirst functions
1 parent 9c7b391 commit 959f9f5

File tree

4 files changed

+110
-1
lines changed

4 files changed

+110
-1
lines changed

ext/mbstring/mbstring.c

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2953,6 +2953,72 @@ PHP_FUNCTION(mb_strtolower)
29532953
RETURN_STR(mbstring_convert_case(PHP_UNICODE_CASE_LOWER, ZSTR_VAL(str), ZSTR_LEN(str), enc));
29542954
}
29552955

2956+
static zend_string* php_mb_ucfirst(zend_string *str, const mbfl_encoding *enc)
2957+
{
2958+
zend_string *first, *second, *head;
2959+
first = mb_get_substr(str, 0, 1, enc);
2960+
second = mb_get_substr(str, 1, MBFL_SUBSTR_UNTIL_END, enc);
2961+
head = mbstring_convert_case(PHP_UNICODE_CASE_UPPER, ZSTR_VAL(first), ZSTR_LEN(first), enc);
2962+
zend_string_release(first);
2963+
2964+
zend_string *retval = zend_string_concat2(ZSTR_VAL(head), ZSTR_LEN(head), ZSTR_VAL(second), ZSTR_LEN(second));
2965+
zend_string_release(head);
2966+
zend_string_release(second);
2967+
2968+
return retval;
2969+
}
2970+
2971+
PHP_FUNCTION(mb_ucfirst)
2972+
{
2973+
zend_string *str, *from_encoding = NULL;
2974+
2975+
ZEND_PARSE_PARAMETERS_START(1, 2)
2976+
Z_PARAM_STR(str)
2977+
Z_PARAM_OPTIONAL
2978+
Z_PARAM_STR_OR_NULL(from_encoding)
2979+
ZEND_PARSE_PARAMETERS_END();
2980+
2981+
const mbfl_encoding *enc = php_mb_get_encoding(from_encoding, 2);
2982+
if (!enc) {
2983+
RETURN_THROWS();
2984+
}
2985+
2986+
RETVAL_STR(php_mb_ucfirst(str, enc));
2987+
}
2988+
2989+
static zend_string* php_mb_lcfirst(zend_string *str, const mbfl_encoding *enc)
2990+
{
2991+
zend_string *first, *second, *head;
2992+
first = mb_get_substr(str, 0, 1, enc);
2993+
second = mb_get_substr(str, 1, MBFL_SUBSTR_UNTIL_END, enc);
2994+
head = mbstring_convert_case(PHP_UNICODE_CASE_LOWER, ZSTR_VAL(first), ZSTR_LEN(first), enc);
2995+
zend_string_release(first);
2996+
2997+
zend_string *retval = zend_string_concat2(ZSTR_VAL(head), ZSTR_LEN(head), ZSTR_VAL(second), ZSTR_LEN(second));
2998+
zend_string_release(head);
2999+
zend_string_release(second);
3000+
3001+
return retval;
3002+
}
3003+
3004+
PHP_FUNCTION(mb_lcfirst)
3005+
{
3006+
zend_string *str, *from_encoding = NULL;
3007+
3008+
ZEND_PARSE_PARAMETERS_START(1, 2)
3009+
Z_PARAM_STR(str)
3010+
Z_PARAM_OPTIONAL
3011+
Z_PARAM_STR_OR_NULL(from_encoding)
3012+
ZEND_PARSE_PARAMETERS_END();
3013+
3014+
const mbfl_encoding *enc = php_mb_get_encoding(from_encoding, 2);
3015+
if (!enc) {
3016+
RETURN_THROWS();
3017+
}
3018+
3019+
RETVAL_STR(php_mb_lcfirst(str, enc));
3020+
}
3021+
29563022
typedef enum {
29573023
MB_LTRIM = 1,
29583024
MB_RTRIM = 2,

ext/mbstring/mbstring.stub.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,10 @@ function mb_strtoupper(string $string, ?string $encoding = null): string {}
135135
/** @refcount 1 */
136136
function mb_strtolower(string $string, ?string $encoding = null): string {}
137137

138+
function mb_ucfirst(string $string, ?string $encoding = null): string {}
139+
140+
function mb_lcfirst(string $string, ?string $encoding = null): string {}
141+
138142
function mb_trim(string $string, string $characters = " \f\n\r\t\v\x00\u{00A0}\u{1680}\u{2000}\u{2001}\u{2002}\u{2003}\u{2004}\u{2005}\u{2006}\u{2007}\u{2008}\u{2009}\u{200A}\u{2028}\u{2029}\u{202F}\u{205F}\u{3000}\u{0085}\u{180E}", ?string $encoding = null): string {}
139143

140144
function mb_ltrim(string $string, string $characters = " \f\n\r\t\v\x00\u{00A0}\u{1680}\u{2000}\u{2001}\u{2002}\u{2003}\u{2004}\u{2005}\u{2006}\u{2007}\u{2008}\u{2009}\u{200A}\u{2028}\u{2029}\u{202F}\u{205F}\u{3000}\u{0085}\u{180E}", ?string $encoding = null): string {}

ext/mbstring/mbstring_arginfo.h

Lines changed: 9 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
--TEST--
2+
mb_ucfirst(), mb_lcfirst functions tests
3+
--EXTENSIONS--
4+
mbstring
5+
--FILE--
6+
<?php
7+
mb_internal_encoding("UTF-8");
8+
echo "== Empty String ==\n";
9+
var_dump(mb_ucfirst(""));
10+
var_dump(mb_lcfirst(""));
11+
echo "== mb_ucfirst ==\n";
12+
var_dump(mb_ucfirst("ab"));
13+
var_dump(mb_ucfirst("ABS"));
14+
var_dump(mb_ucfirst("đắt quá!"));
15+
echo "== mb_lcfirst ==\n";
16+
var_dump(mb_lcfirst("ABS"));
17+
var_dump(mb_lcfirst("Xin chào"));
18+
var_dump(mb_lcfirst("Đẹp quá!"));
19+
?>
20+
--EXPECT--
21+
== Empty String ==
22+
string(0) ""
23+
string(0) ""
24+
== mb_ucfirst ==
25+
string(6) "Ab"
26+
string(9) "ABS"
27+
string(12) "Đắt quá!"
28+
== mb_lcfirst ==
29+
string(9) "aBS"
30+
string(9) "xin chào"
31+
string(12) "đẹp quá!"

0 commit comments

Comments
 (0)