1e02cc9fd808608015bab6843792ff9c53228afb
[yaffs-website] / vendor / symfony / yaml / Unescaper.php
1 <?php
2
3 /*
4  * This file is part of the Symfony package.
5  *
6  * (c) Fabien Potencier <fabien@symfony.com>
7  *
8  * For the full copyright and license information, please view the LICENSE
9  * file that was distributed with this source code.
10  */
11
12 namespace Symfony\Component\Yaml;
13
14 /**
15  * Unescaper encapsulates unescaping rules for single and double-quoted
16  * YAML strings.
17  *
18  * @author Matthew Lewinski <matthew@lewinski.org>
19  *
20  * @internal
21  */
22 class Unescaper
23 {
24     /**
25      * Parser and Inline assume UTF-8 encoding, so escaped Unicode characters
26      * must be converted to that encoding.
27      *
28      * @deprecated since version 2.5, to be removed in 3.0
29      *
30      * @internal
31      */
32     const ENCODING = 'UTF-8';
33
34     /**
35      * Regex fragment that matches an escaped character in a double quoted string.
36      */
37     const REGEX_ESCAPED_CHARACTER = '\\\\(x[0-9a-fA-F]{2}|u[0-9a-fA-F]{4}|U[0-9a-fA-F]{8}|.)';
38
39     /**
40      * Unescapes a single quoted string.
41      *
42      * @param string $value A single quoted string
43      *
44      * @return string The unescaped string
45      */
46     public function unescapeSingleQuotedString($value)
47     {
48         return str_replace('\'\'', '\'', $value);
49     }
50
51     /**
52      * Unescapes a double quoted string.
53      *
54      * @param string $value A double quoted string
55      *
56      * @return string The unescaped string
57      */
58     public function unescapeDoubleQuotedString($value)
59     {
60         $self = $this;
61         $callback = function ($match) use ($self) {
62             return $self->unescapeCharacter($match[0]);
63         };
64
65         // evaluate the string
66         return preg_replace_callback('/'.self::REGEX_ESCAPED_CHARACTER.'/u', $callback, $value);
67     }
68
69     /**
70      * Unescapes a character that was found in a double-quoted string.
71      *
72      * @param string $value An escaped character
73      *
74      * @return string The unescaped character
75      *
76      * @internal This method is public to be usable as callback. It should not
77      *           be used in user code. Should be changed in 3.0.
78      */
79     public function unescapeCharacter($value)
80     {
81         switch ($value[1]) {
82             case '0':
83                 return "\x0";
84             case 'a':
85                 return "\x7";
86             case 'b':
87                 return "\x8";
88             case 't':
89                 return "\t";
90             case "\t":
91                 return "\t";
92             case 'n':
93                 return "\n";
94             case 'v':
95                 return "\xB";
96             case 'f':
97                 return "\xC";
98             case 'r':
99                 return "\r";
100             case 'e':
101                 return "\x1B";
102             case ' ':
103                 return ' ';
104             case '"':
105                 return '"';
106             case '/':
107                 return '/';
108             case '\\':
109                 return '\\';
110             case 'N':
111                 // U+0085 NEXT LINE
112                 return "\xC2\x85";
113             case '_':
114                 // U+00A0 NO-BREAK SPACE
115                 return "\xC2\xA0";
116             case 'L':
117                 // U+2028 LINE SEPARATOR
118                 return "\xE2\x80\xA8";
119             case 'P':
120                 // U+2029 PARAGRAPH SEPARATOR
121                 return "\xE2\x80\xA9";
122             case 'x':
123                 return self::utf8chr(hexdec(substr($value, 2, 2)));
124             case 'u':
125                 return self::utf8chr(hexdec(substr($value, 2, 4)));
126             case 'U':
127                 return self::utf8chr(hexdec(substr($value, 2, 8)));
128             default:
129                 @trigger_error('Not escaping a backslash in a double-quoted string is deprecated since Symfony 2.8 and will throw a ParseException in 3.0.', E_USER_DEPRECATED);
130
131                 return $value;
132         }
133     }
134
135     /**
136      * Get the UTF-8 character for the given code point.
137      *
138      * @param int $c The unicode code point
139      *
140      * @return string The corresponding UTF-8 character
141      */
142     private static function utf8chr($c)
143     {
144         if (0x80 > $c %= 0x200000) {
145             return chr($c);
146         }
147         if (0x800 > $c) {
148             return chr(0xC0 | $c >> 6).chr(0x80 | $c & 0x3F);
149         }
150         if (0x10000 > $c) {
151             return chr(0xE0 | $c >> 12).chr(0x80 | $c >> 6 & 0x3F).chr(0x80 | $c & 0x3F);
152         }
153
154         return chr(0xF0 | $c >> 18).chr(0x80 | $c >> 12 & 0x3F).chr(0x80 | $c >> 6 & 0x3F).chr(0x80 | $c & 0x3F);
155     }
156 }