Skip to content

Commit 7a1c261

Browse files
[RFC] Add grapheme_strrev function (#20949)
* [RFC] Add grapheme_strrev function Add more tests Arabic for grapheme_strrev function.
1 parent c7824b3 commit 7a1c261

File tree

6 files changed

+73
-1
lines changed

6 files changed

+73
-1
lines changed

NEWS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ PHP NEWS
3737
(BogdanUngureanu)
3838
. Fixed bug GH-20426 (Spoofchecker::setRestrictionLevel() error message
3939
suggests missing constants). (DanielEScherzer)
40+
. Added grapheme_strrev (Yuya Hamada)
4041

4142
- JSON:
4243
. Enriched JSON last error / exception message with error location.

UPGRADING

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,10 @@ PHP 8.6 UPGRADE NOTES
137137
. Added ReflectionProperty::isReadable() and ReflectionProperty::isWritable().
138138
RFC: https://wiki.php.net/rfc/isreadable-iswriteable
139139

140+
- Intl:
141+
. `grapheme_strrev()` returns strrev for grapheme cluster unit.
142+
RFC: https://wiki.php.net/rfc/grapheme_strrev
143+
140144
- Standard:
141145
. `clamp()` returns the given value if in range, else return the nearest
142146
bound.

ext/intl/grapheme/grapheme_string.cpp

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1135,4 +1135,63 @@ U_CFUNC PHP_FUNCTION(grapheme_levenshtein)
11351135
efree(ustring1);
11361136
}
11371137

1138+
U_CFUNC PHP_FUNCTION(grapheme_strrev)
1139+
{
1140+
zend_string *string;
1141+
UText *ut = nullptr;
1142+
UErrorCode ustatus = U_ZERO_ERROR;
1143+
UBreakIterator *bi;
1144+
char *pstr, *end, *p;
1145+
zend_string *ret;
1146+
int32_t pos = 0, current = 0, end_len = 0;
1147+
unsigned char u_break_iterator_buffer[U_BRK_SAFECLONE_BUFFERSIZE];
1148+
1149+
ZEND_PARSE_PARAMETERS_START(1, 1)
1150+
Z_PARAM_STR(string)
1151+
ZEND_PARSE_PARAMETERS_END();
1152+
1153+
if (ZSTR_LEN(string) == 0) {
1154+
RETURN_EMPTY_STRING();
1155+
}
1156+
1157+
pstr = ZSTR_VAL(string);
1158+
ut = utext_openUTF8(ut, pstr, ZSTR_LEN(string), &ustatus);
1159+
1160+
if (U_FAILURE(ustatus)) {
1161+
intl_error_set_code(nullptr, ustatus);
1162+
intl_error_set_custom_msg(nullptr, "Error opening UTF-8 text");
1163+
1164+
RETVAL_FALSE;
1165+
goto close;
1166+
}
1167+
1168+
bi = nullptr;
1169+
ustatus = U_ZERO_ERROR;
1170+
1171+
bi = grapheme_get_break_iterator((void*)u_break_iterator_buffer, &ustatus );
1172+
ret = zend_string_alloc(ZSTR_LEN(string), 0);
1173+
p = ZSTR_VAL(ret);
1174+
1175+
ubrk_setUText(bi, ut, &ustatus);
1176+
pos = ubrk_last(bi);
1177+
if (pos == UBRK_DONE) {
1178+
goto ubrk_end;
1179+
}
1180+
1181+
current = ZSTR_LEN(string);
1182+
for (end = pstr; pos != UBRK_DONE; ) {
1183+
pos = ubrk_previous(bi);
1184+
end_len = current - pos;
1185+
for (int32_t j = 0; j < end_len; j++) {
1186+
*p++ = *(pstr + pos + j);
1187+
}
1188+
current = pos;
1189+
}
1190+
ubrk_end:
1191+
RETVAL_NEW_STR(ret);
1192+
ubrk_close(bi);
1193+
close:
1194+
utext_close(ut);
1195+
}
1196+
11381197
/* }}} */

ext/intl/php_intl.stub.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -445,6 +445,8 @@ function grapheme_str_split(string $string, int $length = 1): array|false {}
445445

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

448+
function grapheme_strrev(string $string): string|false {}
449+
448450
/** @param int $next */
449451
function grapheme_extract(string $haystack, int $size, int $type = GRAPHEME_EXTR_COUNT, int $offset = 0, &$next = null): string|false {}
450452

ext/intl/php_intl_arginfo.h

Lines changed: 7 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
866 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)