Skip to content
Open
Show file tree
Hide file tree
Changes from all 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 @@ -37,6 +37,7 @@ PHP NEWS
(BogdanUngureanu)
. Fixed bug GH-20426 (Spoofchecker::setRestrictionLevel() error message
suggests missing constants). (DanielEScherzer)
. Added grapheme_strrev (Yuya Hamada)

- JSON:
. Enriched JSON last error / exception message with error location.
Expand Down
4 changes: 4 additions & 0 deletions UPGRADING
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,10 @@ PHP 8.6 UPGRADE NOTES
- Reflection:
. ReflectionConstant::inNamespace()

- Intl:
. `grapheme_strrev()` returns strrev for grapheme cluster unit.
RFC: https://wiki.php.net/rfc/grapheme_strrev

- Standard:
. `clamp()` returns the given value if in range, else return the nearest
bound.
Expand Down
59 changes: 59 additions & 0 deletions ext/intl/grapheme/grapheme_string.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1135,4 +1135,63 @@ U_CFUNC PHP_FUNCTION(grapheme_levenshtein)
efree(ustring1);
}

U_CFUNC PHP_FUNCTION(grapheme_strrev)
{
zend_string *string;
UText *ut = nullptr;
UErrorCode ustatus = U_ZERO_ERROR;
UBreakIterator *bi;
char *pstr, *end, *p;
zend_string *ret;
int32_t pos = 0, current = 0, end_len = 0;
unsigned char u_break_iterator_buffer[U_BRK_SAFECLONE_BUFFERSIZE];

ZEND_PARSE_PARAMETERS_START(1, 1)
Z_PARAM_STR(string)
ZEND_PARSE_PARAMETERS_END();

if (ZSTR_LEN(string) == 0) {
RETURN_EMPTY_STRING();
}

pstr = ZSTR_VAL(string);
ut = utext_openUTF8(ut, pstr, ZSTR_LEN(string), &ustatus);

if (U_FAILURE(ustatus)) {
intl_error_set_code(nullptr, ustatus);
intl_error_set_custom_msg(nullptr, "Error opening UTF-8 text");

RETVAL_FALSE;
goto close;
}

bi = nullptr;
ustatus = U_ZERO_ERROR;

bi = grapheme_get_break_iterator((void*)u_break_iterator_buffer, &ustatus );
ret = zend_string_alloc(ZSTR_LEN(string), 0);
p = ZSTR_VAL(ret);

ubrk_setUText(bi, ut, &ustatus);
pos = ubrk_last(bi);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: might be worth check in case pos == UBRK_DONE is to "jump to conclusion" to bypass the loop below wdyt ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indeed, I added RETVAL_EMPTY_STRING.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just one detail, do not forget you allocate ret pointer line 1172.

Copy link
Member

@devnexen devnexen Mar 1, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it might just simpler to raise the goto label at the 1190 line instead

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried add ubrk_end in RETVAL_NEW_STR .

if (pos == UBRK_DONE) {
goto ubrk_end;
}

current = ZSTR_LEN(string);
for (end = pstr; pos != UBRK_DONE; ) {
pos = ubrk_previous(bi);
end_len = current - pos;
for (int32_t j = 0; j < end_len; j++) {
*p++ = *(pstr + pos + j);
}
current = pos;
}
ubrk_end:
RETVAL_NEW_STR(ret);
ubrk_close(bi);
close:
utext_close(ut);
}

/* }}} */
2 changes: 2 additions & 0 deletions ext/intl/php_intl.stub.php
Original file line number Diff line number Diff line change
Expand Up @@ -445,6 +445,8 @@ function grapheme_str_split(string $string, int $length = 1): array|false {}

function grapheme_levenshtein(string $string1, string $string2, int $insertion_cost = 1, int $replacement_cost = 1, int $deletion_cost = 1, string $locale = ""): int|false {}

function grapheme_strrev(string $string): string|false {}

/** @param int $next */
function grapheme_extract(string $haystack, int $size, int $type = GRAPHEME_EXTR_COUNT, int $offset = 0, &$next = null): string|false {}

Expand Down
8 changes: 7 additions & 1 deletion ext/intl/php_intl_arginfo.h

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

Binary file added ext/intl/tests/grapheme_strrev.phpt
Binary file not shown.
Loading