Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

optimized twig_escape_filter

uses a static variable to avoid initializing the charsets array each
time the function is called; and does a hash lookup instead of array
search (saves a function call too).
  • Loading branch information...
commit 7b8d476ea5e842cf9fb863e180dd86a983949970 1 parent fba7f95
@arnaud-lb arnaud-lb authored
Showing with 22 additions and 15 deletions.
  1. +22 −15 lib/Twig/Extension/Core.php
View
37 lib/Twig/Extension/Core.php
@@ -503,21 +503,28 @@ function twig_escape_filter(Twig_Environment $env, $string, $type = 'html', $cha
case 'html':
// see http://php.net/htmlspecialchars
- if (in_array(strtolower($charset), array(
- 'iso-8859-1', 'iso8859-1',
- 'iso-8859-15', 'iso8859-15',
- 'utf-8',
- 'cp866', 'ibm866', '866',
- 'cp1251', 'windows-1251', 'win-1251', '1251',
- 'cp1252', 'windows-1252', '1252',
- 'koi8-r', 'koi8-ru', 'koi8r',
- 'big5', '950',
- 'gb2312', '936',
- 'big5-hkscs',
- 'shift_jis', 'sjis', '932',
- 'euc-jp', 'eucjp',
- 'iso8859-5', 'iso-8859-5', 'macroman',
- ))) {
+
+ // Using a static variable to avoid initializing the array
+ // each time the function is called. Moving the declaration on the
+ // top of the function slow downs other escaping types.
+ static $htmlspecialcharsCharsets = array(
+ 'iso-8859-1' => true, 'iso8859-1' => true,
+ 'iso-8859-15' => true, 'iso8859-15' => true,
+ 'utf-8' => true,
+ 'cp866' => true, 'ibm866' => true, '866' => true,
+ 'cp1251' => true, 'windows-1251' => true, 'win-1251' => true,
+ '1251' => true,
+ 'cp1252' => true, 'windows-1252' => true, '1252' => true,
+ 'koi8-r' => true, 'koi8-ru' => true, 'koi8r' => true,
+ 'big5' => true, '950' => true,
+ 'gb2312' => true, '936' => true,
+ 'big5-hkscs' => true,
+ 'shift_jis' => true, 'sjis' => true, '932' => true,
+ 'euc-jp' => true, 'eucjp' => true,
+ 'iso8859-5' => true, 'iso-8859-5' => true, 'macroman' => true,
+ );
+
+ if (isset($htmlspecialcharsCharsets[strtolower($charset)])) {
return htmlspecialchars($string, ENT_QUOTES | ENT_SUBSTITUTE, $charset);
}
Please sign in to comment.
Something went wrong with that request. Please try again.