Skip to content
Closed
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
27 changes: 24 additions & 3 deletions ext/intl/grapheme/grapheme_util.c
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,26 @@ void grapheme_substr_ascii(char *str, size_t str_len, int32_t f, int32_t l, char
goto finish; \
}

/* Find the next match that starts at or after "from". When "from" falls inside
* a contraction, such as a Thai prevowel and its consonant, ICU returns the match
* that starts at the contraction, before "from". Search again from each following
* grapheme boundary, so that a match overlapping the skipped one is not missed. */
static int32_t grapheme_usearch_next_from(UStringSearch *src, UBreakIterator *bi, int32_t from, UErrorCode *status)
{
int32_t pos = usearch_next(src, status);
int32_t boundary = from;

while (U_SUCCESS(*status) && pos != USEARCH_DONE && pos < from) {
boundary = ubrk_following(bi, boundary);
if (boundary == UBRK_DONE) {
return USEARCH_DONE;
}
usearch_setOffset(src, boundary, status);
pos = usearch_next(src, status);
}

return pos;
}

/* {{{ grapheme_strpos_utf16 - strrpos using utf16*/
int32_t grapheme_strpos_utf16(char *haystack, size_t haystack_len, char *needle, size_t needle_len, int32_t offset, int32_t *puchar_pos, int f_ignore_case, int last)
Expand Down Expand Up @@ -177,14 +197,15 @@ int32_t grapheme_strpos_utf16(char *haystack, size_t haystack_len, char *needle,
} else {
/* searching backwards is broken, so we search forwards, albeit it's less efficient */
int32_t prev_pos = USEARCH_DONE;
int32_t next_pos = 0;
do {
char_pos = usearch_next(src, &status);
char_pos = grapheme_usearch_next_from(src, bi, next_pos, &status);
if (char_pos == USEARCH_DONE || char_pos > offset_pos) {
char_pos = prev_pos;
break;
}
prev_pos = char_pos;
int32_t next_pos = ubrk_following(bi, char_pos);
next_pos = ubrk_following(bi, char_pos);
if (next_pos == UBRK_DONE) {
break;
}
Expand All @@ -193,7 +214,7 @@ int32_t grapheme_strpos_utf16(char *haystack, size_t haystack_len, char *needle,
} while(1);
}
} else {
char_pos = usearch_next(src, &status);
char_pos = grapheme_usearch_next_from(src, bi, offset_pos, &status);
}
STRPOS_CHECK_STATUS(status, "Error looking up string");
if(char_pos != USEARCH_DONE && ubrk_isBoundary(bi, char_pos)) {
Expand Down
42 changes: 42 additions & 0 deletions ext/intl/tests/grapheme_search_contraction.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
--TEST--
grapheme_*pos() with an offset inside a contraction
--EXTENSIONS--
intl
--FILE--
<?php
// In the root collation a Thai prevowel and the next consonant form one
// contraction, but two graphemes.
echo "=== No match before the offset ===\n";
var_dump(grapheme_strpos("เกเก", "เก", 1));
var_dump(grapheme_stripos("เกเก", "เก", 1));
var_dump(grapheme_strpos("เกเก", "เก", -3));
var_dump(grapheme_strpos("และและ", "และ", 1));

echo "=== Negative offsets do not loop forever ===\n";
var_dump(grapheme_strrpos("เกเกเกé", "เก", -2));
var_dump(grapheme_strripos("เกเกเกé", "เก", -2));
var_dump(grapheme_strrpos("เกเกเกé", "เก", -4));
var_dump(grapheme_strrpos("เรา", "เรา", -1));

echo "=== A match overlapping the skipped one is found ===\n";
var_dump(grapheme_strpos("เกเกเก", "เกเก", 1));
var_dump(grapheme_stripos("เกเกเก", "เกเก", 1));
var_dump(grapheme_strrpos("เกเกเก", "เกเก", -1));
var_dump(grapheme_strripos("เกเกเก", "เกเก", -1));
?>
--EXPECT--
=== No match before the offset ===
int(2)
int(2)
int(2)
int(3)
=== Negative offsets do not loop forever ===
int(4)
int(4)
int(2)
int(0)
=== A match overlapping the skipped one is found ===
int(2)
int(2)
int(2)
int(2)
Loading