Skip to content

Commit 8d7699d

Browse files
committed
Merge branch 'PHP-8.6'
* PHP-8.6: ext/intl: Fix grapheme searches with an offset inside a contraction
2 parents 4c4e4b7 + 6d122e3 commit 8d7699d

2 files changed

Lines changed: 66 additions & 3 deletions

File tree

‎ext/intl/grapheme/grapheme_util.cpp‎

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,26 @@ void grapheme_substr_ascii(char *str, size_t str_len, int32_t f, int32_t l, char
9898
goto finish; \
9999
}
100100

101+
/* Find the next match that starts at or after "from". When "from" falls inside
102+
* a contraction, such as a Thai prevowel and its consonant, ICU returns the match
103+
* that starts at the contraction, before "from". Search again from each following
104+
* grapheme boundary, so that a match overlapping the skipped one is not missed. */
105+
static int32_t grapheme_usearch_next_from(UStringSearch *src, UBreakIterator *bi, int32_t from, UErrorCode *status)
106+
{
107+
int32_t pos = usearch_next(src, status);
108+
int32_t boundary = from;
109+
110+
while (U_SUCCESS(*status) && pos != USEARCH_DONE && pos < from) {
111+
boundary = ubrk_following(bi, boundary);
112+
if (boundary == UBRK_DONE) {
113+
return USEARCH_DONE;
114+
}
115+
usearch_setOffset(src, boundary, status);
116+
pos = usearch_next(src, status);
117+
}
118+
119+
return pos;
120+
}
101121

102122
/* {{{ grapheme_strpos_utf16 - strrpos using utf16*/
103123
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, const char* locale)
@@ -182,14 +202,15 @@ int32_t grapheme_strpos_utf16(char *haystack, size_t haystack_len, char *needle,
182202
} else {
183203
/* searching backwards is broken, so we search forwards, albeit it's less efficient */
184204
int32_t prev_pos = USEARCH_DONE;
205+
int32_t next_pos = 0;
185206
do {
186-
char_pos = usearch_next(src, &status);
207+
char_pos = grapheme_usearch_next_from(src, bi, next_pos, &status);
187208
if (char_pos == USEARCH_DONE || char_pos > offset_pos) {
188209
char_pos = prev_pos;
189210
break;
190211
}
191212
prev_pos = char_pos;
192-
int32_t next_pos = ubrk_following(bi, char_pos);
213+
next_pos = ubrk_following(bi, char_pos);
193214
if (next_pos == UBRK_DONE) {
194215
break;
195216
}
@@ -198,7 +219,7 @@ int32_t grapheme_strpos_utf16(char *haystack, size_t haystack_len, char *needle,
198219
} while(1);
199220
}
200221
} else {
201-
char_pos = usearch_next(src, &status);
222+
char_pos = grapheme_usearch_next_from(src, bi, offset_pos, &status);
202223
}
203224
STRPOS_CHECK_STATUS(status, "Error looking up string");
204225
if(char_pos != USEARCH_DONE && ubrk_isBoundary(bi, char_pos)) {
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
--TEST--
2+
grapheme_*pos() with an offset inside a contraction
3+
--EXTENSIONS--
4+
intl
5+
--FILE--
6+
<?php
7+
// In the root collation a Thai prevowel and the next consonant form one
8+
// contraction, but two graphemes.
9+
echo "=== No match before the offset ===\n";
10+
var_dump(grapheme_strpos("เกเก", "เก", 1));
11+
var_dump(grapheme_stripos("เกเก", "เก", 1));
12+
var_dump(grapheme_strpos("เกเก", "เก", -3));
13+
var_dump(grapheme_strpos("และและ", "และ", 1));
14+
15+
echo "=== Negative offsets do not loop forever ===\n";
16+
var_dump(grapheme_strrpos("เกเกเกé", "เก", -2));
17+
var_dump(grapheme_strripos("เกเกเกé", "เก", -2));
18+
var_dump(grapheme_strrpos("เกเกเกé", "เก", -4));
19+
var_dump(grapheme_strrpos("เรา", "เรา", -1));
20+
21+
echo "=== A match overlapping the skipped one is found ===\n";
22+
var_dump(grapheme_strpos("เกเกเก", "เกเก", 1));
23+
var_dump(grapheme_stripos("เกเกเก", "เกเก", 1));
24+
var_dump(grapheme_strrpos("เกเกเก", "เกเก", -1));
25+
var_dump(grapheme_strripos("เกเกเก", "เกเก", -1));
26+
?>
27+
--EXPECT--
28+
=== No match before the offset ===
29+
int(2)
30+
int(2)
31+
int(2)
32+
int(3)
33+
=== Negative offsets do not loop forever ===
34+
int(4)
35+
int(4)
36+
int(2)
37+
int(0)
38+
=== A match overlapping the skipped one is found ===
39+
int(2)
40+
int(2)
41+
int(2)
42+
int(2)

0 commit comments

Comments
 (0)