Commit 7af92173 by Carsten Brandt

length arg of byteSubstr is now optional

parent cde71f43
......@@ -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)
......
......@@ -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');
}
/**
......
......@@ -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()
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment