mirror of
https://github.com/twigphp/Twig.git
synced 2026-09-16 04:16:28 +00:00
Optimize stripcslashes
This commit is contained in:
committed by
Fabien Potencier
parent
5541e6a6fc
commit
bc6e36dee9
+40
-50
@@ -50,6 +50,14 @@ class Lexer
|
|||||||
public const REGEX_DQ_STRING_PART = '/[^#"\\\\]*(?:(?:\\\\.|#(?!\{))[^#"\\\\]*)*/As';
|
public const REGEX_DQ_STRING_PART = '/[^#"\\\\]*(?:(?:\\\\.|#(?!\{))[^#"\\\\]*)*/As';
|
||||||
public const PUNCTUATION = '()[]{}?:.,|';
|
public const PUNCTUATION = '()[]{}?:.,|';
|
||||||
|
|
||||||
|
private const SPECIAL_CHARS = [
|
||||||
|
'f' => "\f",
|
||||||
|
'n' => "\n",
|
||||||
|
'r' => "\r",
|
||||||
|
't' => "\t",
|
||||||
|
'v' => "\v",
|
||||||
|
];
|
||||||
|
|
||||||
public function __construct(Environment $env, array $options = [])
|
public function __construct(Environment $env, array $options = [])
|
||||||
{
|
{
|
||||||
$this->env = $env;
|
$this->env = $env;
|
||||||
@@ -384,76 +392,58 @@ class Lexer
|
|||||||
|
|
||||||
private function stripcslashes(string $str, string $quoteType): string
|
private function stripcslashes(string $str, string $quoteType): string
|
||||||
{
|
{
|
||||||
if (!str_contains($str, '\\')) {
|
|
||||||
return $str;
|
|
||||||
}
|
|
||||||
|
|
||||||
$result = '';
|
$result = '';
|
||||||
$length = strlen($str);
|
$length = strlen($str);
|
||||||
$specialChars = [
|
|
||||||
'f' => "\f",
|
$i = 0;
|
||||||
'n' => "\n",
|
while ($i < $length) {
|
||||||
'r' => "\r",
|
if (false === $pos = strpos($str, '\\', $i)) {
|
||||||
't' => "\t",
|
$result .= substr($str, $i);
|
||||||
'v' => "\v",
|
break;
|
||||||
];
|
|
||||||
|
|
||||||
for ($i = 0; $i < $length; $i++) {
|
|
||||||
if ($str[$i] !== '\\' || $i + 1 >= $length) {
|
|
||||||
$result .= $str[$i];
|
|
||||||
|
|
||||||
continue;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
$nextChar = $str[$i + 1];
|
$result .= substr($str, $i, $pos - $i);
|
||||||
if (isset($specialChars[$nextChar])) {
|
$i = $pos + 1;
|
||||||
$result .= $specialChars[$nextChar];
|
|
||||||
$i++;
|
if ($i >= $length) {
|
||||||
} elseif ($nextChar === '\\') {
|
|
||||||
$result .= '\\';
|
$result .= '\\';
|
||||||
$i++;
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
$nextChar = $str[$i];
|
||||||
|
|
||||||
|
if (isset(self::SPECIAL_CHARS[$nextChar])) {
|
||||||
|
$result .= self::SPECIAL_CHARS[$nextChar];
|
||||||
|
} elseif ($nextChar === '\\') {
|
||||||
|
$result .= $nextChar;
|
||||||
} elseif ($nextChar === "'" || $nextChar === '"') {
|
} elseif ($nextChar === "'" || $nextChar === '"') {
|
||||||
if ($nextChar !== $quoteType) {
|
if ($nextChar !== $quoteType) {
|
||||||
trigger_deprecation('twig/twig', '3.12', 'Character "%s" at position %d does not need to be escaped anymore.', $nextChar, $i + 2);
|
trigger_deprecation('twig/twig', '3.12', 'Character "%s" at position %d does not need to be escaped anymore.', $nextChar, $i + 1);
|
||||||
}
|
}
|
||||||
$result .= $nextChar;
|
$result .= $nextChar;
|
||||||
$i++;
|
} elseif ($nextChar === '#' && $i + 1 < $length && $str[$i + 1] === '{') {
|
||||||
} elseif ($nextChar === '#' && $i + 2 < $length && $str[$i + 2] === '{') {
|
|
||||||
$result .= '#{';
|
$result .= '#{';
|
||||||
$i += 2;
|
$i++;
|
||||||
} elseif ($nextChar === 'x' && $i + 2 < $length && ctype_xdigit($str[$i + 2])) {
|
} elseif ($nextChar === 'x' && $i + 1 < $length && ctype_xdigit($str[$i + 1])) {
|
||||||
$hex = $str[$i + 2];
|
$hex = $str[++$i];
|
||||||
if ($i + 3 < $length && ctype_xdigit($str[$i + 3])) {
|
if ($i + 1 < $length && ctype_xdigit($str[$i + 1])) {
|
||||||
$hex .= $str[$i + 3];
|
$hex .= $str[++$i];
|
||||||
$i++;
|
|
||||||
}
|
}
|
||||||
$result .= chr(hexdec($hex));
|
$result .= chr(hexdec($hex));
|
||||||
$i += 2;
|
|
||||||
} elseif (ctype_digit($nextChar) && $nextChar < '8') {
|
} elseif (ctype_digit($nextChar) && $nextChar < '8') {
|
||||||
$octal = $nextChar;
|
$octal = $nextChar;
|
||||||
for ($j = $i + 1; $j <= 3; $j++) {
|
while ($i + 1 < $length && ctype_digit($str[$i + 1]) && $str[$i + 1] < '8' && strlen($octal) < 3) {
|
||||||
$o = $j + 1;
|
$octal .= $str[++$i];
|
||||||
if ($o >= $length) {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
if (!ctype_digit($str[$o])) {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
if ($str[$o] >= '8') {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
$octal .= $str[$o];
|
|
||||||
$i++;
|
|
||||||
}
|
}
|
||||||
$result .= chr(octdec($octal));
|
$result .= chr(octdec($octal));
|
||||||
$i++;
|
|
||||||
} else {
|
} else {
|
||||||
trigger_deprecation('twig/twig', '3.12', 'Character "%s" at position %d does not need to be escaped anymore.', $nextChar, $i + 2);
|
trigger_deprecation('twig/twig', '3.12', sprintf('Character "%s" at position %d does not need to be escaped anymore.', $nextChar, $i + 1));
|
||||||
$result .= $nextChar;
|
$result .= $nextChar;
|
||||||
$i++;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$i++;
|
||||||
}
|
}
|
||||||
|
|
||||||
return $result;
|
return $result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user