From 9a929d1860d00cd3bd37fcebfd341a28ce9fc22a Mon Sep 17 00:00:00 2001 From: Lucas Michot Date: Mon, 29 Sep 2014 13:48:30 +0200 Subject: [PATCH 1/2] Simplify substr --- src/Stringy.php | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/src/Stringy.php b/src/Stringy.php index 9f887b8..545929c 100644 --- a/src/Stringy.php +++ b/src/Stringy.php @@ -1077,14 +1077,9 @@ class Stringy implements \Countable, \IteratorAggregate, \ArrayAccess public function substr($start, $length = null) { $stringy = self::create($this->str, $this->encoding); + $length = $length === null ? $this->length() : $length; - if ($length === null) { - $stringy->str = mb_substr($stringy->str, $start, - $stringy->length() - $start, $this->encoding); - } else { - $stringy->str = mb_substr($stringy->str, $start, $length, - $stringy->encoding); - } + $stringy->str = mb_substr($stringy->str, $start, $length, $stringy->encoding); return $stringy; } From d1df244678e61e76b1bc6d0b59c4194c8eaba007 Mon Sep 17 00:00:00 2001 From: Lucas Michot Date: Mon, 29 Sep 2014 14:05:35 +0200 Subject: [PATCH 2/2] Use $this --- src/Stringy.php | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/src/Stringy.php b/src/Stringy.php index 545929c..68eff3e 100644 --- a/src/Stringy.php +++ b/src/Stringy.php @@ -1076,12 +1076,10 @@ class Stringy implements \Countable, \IteratorAggregate, \ArrayAccess */ public function substr($start, $length = null) { - $stringy = self::create($this->str, $this->encoding); $length = $length === null ? $this->length() : $length; + $str = mb_substr($this->str, $start, $length, $this->encoding); - $stringy->str = mb_substr($stringy->str, $start, $length, $stringy->encoding); - - return $stringy; + return self::create($str, $this->encoding); } /**