Skip to content

[intl] Fix empty-needle grapheme_strpos offsets - #23519

Open
iliaal wants to merge 1 commit into
php:PHP-8.4from
iliaal:fix/grapheme-empty-offset-84
Open

[intl] Fix empty-needle grapheme_strpos offsets#23519
iliaal wants to merge 1 commit into
php:PHP-8.4from
iliaal:fix/grapheme-empty-offset-84

Conversation

@iliaal

@iliaal iliaal commented Aug 31, 2026

Copy link
Copy Markdown
Member

grapheme_strpos() and grapheme_strrpos() with an empty needle return the raw UTF-16 code-unit position instead of a grapheme index, so on a haystack containing a non-BMP or combining grapheme the reported offset is too large: grapheme_strpos("😀x", "", 1) gives 3 where 1 is correct. The empty-needle early return in grapheme_strpos_utf16() skips the grapheme_count_graphemes() conversion that the non-empty search already does. grapheme_strstr() is unaffected: it consumes the separate raw UTF-16 out-parameter, not the return value.

Comment thread ext/intl/tests/grapheme_empty_offset_multibyte.phpt Outdated
grapheme_strpos_utf16() returned raw UTF-16 code-unit positions for an
empty needle instead of grapheme counts, so multi-code-unit graphemes
made strpos() and strrpos() over-report the offset. Convert the boundary
position like the non-empty search path; the ASCII fast paths coincide
and grapheme_strstr() consumes the separate raw UTF-16 out-parameter,
which stays correct.
@iliaal
iliaal force-pushed the fix/grapheme-empty-offset-84 branch from 8db9a85 to 922baf6 Compare August 31, 2026 17:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants