Subversion-Projekte lars-tiefland.laravel_shop

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
621 lars 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
use Symfony\Component\Yaml\Exception\ParseException;
15
 
16
/**
17
 * Unescaper encapsulates unescaping rules for single and double-quoted
18
 * YAML strings.
19
 *
20
 * @author Matthew Lewinski <matthew@lewinski.org>
21
 *
22
 * @internal
23
 */
24
class Unescaper
25
{
26
    /**
27
     * Regex fragment that matches an escaped character in a double quoted string.
28
     */
29
    public const REGEX_ESCAPED_CHARACTER = '\\\\(x[0-9a-fA-F]{2}|u[0-9a-fA-F]{4}|U[0-9a-fA-F]{8}|.)';
30
 
31
    /**
32
     * Unescapes a single quoted string.
33
     *
34
     * @param string $value A single quoted string
35
     */
36
    public function unescapeSingleQuotedString(string $value): string
37
    {
38
        return str_replace('\'\'', '\'', $value);
39
    }
40
 
41
    /**
42
     * Unescapes a double quoted string.
43
     *
44
     * @param string $value A double quoted string
45
     */
46
    public function unescapeDoubleQuotedString(string $value): string
47
    {
48
        $callback = function ($match) {
49
            return $this->unescapeCharacter($match[0]);
50
        };
51
 
52
        // evaluate the string
53
        return preg_replace_callback('/'.self::REGEX_ESCAPED_CHARACTER.'/u', $callback, $value);
54
    }
55
 
56
    /**
57
     * Unescapes a character that was found in a double-quoted string.
58
     *
59
     * @param string $value An escaped character
60
     */
61
    private function unescapeCharacter(string $value): string
62
    {
63
        return match ($value[1]) {
64
            '0' => "\x0",
65
            'a' => "\x7",
66
            'b' => "\x8",
67
            't' => "\t",
68
            "\t" => "\t",
69
            'n' => "\n",
70
            'v' => "\xB",
71
            'f' => "\xC",
72
            'r' => "\r",
73
            'e' => "\x1B",
74
            ' ' => ' ',
75
            '"' => '"',
76
            '/' => '/',
77
            '\\' => '\\',
78
            // U+0085 NEXT LINE
79
            'N' => "\xC2\x85",
80
            // U+00A0 NO-BREAK SPACE
81
            '_' => "\xC2\xA0",
82
            // U+2028 LINE SEPARATOR
83
            'L' => "\xE2\x80\xA8",
84
            // U+2029 PARAGRAPH SEPARATOR
85
            'P' => "\xE2\x80\xA9",
86
            'x' => self::utf8chr(hexdec(substr($value, 2, 2))),
87
            'u' => self::utf8chr(hexdec(substr($value, 2, 4))),
88
            'U' => self::utf8chr(hexdec(substr($value, 2, 8))),
89
            default => throw new ParseException(sprintf('Found unknown escape character "%s".', $value)),
90
        };
91
    }
92
 
93
    /**
94
     * Get the UTF-8 character for the given code point.
95
     */
96
    private static function utf8chr(int $c): string
97
    {
98
        if (0x80 > $c %= 0x200000) {
99
            return \chr($c);
100
        }
101
        if (0x800 > $c) {
102
            return \chr(0xC0 | $c >> 6).\chr(0x80 | $c & 0x3F);
103
        }
104
        if (0x10000 > $c) {
105
            return \chr(0xE0 | $c >> 12).\chr(0x80 | $c >> 6 & 0x3F).\chr(0x80 | $c & 0x3F);
106
        }
107
 
108
        return \chr(0xF0 | $c >> 18).\chr(0x80 | $c >> 12 & 0x3F).\chr(0x80 | $c >> 6 & 0x3F).\chr(0x80 | $c & 0x3F);
109
    }
110
}