Skip to content

Commit

Permalink
minor #38128 [String] ensure that the offset is part of the string (x…
Browse files Browse the repository at this point in the history
…abbuh)

This PR was merged into the 5.1 branch.

Discussion
----------

[String] ensure that the offset is part of the string

| Q             | A
| ------------- | ---
| Branch?       | 5.1
| Bug fix?      | no
| New feature?  | no
| Deprecations? | no
| Tickets       |
| License       | MIT
| Doc PR        |

Commits
-------

a6c046f ensure that the offset is part of the string
  • Loading branch information
fabpot committed Sep 10, 2020
2 parents 41cb27b + a6c046f commit 3c5a4ed
Showing 1 changed file with 9 additions and 1 deletion.
10 changes: 9 additions & 1 deletion src/Symfony/Component/String/UnicodeString.php
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,10 @@ public function indexOf($needle, int $offset = 0): ?int
return null;
}

if ($this->length() <= $offset) {
return null;
}

$i = $this->ignoreCase ? grapheme_stripos($this->string, $needle, $offset) : grapheme_strpos($this->string, $needle, $offset);

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

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

public function slice(int $start = 0, int $length = null): AbstractString
{
if ($this->length() <= $start) {
return new self();
}

$str = clone $this;
$str->string = (string) grapheme_substr($this->string, $start, $length ?? \PHP_INT_MAX);

Expand Down

0 comments on commit 3c5a4ed

Please sign in to comment.