minor #38144 [String] improve fix (nicolas-grekas)

This PR was merged into the 5.1 branch.

Discussion
----------

[String] improve fix

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

Improves https://github.com/symfony/symfony/pull/38128
(let's wait for the CI before merging)

Commits
-------

db4b1df062 [String] improve fix
This commit is contained in:
Nicolas Grekas 2020-09-10 18:40:56 +02:00
commit ff29f3ae11

View File

@ -143,12 +143,12 @@ class UnicodeString extends AbstractUnicodeString
return null;
}
if ($this->length() <= $offset) {
try {
$i = $this->ignoreCase ? grapheme_stripos($this->string, $needle, $offset) : grapheme_strpos($this->string, $needle, $offset);
} catch (\ValueError $e) {
return null;
}
$i = $this->ignoreCase ? grapheme_stripos($this->string, $needle, $offset) : grapheme_strpos($this->string, $needle, $offset);
return false === $i ? null : $i;
}
@ -266,12 +266,12 @@ 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);
try {
$str->string = (string) grapheme_substr($this->string, $start, $length ?? \PHP_INT_MAX);
} catch (\ValueError $e) {
$str->string = '';
}
return $str;
}