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>
22
// Parser and Inline assume UTF-8 encoding, so escaped Unicode characters
23
// must be converted to that encoding.
24
// @deprecated since 2.5, to be removed in 3.0
25
const ENCODING = 'UTF-8';
27
// Regex fragment that matches an escaped character in a double quoted
29
const REGEX_ESCAPED_CHARACTER = "\\\\([0abt\tnvfre \\\"\\/\\\\N_LP]|x[0-9a-fA-F]{2}|u[0-9a-fA-F]{4}|U[0-9a-fA-F]{8})";
32
* Unescapes a single quoted string.
34
* @param string $value A single quoted string.
36
* @return string The unescaped string.
38
public function unescapeSingleQuotedString($value)
40
return str_replace('\'\'', '\'', $value);
44
* Unescapes a double quoted string.
46
* @param string $value A double quoted string.
48
* @return string The unescaped string.
50
public function unescapeDoubleQuotedString($value)
53
$callback = function ($match) use ($self) {
54
return $self->unescapeCharacter($match[0]);
57
// evaluate the string
58
return preg_replace_callback('/'.self::REGEX_ESCAPED_CHARACTER.'/u', $callback, $value);
62
* Unescapes a character that was found in a double-quoted string.
64
* @param string $value An escaped character
66
* @return string The unescaped character
68
public function unescapeCharacter($value)
103
// U+00A0 NO-BREAK SPACE
106
// U+2028 LINE SEPARATOR
107
return "\xE2\x80\xA8";
109
// U+2029 PARAGRAPH SEPARATOR
110
return "\xE2\x80\xA9";
112
return self::utf8chr(hexdec(substr($value, 2, 2)));
114
return self::utf8chr(hexdec(substr($value, 2, 4)));
116
return self::utf8chr(hexdec(substr($value, 2, 8)));
121
* Get the UTF-8 character for the given code point.
123
* @param int $c The unicode code point
125
* @return string The corresponding UTF-8 character
127
private static function utf8chr($c)
129
if (0x80 > $c %= 0x200000) {
133
return chr(0xC0 | $c >> 6).chr(0x80 | $c & 0x3F);
136
return chr(0xE0 | $c >> 12).chr(0x80 | $c >> 6 & 0x3F).chr(0x80 | $c & 0x3F);
139
return chr(0xF0 | $c >> 18).chr(0x80 | $c >> 12 & 0x3F).chr(0x80 | $c >> 6 & 0x3F).chr(0x80 | $c & 0x3F);