Skip to content

Commit 097cae9

Browse files
committed
Merge branch 'PHP-8.0'
* PHP-8.0: Fix #74264: grapheme_strrpos() broken for negative offsets
2 parents b626e89 + 8071bd2 commit 097cae9

File tree

2 files changed

+42
-5
lines changed

2 files changed

+42
-5
lines changed

ext/intl/grapheme/grapheme_util.c

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -157,16 +157,29 @@ int32_t grapheme_strpos_utf16(char *haystack, size_t haystack_len, char *needle,
157157
goto finish;
158158
}
159159
status = U_ZERO_ERROR;
160-
usearch_setOffset(src, offset_pos, &status);
160+
usearch_setOffset(src, last ? 0 : offset_pos, &status);
161161
STRPOS_CHECK_STATUS(status, "Invalid search offset");
162162
}
163163

164164

165165
if(last) {
166-
char_pos = usearch_last(src, &status);
167-
if(char_pos < offset_pos) {
168-
/* last one is beyound our start offset */
169-
char_pos = USEARCH_DONE;
166+
if (offset >= 0) {
167+
char_pos = usearch_last(src, &status);
168+
if(char_pos < offset_pos) {
169+
/* last one is beyond our start offset */
170+
char_pos = USEARCH_DONE;
171+
}
172+
} else {
173+
/* searching backwards is broken, so we search forwards, albeit it's less efficient */
174+
int32_t prev_pos = USEARCH_DONE;
175+
do {
176+
char_pos = usearch_next(src, &status);
177+
if (char_pos == USEARCH_DONE || char_pos > offset_pos) {
178+
char_pos = prev_pos;
179+
break;
180+
}
181+
prev_pos = char_pos;
182+
} while(1);
170183
}
171184
} else {
172185
char_pos = usearch_next(src, &status);

ext/intl/tests/bug74264.phpt

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
--TEST--
2+
Bug #74264 (grapheme_sttrpos() broken for negative offsets)
3+
--EXTENSIONS--
4+
intl
5+
--FILE--
6+
<?php
7+
foreach (range(-5, -1) as $offset) {
8+
var_dump(
9+
grapheme_strrpos('déjàààà', 'à', $offset),
10+
grapheme_strripos('DÉJÀÀÀÀ', 'à', $offset)
11+
);
12+
}
13+
?>
14+
--EXPECT--
15+
bool(false)
16+
bool(false)
17+
int(3)
18+
int(3)
19+
int(4)
20+
int(4)
21+
int(5)
22+
int(5)
23+
int(6)
24+
int(6)

0 commit comments

Comments
 (0)