Skip to content

[RFC] Add mb_ucfirst and mb_lcfirst functions #13161

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 15 commits into from
Mar 20, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 66 additions & 0 deletions ext/mbstring/mbstring.c
Original file line number Diff line number Diff line change
Expand Up @@ -2953,6 +2953,72 @@ PHP_FUNCTION(mb_strtolower)
RETURN_STR(mbstring_convert_case(PHP_UNICODE_CASE_LOWER, ZSTR_VAL(str), ZSTR_LEN(str), enc));
}

static zend_string* php_mb_ucfirst(zend_string *str, const mbfl_encoding *enc)
{
zend_string *first, *second, *head;
first = mb_get_substr(str, 0, 1, enc);
second = mb_get_substr(str, 1, MBFL_SUBSTR_UNTIL_END, enc);
head = mbstring_convert_case(PHP_UNICODE_CASE_UPPER, ZSTR_VAL(first), ZSTR_LEN(first), enc);
zend_string_release(first);

zend_string *retval = zend_string_concat2(ZSTR_VAL(head), ZSTR_LEN(head), ZSTR_VAL(second), ZSTR_LEN(second));
zend_string_release(head);
zend_string_release(second);

return retval;
}

PHP_FUNCTION(mb_ucfirst)
{
zend_string *str, *from_encoding = NULL;

ZEND_PARSE_PARAMETERS_START(1, 2)
Z_PARAM_STR(str)
Z_PARAM_OPTIONAL
Z_PARAM_STR_OR_NULL(from_encoding)
ZEND_PARSE_PARAMETERS_END();

const mbfl_encoding *enc = php_mb_get_encoding(from_encoding, 2);
if (!enc) {
RETURN_THROWS();
}

RETVAL_STR(php_mb_ucfirst(str, enc));
}

static zend_string* php_mb_lcfirst(zend_string *str, const mbfl_encoding *enc)
{
zend_string *first, *second, *head;
first = mb_get_substr(str, 0, 1, enc);
second = mb_get_substr(str, 1, MBFL_SUBSTR_UNTIL_END, enc);
head = mbstring_convert_case(PHP_UNICODE_CASE_LOWER, ZSTR_VAL(first), ZSTR_LEN(first), enc);
zend_string_release(first);

zend_string *retval = zend_string_concat2(ZSTR_VAL(head), ZSTR_LEN(head), ZSTR_VAL(second), ZSTR_LEN(second));
zend_string_release(head);
zend_string_release(second);

return retval;
}

PHP_FUNCTION(mb_lcfirst)
{
zend_string *str, *from_encoding = NULL;

ZEND_PARSE_PARAMETERS_START(1, 2)
Z_PARAM_STR(str)
Z_PARAM_OPTIONAL
Z_PARAM_STR_OR_NULL(from_encoding)
ZEND_PARSE_PARAMETERS_END();

const mbfl_encoding *enc = php_mb_get_encoding(from_encoding, 2);
if (!enc) {
RETURN_THROWS();
}

RETVAL_STR(php_mb_lcfirst(str, enc));
}

typedef enum {
MB_LTRIM = 1,
MB_RTRIM = 2,
Expand Down
4 changes: 4 additions & 0 deletions ext/mbstring/mbstring.stub.php
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,10 @@ function mb_strtoupper(string $string, ?string $encoding = null): string {}
/** @refcount 1 */
function mb_strtolower(string $string, ?string $encoding = null): string {}

function mb_ucfirst(string $string, ?string $encoding = null): string {}

function mb_lcfirst(string $string, ?string $encoding = null): string {}

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 {}

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 {}
Expand Down
10 changes: 9 additions & 1 deletion ext/mbstring/mbstring_arginfo.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

31 changes: 31 additions & 0 deletions ext/mbstring/tests/mb_ucfirst_lcfirst.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
--TEST--
mb_ucfirst(), mb_lcfirst functions tests
--EXTENSIONS--
mbstring
--FILE--
<?php
mb_internal_encoding("UTF-8");
echo "== Empty String ==\n";
var_dump(mb_ucfirst(""));
var_dump(mb_lcfirst(""));
echo "== mb_ucfirst ==\n";
var_dump(mb_ucfirst("ab"));
var_dump(mb_ucfirst("ABS"));
var_dump(mb_ucfirst("đắt quá!"));
echo "== mb_lcfirst ==\n";
var_dump(mb_lcfirst("ABS"));
var_dump(mb_lcfirst("Xin chào"));
var_dump(mb_lcfirst("Đẹp quá!"));
?>
--EXPECT--
== Empty String ==
string(0) ""
string(0) ""
== mb_ucfirst ==
string(6) "Ab"
string(9) "ABS"
string(12) "Đắt quá!"
== mb_lcfirst ==
string(9) "aBS"
string(9) "xin chào"
string(12) "đẹp quá!"