Commit 564e9567e8c for php
commit 564e9567e8c63ce0125004a7f396a29f4532c0af
Author: ChrisAbner <chrisabner@hotmail.com>
Date: Sat Dec 5 18:23:38 2026 -0500
ext/intl: Fix grapheme searches with an offset inside a contraction
When the search position falls inside a contraction that spans graphemes,
such as a Thai prevowel and its consonant, usearch_next() returns the
match that starts at the contraction, before that position:
grapheme_strpos("และและ", "และ", 1); // 0 instead of 3
grapheme_strrpos("เรา", "เรา", -1); // never returns
The first one also makes the usual loop over all matches
($offset = $pos + 1) never end. The second one comes from the negative
offset loop added in GH-24028, which sets the same position again
forever. It is in 8.4.27 and later.
The search now starts again from the following grapheme boundaries, so a
match that overlaps the skipped one is still found. This affects Thai,
Lao and other scripts with prevowels. On 8.5, a $locale with such
tailorings, like Danish "aa", reaches it too. Tests added.
Close GH-26101
diff --git a/NEWS b/NEWS
index adb9038c4eb..4496a7f380a 100644
--- a/NEWS
+++ b/NEWS
@@ -17,6 +17,7 @@ PHP NEWS
- Intl:
. Fixed stale or missing global error code and message in MessageFormatter
and IntlDateFormatter::formatObject(). (ChrisAbner)
+ . Fixed grapheme searches with an offset inside a contraction. (ChrisAbner)
- ODBC:
. Fixed bug GH-26053 (Odbc\Result not freed when its last reference is
diff --git a/ext/intl/grapheme/grapheme_util.c b/ext/intl/grapheme/grapheme_util.c
index cd337055d45..e74e46c2692 100644
--- a/ext/intl/grapheme/grapheme_util.c
+++ b/ext/intl/grapheme/grapheme_util.c
@@ -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)
@@ -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;
}
@@ -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)) {
diff --git a/ext/intl/tests/grapheme_search_contraction.phpt b/ext/intl/tests/grapheme_search_contraction.phpt
new file mode 100644
index 00000000000..124c6629d02
--- /dev/null
+++ b/ext/intl/tests/grapheme_search_contraction.phpt
@@ -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)