{"action":"closed","issue":{"url":"https://api.github.com/repos/twigphp/Twig/issues/1477","labels_url":"https://api.github.com/repos/twigphp/Twig/issues/1477/labels{/name}","comments_url":"https://api.github.com/repos/twigphp/Twig/issues/1477/comments","events_url":"https://api.github.com/repos/twigphp/Twig/issues/1477/events","html_url":"https://github.com/twigphp/Twig/issues/1477","id":39813571,"number":1477,"title":"Proposal / inconsistency / bug - make the result of getCharSet always safe","user":{"login":"1emming","id":1867655,"avatar_url":"https://avatars.githubusercontent.com/u/1867655?v=3","gravatar_id":"","url":"https://api.github.com/users/1emming","html_url":"https://github.com/1emming","followers_url":"https://api.github.com/users/1emming/followers","following_url":"https://api.github.com/users/1emming/following{/other_user}","gists_url":"https://api.github.com/users/1emming/gists{/gist_id}","starred_url":"https://api.github.com/users/1emming/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/1emming/subscriptions","organizations_url":"https://api.github.com/users/1emming/orgs","repos_url":"https://api.github.com/users/1emming/repos","events_url":"https://api.github.com/users/1emming/events{/privacy}","received_events_url":"https://api.github.com/users/1emming/received_events","type":"User","site_admin":false},"labels":[],"state":"closed","locked":false,"assignee":null,"milestone":null,"comments":0,"created_at":"2014-08-08T11:21:24Z","updated_at":"2015-01-07T06:24:45Z","closed_at":"2015-01-07T06:24:45Z","body":"Hi all,\r\n\r\nThere are three things that could be improved if the result of `getCharSet` could always be trusted:\r\n \r\n* Filters like `slice` and `twig_reverse_filter` fallback to single byte character logic. \r\n There is no real need to this and is only done because the `mb` functions need to know the encoding.\r\n\r\n```php\r\n\t[...]\r\n    if (function_exists('mb_get_info') && null !== $charset = $env->getCharset()) {\r\n        return (string) mb_substr($item, $start, null === $length ? mb_strlen($item, $charset) - $start : $length, $charset);\r\n    }\r\n\t[...]\r\n```\t\t\r\n\r\n```php\t\r\n\t[...]\r\n\tif (null !== $charset = $env->getCharset()) {\r\n        [...]\r\n    }\r\n    return strrev((string) $item);\r\n```\r\n\r\n* The default of the escape filters like `twig_escape_filter` do a fallback to UTF-8.\r\nWhen a user wants default escaping with UTF-8 then the user probably wants to filter the content using the same encoding.\r\n\r\n* A filter like `twig_length_filter` trusts the result already (for value `null` this results in `PHP Warning:  mb_strlen(): Unknown encoding \"\" in /var/www/Twig/lib/Twig/Extension/Core.php on line 1198.`)\r\n\r\nWith the default already being `UTF-8` the fix I think makes sense is to disallow the user to set the encoding to `null` (/empty).\r\nWhen we can trust the result of `$env->getCharset()` then a lot of code can be simplified and the inconsistency between the default behaviors of filters and escape filters can be resolved. \r\n\r\nThis might/would be a BC-break so maybe this is something for version 2?"}}