@@ -64,25 +64,44 @@ private function dumpVar(&$var, array $parents = [], int $level = 0, int $column
6464 }
6565
6666
67- private function dumpString (string $ var ): string
67+ private function dumpString (string $ s ): string
6868 {
69- if (preg_match ('#[^\x09\x20-\x7E\xA0-\x{10FFFF}]#u ' , $ var ) || preg_last_error ()) {
70- static $ table ;
71- if ($ table === null ) {
72- foreach (array_merge (range ("\x00" , "\x1F" ), range ("\x7F" , "\xFF" )) as $ ch ) {
73- $ table [$ ch ] = '\x ' . str_pad (dechex (ord ($ ch )), 2 , '0 ' , STR_PAD_LEFT );
74- }
75- $ table ['\\' ] = '\\\\' ;
76- $ table ["\r" ] = '\r ' ;
77- $ table ["\n" ] = '\n ' ;
78- $ table ["\t" ] = '\t ' ;
79- $ table ['$ ' ] = '\$ ' ;
80- $ table ['" ' ] = '\" ' ;
81- }
82- return '" ' . strtr ($ var , $ table ) . '" ' ;
83- }
69+ static $ special = [
70+ "\r" => '\r ' ,
71+ "\n" => '\n ' ,
72+ "\t" => '\t ' ,
73+ "\e" => '\e ' ,
74+ '\\' => '\\\\' ,
75+ ];
76+
77+ $ utf8 = preg_match ('##u ' , $ s );
78+ $ escaped = preg_replace_callback (
79+ $ utf8 ? '#[\p{C} \\\\]#u ' : '#[\x00-\x1F\x7F-\xFF \\\\]# ' ,
80+ function ($ m ) use ($ special ) {
81+ return $ special [$ m [0 ]] ?? (strlen ($ m [0 ]) === 1
82+ ? '\x ' . str_pad (strtoupper (dechex (ord ($ m [0 ]))), 2 , '0 ' , STR_PAD_LEFT ) . ''
83+ : '\u{ ' . strtoupper (ltrim (dechex (self ::utf8Ord ($ m [0 ])), '0 ' )) . '} ' );
84+ },
85+ $ s
86+ );
87+ return $ s === str_replace ('\\\\' , '\\' , $ escaped )
88+ ? "' " . preg_replace ('# \'| \\\\(?=[ \'\\\\]|$)#D ' , '\\\\$0 ' , $ s ) . "' "
89+ : '" ' . addcslashes ($ escaped , '"$ ' ) . '" ' ;
90+ }
8491
85- return "' " . preg_replace ('# \'| \\\\(?=[ \'\\\\]|$)#D ' , '\\\\$0 ' , $ var ) . "' " ;
92+
93+ private static function utf8Ord (string $ c ): int
94+ {
95+ $ ord0 = ord ($ c [0 ]);
96+ if ($ ord0 < 0x80 ) {
97+ return $ ord0 ;
98+ } elseif ($ ord0 < 0xE0 ) {
99+ return ($ ord0 << 6 ) + ord ($ c [1 ]) - 0x3080 ;
100+ } elseif ($ ord0 < 0xF0 ) {
101+ return ($ ord0 << 12 ) + (ord ($ c [1 ]) << 6 ) + ord ($ c [2 ]) - 0xE2080 ;
102+ } else {
103+ return ($ ord0 << 18 ) + (ord ($ c [1 ]) << 12 ) + (ord ($ c [2 ]) << 6 ) + ord ($ c [3 ]) - 0x3C82080 ;
104+ }
86105 }
87106
88107
0 commit comments