123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156 |
- <?php
- namespace Symfony\Component\Yaml;
- class Unescaper
- {
-
- const ENCODING = 'UTF-8';
-
- const REGEX_ESCAPED_CHARACTER = '\\\\(x[0-9a-fA-F]{2}|u[0-9a-fA-F]{4}|U[0-9a-fA-F]{8}|.)';
-
- public function unescapeSingleQuotedString($value)
- {
- return str_replace('\'\'', '\'', $value);
- }
-
- public function unescapeDoubleQuotedString($value)
- {
- $self = $this;
- $callback = function ($match) use ($self) {
- return $self->unescapeCharacter($match[0]);
- };
-
- return preg_replace_callback('/'.self::REGEX_ESCAPED_CHARACTER.'/u', $callback, $value);
- }
-
- public function unescapeCharacter($value)
- {
- switch ($value[1]) {
- case '0':
- return "\x0";
- case 'a':
- return "\x7";
- case 'b':
- return "\x8";
- case 't':
- return "\t";
- case "\t":
- return "\t";
- case 'n':
- return "\n";
- case 'v':
- return "\xB";
- case 'f':
- return "\xC";
- case 'r':
- return "\r";
- case 'e':
- return "\x1B";
- case ' ':
- return ' ';
- case '"':
- return '"';
- case '/':
- return '/';
- case '\\':
- return '\\';
- case 'N':
-
- return "\xC2\x85";
- case '_':
-
- return "\xC2\xA0";
- case 'L':
-
- return "\xE2\x80\xA8";
- case 'P':
-
- return "\xE2\x80\xA9";
- case 'x':
- return self::utf8chr(hexdec(substr($value, 2, 2)));
- case 'u':
- return self::utf8chr(hexdec(substr($value, 2, 4)));
- case 'U':
- return self::utf8chr(hexdec(substr($value, 2, 8)));
- default:
- @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);
- return $value;
- }
- }
-
- private static function utf8chr($c)
- {
- if (0x80 > $c %= 0x200000) {
- return \chr($c);
- }
- if (0x800 > $c) {
- return \chr(0xC0 | $c >> 6).\chr(0x80 | $c & 0x3F);
- }
- if (0x10000 > $c) {
- return \chr(0xE0 | $c >> 12).\chr(0x80 | $c >> 6 & 0x3F).\chr(0x80 | $c & 0x3F);
- }
- return \chr(0xF0 | $c >> 18).\chr(0x80 | $c >> 12 & 0x3F).\chr(0x80 | $c >> 6 & 0x3F).\chr(0x80 | $c & 0x3F);
- }
- }
|