minor #38128 [String] ensure that the offset is part of the string (xabbuh)
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
-------
a6c046f3ab
ensure that the offset is part of the string
This commit is contained in:
commit
3c5a4ed06e
@ -143,6 +143,10 @@ class UnicodeString extends AbstractUnicodeString
|
||||
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;
|
||||
@ -235,7 +239,7 @@ class UnicodeString extends AbstractUnicodeString
|
||||
$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));
|
||||
@ -262,6 +266,10 @@ class UnicodeString extends AbstractUnicodeString
|
||||
|
||||
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);
|
||||
|
||||
|
Reference in New Issue
Block a user