Skip to content

Commit 74faf61

Browse files
committed
ensure that the offset is part of the string
1 parent 7523a9c commit 74faf61

File tree

1 file changed

+9
-1
lines changed

1 file changed

+9
-1
lines changed

UnicodeString.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,10 @@ public function indexOf($needle, int $offset = 0): ?int
143143
return null;
144144
}
145145

146+
if ($this->length() <= $offset) {
147+
return null;
148+
}
149+
146150
$i = $this->ignoreCase ? grapheme_stripos($this->string, $needle, $offset) : grapheme_strpos($this->string, $needle, $offset);
147151

148152
return false === $i ? null : $i;
@@ -235,7 +239,7 @@ public function replace(string $from, string $to): AbstractString
235239
$result = '';
236240
$indexOf = $this->ignoreCase ? 'grapheme_stripos' : 'grapheme_strpos';
237241

238-
while (false !== $i = $indexOf($tail, $from)) {
242+
while ('' !== $tail && false !== $i = $indexOf($tail, $from)) {
239243
$slice = grapheme_substr($tail, 0, $i);
240244
$result .= $slice.$to;
241245
$tail = substr($tail, \strlen($slice) + \strlen($from));
@@ -262,6 +266,10 @@ public function replaceMatches(string $fromRegexp, $to): AbstractString
262266

263267
public function slice(int $start = 0, int $length = null): AbstractString
264268
{
269+
if ($this->length() <= $start) {
270+
return new self();
271+
}
272+
265273
$str = clone $this;
266274
$str->string = (string) grapheme_substr($this->string, $start, $length ?? \PHP_INT_MAX);
267275

0 commit comments

Comments
 (0)