Skip to content

Commit

Permalink
Fix MB characters handling in split
Browse files Browse the repository at this point in the history
  • Loading branch information
1emming authored and fabpot committed Oct 10, 2014
1 parent 9fb15e3 commit 8cde52d
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 2 deletions.
22 changes: 20 additions & 2 deletions lib/Twig/Extension/Core.php
Expand Up @@ -173,7 +173,7 @@ public function getFilters()

// array helpers
new Twig_SimpleFilter('join', 'twig_join_filter'),
new Twig_SimpleFilter('split', 'twig_split_filter'),
new Twig_SimpleFilter('split', 'twig_split_filter', array('needs_environment' => true)),
new Twig_SimpleFilter('sort', 'twig_sort_filter'),
new Twig_SimpleFilter('merge', 'twig_array_merge'),
new Twig_SimpleFilter('batch', 'twig_array_batch'),
Expand Down Expand Up @@ -796,9 +796,27 @@ function twig_join_filter($value, $glue = '')
*
* @return array The split string as an array
*/
function twig_split_filter($value, $delimiter, $limit = null)
function twig_split_filter(Twig_Environment $env, $value, $delimiter, $limit = null)
{
if (empty($delimiter)) {
if (function_exists('mb_get_info') && null !== $charset = $env->getCharset()) {
if ($limit > 1) {
$length = mb_strlen($value, $charset);
if ($length < $limit) {
return array($value);
}

$r = array();
for ($i = 0; $i < $length; $i += $limit) {
$r[] = mb_substr($value, $i, $limit, $charset);
}

return $r;
}

return preg_split('/(?<!^)(?!$)/u', $value);
}

return str_split($value, null === $limit ? 1 : $limit);
}

Expand Down
2 changes: 2 additions & 0 deletions test/Twig/Tests/Fixtures/filters/split.test
Expand Up @@ -5,6 +5,7 @@
{{ foo|split(',')|join('-') }}
{{ foo|split(',', 3)|join('-') }}
{{ baz|split('')|join('-') }}
{{ baz|split('', 1)|join('-') }}
{{ baz|split('', 2)|join('-') }}
{{ foo|split(',', -2)|join('-') }}
--DATA--
Expand All @@ -14,5 +15,6 @@ one-two-three-four-five
one-two-three-four-five
one-two-three,four,five
1-2-3-4-5
1-2-3-4-5
12-34-5
one-two-three
24 changes: 24 additions & 0 deletions test/Twig/Tests/Fixtures/filters/split_utf8.test
@@ -0,0 +1,24 @@
--TEST--
"split" filter
--CONDITION--
function_exists('mb_get_info')
--TEMPLATE--
{{ "é"|split('', 10)|join('-') }}
{{ foo|split(',')|join('-') }}
{{ foo|split(',', 1)|join('-') }}
{{ foo|split(',', 2)|join('-') }}
{{ foo|split(',', 3)|join('-') }}
{{ baz|split('')|join('-') }}
{{ baz|split('', 1)|join('-') }}
{{ baz|split('', 2)|join('-') }}
--DATA--
return array('foo' => 'Ä,é,Äほ', 'baz' => 'éÄßごa',)
--EXPECT--
é
Ä-é-Äほ
Ä,é,Äほ
Ä-é,Äほ
Ä-é-Äほ
é-Ä-ß-ご-a
é-Ä-ß-ご-a
éÄ-ßご-a

0 comments on commit 8cde52d

Please sign in to comment.