Skip to content

Commit b61bac2

Browse files
committed
[RFC] Add grapheme_strrev function
Add more tests Arabic for grapheme_strrev function.
1 parent eea9a62 commit b61bac2

File tree

4 files changed

+65
-1
lines changed

4 files changed

+65
-1
lines changed

ext/intl/grapheme/grapheme_string.cpp

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1135,4 +1135,60 @@ 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+
1178+
current = ZSTR_LEN(string);
1179+
for (end = pstr; pos != UBRK_DONE; ) {
1180+
pos = ubrk_previous(bi);
1181+
end_len = current - pos;
1182+
for (int32_t j = 0; j < end_len; j++) {
1183+
*p++ = *(pstr + pos + j);
1184+
}
1185+
current = pos;
1186+
}
1187+
1188+
RETVAL_NEW_STR(ret);
1189+
ubrk_close(bi);
1190+
close:
1191+
utext_close(ut);
1192+
}
1193+
11381194
/* }}} */

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.
805 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)