mirror of
https://github.com/danog/PHP-Parser.git
synced 2024-11-26 20:04:48 +01:00
Escape all low control characters in strings
This commit is contained in:
parent
47b254ea51
commit
371c783344
@ -849,9 +849,20 @@ class Standard extends PrettyPrinterAbstract
|
||||
protected function escapeString($string, $quote) {
|
||||
if (null === $quote) {
|
||||
// For doc strings, don't escape newlines
|
||||
return addcslashes($string, "\t\f\v$\\");
|
||||
$escaped = addcslashes($string, "\t\f\v$\\");
|
||||
} else {
|
||||
$escaped = addcslashes($string, "\n\r\t\f\v$" . $quote . "\\");
|
||||
}
|
||||
return addcslashes($string, "\n\r\t\f\v$" . $quote . "\\");
|
||||
|
||||
// Escape other control characters
|
||||
return preg_replace_callback('/([\0-\10\16-\37])(?=([0-7]?))/', function ($matches) {
|
||||
$oct = decoct(ord($matches[1]));
|
||||
if ($matches[2] !== '') {
|
||||
// If there is a trailing digit, use the full three character form
|
||||
return '\\' . str_pad($oct, 3, '0', STR_PAD_LEFT);
|
||||
}
|
||||
return '\\' . $oct;
|
||||
}, $escaped);
|
||||
}
|
||||
|
||||
protected function containsEndLabel($string, $label, $atStart = true, $atEnd = true) {
|
||||
|
23
test/code/prettyPrinter/expr/stringEscaping.test
Normal file
23
test/code/prettyPrinter/expr/stringEscaping.test
Normal file
@ -0,0 +1,23 @@
|
||||
Escape sequences in double-quoted strings
|
||||
-----
|
||||
<?php
|
||||
"\n\r\t\f\v\$\"\\";
|
||||
"@@{ implode(range("\0", "\37")) }@@";
|
||||
"\0000\0001";
|
||||
|
||||
<<<DOC
|
||||
\n\r\t\f\v\$\"\\
|
||||
@@{ implode(range("\0", "\37")) }@@
|
||||
\0000\0001
|
||||
DOC;
|
||||
|
||||
-----
|
||||
"\n\r\t\f\v\$\"\\";
|
||||
"\0\1\2\3\4\5\6\7\10\t\n\v\f\r\16\17\20\21\22\23\24\25\26\27\30\31\32\33\34\35\36\37";
|
||||
"\0000\0001";
|
||||
<<<DOC
|
||||
@@{ "\n\r" }@@\t\f\v\$\\"\\
|
||||
\0\1\2\3\4\5\6\7\10\t@@{ "\n" }@@\v\f@@{ "\r" }@@\16\17\20\21\22\23\24\25\26\27\30\31\32\33\34\35\36\37
|
||||
\0000\0001
|
||||
DOC
|
||||
;
|
Loading…
Reference in New Issue
Block a user