4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\Yaml;
15 * Unescaper encapsulates unescaping rules for single and double-quoted
18 * @author Matthew Lewinski <matthew@lewinski.org>
25 * Parser and Inline assume UTF-8 encoding, so escaped Unicode characters
26 * must be converted to that encoding.
28 * @deprecated since version 2.5, to be removed in 3.0
32 const ENCODING = 'UTF-8';
35 * Regex fragment that matches an escaped character in a double quoted string.
37 const REGEX_ESCAPED_CHARACTER = '\\\\(x[0-9a-fA-F]{2}|u[0-9a-fA-F]{4}|U[0-9a-fA-F]{8}|.)';
40 * Unescapes a single quoted string.
42 * @param string $value A single quoted string
44 * @return string The unescaped string
46 public function unescapeSingleQuotedString($value)
48 return str_replace('\'\'', '\'', $value);
52 * Unescapes a double quoted string.
54 * @param string $value A double quoted string
56 * @return string The unescaped string
58 public function unescapeDoubleQuotedString($value)
61 $callback = function ($match) use ($self) {
62 return $self->unescapeCharacter($match[0]);
65 // evaluate the string
66 return preg_replace_callback('/'.self::REGEX_ESCAPED_CHARACTER.'/u', $callback, $value);
70 * Unescapes a character that was found in a double-quoted string.
72 * @param string $value An escaped character
74 * @return string The unescaped character
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.
79 public function unescapeCharacter($value)
114 // U+00A0 NO-BREAK SPACE
117 // U+2028 LINE SEPARATOR
118 return "\xE2\x80\xA8";
120 // U+2029 PARAGRAPH SEPARATOR
121 return "\xE2\x80\xA9";
123 return self::utf8chr(hexdec(substr($value, 2, 2)));
125 return self::utf8chr(hexdec(substr($value, 2, 4)));
127 return self::utf8chr(hexdec(substr($value, 2, 8)));
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);
136 * Get the UTF-8 character for the given code point.
138 * @param int $c The unicode code point
140 * @return string The corresponding UTF-8 character
142 private static function utf8chr($c)
144 if (0x80 > $c %= 0x200000) {
148 return chr(0xC0 | $c >> 6).chr(0x80 | $c & 0x3F);
151 return chr(0xE0 | $c >> 12).chr(0x80 | $c >> 6 & 0x3F).chr(0x80 | $c & 0x3F);
154 return chr(0xF0 | $c >> 18).chr(0x80 | $c >> 12 & 0x3F).chr(0x80 | $c >> 6 & 0x3F).chr(0x80 | $c & 0x3F);