Correct tokenize for empty strings.

This commit is contained in:
euromark 2013-12-19 15:58:08 +01:00
parent dc52f0bc0e
commit 3ecce19732
2 changed files with 8 additions and 4 deletions

View file

@ -303,6 +303,10 @@ class StringTest extends CakeTestCase {
$result = String::tokenize('tagA "single tag" tagB', ' ', '"', '"'); $result = String::tokenize('tagA "single tag" tagB', ' ', '"', '"');
$expected = array('tagA', '"single tag"', 'tagB'); $expected = array('tagA', '"single tag"', 'tagB');
$this->assertEquals($expected, $result); $this->assertEquals($expected, $result);
$result = String::tokenize('');
$expected = array();
$this->assertEquals($expected, $result);
} }
public function testReplaceWithQuestionMarkInString() { public function testReplaceWithQuestionMarkInString() {

View file

@ -99,17 +99,17 @@ class String {
/** /**
* Tokenizes a string using $separator, ignoring any instance of $separator that appears between * Tokenizes a string using $separator, ignoring any instance of $separator that appears between
* $leftBound and $rightBound * $leftBound and $rightBound.
* *
* @param string $data The data to tokenize * @param string $data The data to tokenize.
* @param string $separator The token to split the data on. * @param string $separator The token to split the data on.
* @param string $leftBound The left boundary to ignore separators in. * @param string $leftBound The left boundary to ignore separators in.
* @param string $rightBound The right boundary to ignore separators in. * @param string $rightBound The right boundary to ignore separators in.
* @return array Array of tokens in $data. * @return array Array of tokens in $data.
*/ */
public static function tokenize($data, $separator = ',', $leftBound = '(', $rightBound = ')') { public static function tokenize($data, $separator = ',', $leftBound = '(', $rightBound = ')') {
if (empty($data) || is_array($data)) { if (empty($data)) {
return $data; return array();
} }
$depth = 0; $depth = 0;