-
Notifications
You must be signed in to change notification settings - Fork 8k
[RFC] Add grapheme_strrev function #20949
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
Changes from all commits
80e6b92
63fac4e
e90e972
adb0df9
c2c28fd
bd491e9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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); | ||
| if (pos == UBRK_DONE) { | ||
| goto ubrk_end; | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh, Indeed. So I use
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I suppose the empty string should be returned and the string allocation should be postponed to after this is. |
||
| } | ||
|
|
||
| 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); | ||
| } | ||
|
|
||
| /* }}} */ | ||
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
There was a problem hiding this comment.
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 ?
There was a problem hiding this comment.
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.There was a problem hiding this comment.
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
retpointer line 1172.Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
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
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I tried add
ubrk_endinRETVAL_NEW_STR.