Skip to content
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

[RFC] Add mb_ucfirst and mb_lcfirst functions #13161

Merged
merged 15 commits into from
Mar 20, 2024
Merged
Show file tree
Hide file tree
Changes from 9 commits
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
1 change: 1 addition & 0 deletions NEWS
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ Intl:

MBString:
. Added mb_trim, mb_ltrim and mb_rtrim. (Yuya Hamada)
. Added mb_ucfirst and mb_lcfirst. (Yuya Hamada)

Opcache:
. Added large shared segments support for FreeBSD. (David Carlier)
Expand Down
2 changes: 2 additions & 0 deletions UPGRADING
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,8 @@ PDO_SQLITE:
- MBString:
. Added mb_trim, mb_ltrim and mb_rtrim functions.
RFC: https://wiki.php.net/rfc/mb_trim
. Added mb_ucfirst and mb_lcfirst functions.
RFC: https://wiki.php.net/rfc/mb_ucfirst

- Opcache:
. If JIT is enabled, PHP will now exit with a fatal error on startup in case
Expand Down
41 changes: 41 additions & 0 deletions ext/mbstring/mbstring.c
Original file line number Diff line number Diff line change
Expand Up @@ -2953,6 +2953,47 @@ PHP_FUNCTION(mb_strtolower)
RETURN_STR(mbstring_convert_case(PHP_UNICODE_CASE_LOWER, ZSTR_VAL(str), ZSTR_LEN(str), enc));
}

static void php_mb_ulcfirst(INTERNAL_FUNCTION_PARAMETERS, php_case_mode mode)
{
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();
}

zend_string *first = mb_get_substr(str, 0, 1, enc);
zend_string *second = mb_get_substr(str, 1, MBFL_SUBSTR_UNTIL_END, enc);
zend_string *head = mbstring_convert_case(mode, ZSTR_VAL(first), ZSTR_LEN(first), enc);

if (zend_string_equals(first, head)) {
RETVAL_STR(zend_string_copy(str));
youkidearitai marked this conversation as resolved.
Show resolved Hide resolved
}

zend_string_release_ex(first, false);
zend_string *retval = zend_string_concat2(ZSTR_VAL(head), ZSTR_LEN(head), ZSTR_VAL(second), ZSTR_LEN(second));
zend_string_release_ex(head, false);
zend_string_release_ex(second, false);

RETVAL_STR(retval);
}

PHP_FUNCTION(mb_ucfirst)
{
php_mb_ulcfirst(INTERNAL_FUNCTION_PARAM_PASSTHRU, PHP_UNICODE_CASE_TITLE);
}

PHP_FUNCTION(mb_lcfirst)
{
php_mb_ulcfirst(INTERNAL_FUNCTION_PARAM_PASSTHRU, PHP_UNICODE_CASE_LOWER);
}

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.

35 changes: 35 additions & 0 deletions ext/mbstring/tests/mb_ucfirst_lcfirst.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
--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á!"));
var_dump(mb_ucfirst("აბგ"));
var_dump(mb_ucfirst("lj"));
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á!"
string(9) "აბგ"
string(2) "Lj"
== mb_lcfirst ==
string(9) "aBS"
string(9) "xin chào"
string(12) "đẹp quá!"