From db4b1df0629d7d203594efc94b2f6a5c1f338ff0 Mon Sep 17 00:00:00 2001 From: Nicolas Grekas Date: Thu, 10 Sep 2020 15:22:45 +0200 Subject: [PATCH] [String] improve fix --- src/Symfony/Component/String/UnicodeString.php | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/src/Symfony/Component/String/UnicodeString.php b/src/Symfony/Component/String/UnicodeString.php index 5bc8917c2f..81bf8ea56b 100644 --- a/src/Symfony/Component/String/UnicodeString.php +++ b/src/Symfony/Component/String/UnicodeString.php @@ -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; }