bug #1521 Fix MB characters handling in split (1emming)

This PR was squashed before being merged into the 1.16-dev branch (closes #1521).

Discussion
----------

Fix MB characters handling in split

The PR fixes using multibyte characters in the ```split```-filter

Since I messed up the rebase of https://github.com/fabpot/Twig/pull/1446 I created a new PR.
Let me know if this something that is kind of wanted, thanks!

Commits
-------

8cde52d Fix MB characters handling in split
This commit is contained in:
Fabien Potencier
2014-10-11 01:33:37 +02:00
3 changed files with 46 additions and 2 deletions
+20 -2
View File
@@ -173,7 +173,7 @@ class Twig_Extension_Core extends Twig_Extension
// 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'),
@@ -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);
}
@@ -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--
@@ -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
@@ -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