Skip to content
Draft
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
56 changes: 56 additions & 0 deletions ext/intl/grapheme/grapheme_string.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1135,4 +1135,60 @@ 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);

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

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