From 7af92173b1c527813714e814a66d0e2d62e4e56c Mon Sep 17 00:00:00 2001 From: Carsten Brandt Date: Wed, 30 Jul 2014 13:27:30 +0200 Subject: [PATCH] length arg of byteSubstr is now optional --- framework/CHANGELOG.md | 1 + framework/helpers/BaseStringHelper.php | 7 +++-- .../framework/helpers/StringHelperTest.php | 28 +++++++++++++++++++ 3 files changed, 33 insertions(+), 3 deletions(-) diff --git a/framework/CHANGELOG.md b/framework/CHANGELOG.md index 2dd5831388..31c283ba96 100644 --- a/framework/CHANGELOG.md +++ b/framework/CHANGELOG.md @@ -82,6 +82,7 @@ Yii Framework 2 Change Log - Bug: URL encoding for the route parameter added to `\yii\web\UrlManager` (klimov-paul) - Bug: Fixed the bug that requesting protected or private action methods would cause 500 error instead of 404 (qiangxue) - Bug: Fixed Object of class Imagick could not be converted to string in CaptchaAction (eXprojects, cebe) +- Bug: Fixed wrong behavior of `StringHelper::byteSubstr()` in some edge cases (cebe) - Enh #87: Helper `yii\helpers\Security` converted into application component, cryptographic strength improved (klimov-paul) - Enh #422: Added Support for BIT(M) data type default values in Schema (cebe) - Enh #1160: Added $strict parameter to Inflector::camel2id() to handle consecutive uppercase chars (schmunk) diff --git a/framework/helpers/BaseStringHelper.php b/framework/helpers/BaseStringHelper.php index aac58ecaaa..d04d0fb3ec 100644 --- a/framework/helpers/BaseStringHelper.php +++ b/framework/helpers/BaseStringHelper.php @@ -34,13 +34,14 @@ class BaseStringHelper * This method ensures the string is treated as a byte array by using `mb_substr()`. * @param string $string the input string. Must be one character or longer. * @param integer $start the starting position - * @param integer $length the desired portion length + * @param integer $length the desired portion length. If not specified or `null`, there will be + * no limit on length i.e. the output will be until the end of the string. * @return string the extracted part of string, or FALSE on failure or an empty string. * @see http://www.php.net/manual/en/function.substr.php */ - public static function byteSubstr($string, $start, $length) + public static function byteSubstr($string, $start, $length = null) { - return mb_substr($string, $start, $length ?: mb_strlen($string, '8bit'), '8bit'); + return mb_substr($string, $start, $length === null ? mb_strlen($string, '8bit') : $length, '8bit'); } /** diff --git a/tests/unit/framework/helpers/StringHelperTest.php b/tests/unit/framework/helpers/StringHelperTest.php index 4f9a50cd1d..33d075b47c 100644 --- a/tests/unit/framework/helpers/StringHelperTest.php +++ b/tests/unit/framework/helpers/StringHelperTest.php @@ -26,6 +26,34 @@ class StringHelperTest extends TestCase { $this->assertEquals('th', StringHelper::byteSubstr('this', 0, 2)); $this->assertEquals('э', StringHelper::byteSubstr('это', 0, 2)); + + $this->assertEquals('abcdef', StringHelper::byteSubstr('abcdef', 0)); + $this->assertEquals('abcdef', StringHelper::byteSubstr('abcdef', 0, null)); + + $this->assertEquals('de', StringHelper::byteSubstr('abcdef', 3, 2)); + $this->assertEquals('def', StringHelper::byteSubstr('abcdef', 3)); + $this->assertEquals('def', StringHelper::byteSubstr('abcdef', 3, null)); + + $this->assertEquals('cd', StringHelper::byteSubstr('abcdef', -4, 2)); + $this->assertEquals('cdef', StringHelper::byteSubstr('abcdef', -4)); + $this->assertEquals('cdef', StringHelper::byteSubstr('abcdef', -4, null)); + + $this->assertEquals('', StringHelper::byteSubstr('abcdef', 4, 0)); + $this->assertEquals('', StringHelper::byteSubstr('abcdef', -4, 0)); + + $this->assertEquals('это', StringHelper::byteSubstr('это', 0)); + $this->assertEquals('это', StringHelper::byteSubstr('это', 0, null)); + + $this->assertEquals('т', StringHelper::byteSubstr('это', 2, 2)); + $this->assertEquals('то', StringHelper::byteSubstr('это', 2)); + $this->assertEquals('то', StringHelper::byteSubstr('это', 2, null)); + + $this->assertEquals('т', StringHelper::byteSubstr('это', -4, 2)); + $this->assertEquals('то', StringHelper::byteSubstr('это', -4)); + $this->assertEquals('то', StringHelper::byteSubstr('это', -4, null)); + + $this->assertEquals('', StringHelper::byteSubstr('это', 4, 0)); + $this->assertEquals('', StringHelper::byteSubstr('это', -4, 0)); } public function testBasename()