| 1 |
<?php |
| 2 |
|
| 3 |
/* |
| 4 |
* This file is part of the Symfony package. |
| 5 |
* |
| 6 |
* (c) Fabien Potencier <[email protected]> |
| 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 <[email protected]> |
| 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 = fn ($match) => $this->unescapeCharacter($match[0]); |
| 49 |
|
| 50 |
// evaluate the string |
| 51 |
return preg_replace_callback('/'.self::REGEX_ESCAPED_CHARACTER.'/u', $callback, $value); |
| 52 |
} |
| 53 |
|
| 54 |
/** |
| 55 |
* Unescapes a character that was found in a double-quoted string. |
| 56 |
* |
| 57 |
* @param string $value An escaped character |
| 58 |
*/ |
| 59 |
private function unescapeCharacter(string $value): string |
| 60 |
{ |
| 61 |
return match ($value[1]) { |
| 62 |
'0' => "\x0", |
| 63 |
'a' => "\x7", |
| 64 |
'b' => "\x8", |
| 65 |
't' => "\t", |
| 66 |
"\t" => "\t", |
| 67 |
'n' => "\n", |
| 68 |
'v' => "\xB", |
| 69 |
'f' => "\xC", |
| 70 |
'r' => "\r", |
| 71 |
'e' => "\x1B", |
| 72 |
' ' => ' ', |
| 73 |
'"' => '"', |
| 74 |
'/' => '/', |
| 75 |
'\\' => '\\', |
| 76 |
// U+0085 NEXT LINE |
| 77 |
'N' => "\xC2\x85", |
| 78 |
// U+00A0 NO-BREAK SPACE |
| 79 |
'_' => "\xC2\xA0", |
| 80 |
// U+2028 LINE SEPARATOR |
| 81 |
'L' => "\xE2\x80\xA8", |
| 82 |
// U+2029 PARAGRAPH SEPARATOR |
| 83 |
'P' => "\xE2\x80\xA9", |
| 84 |
'x' => self::utf8chr(hexdec(substr($value, 2, 2))), |
| 85 |
'u' => self::utf8chr(hexdec(substr($value, 2, 4))), |
| 86 |
'U' => self::utf8chr(hexdec(substr($value, 2, 8))), |
| 87 |
default => throw new ParseException(\sprintf('Found unknown escape character "%s".', $value)), |
| 88 |
}; |
| 89 |
} |
| 90 |
|
| 91 |
/** |
| 92 |
* Get the UTF-8 character for the given code point. |
| 93 |
*/ |
| 94 |
private static function utf8chr(int $c): string |
| 95 |
{ |
| 96 |
if (0x80 > $c %= 0x200000) { |
| 97 |
return \chr($c); |
| 98 |
} |
| 99 |
if (0x800 > $c) { |
| 100 |
return \chr(0xC0 | $c >> 6).\chr(0x80 | $c & 0x3F); |
| 101 |
} |
| 102 |
if (0x10000 > $c) { |
| 103 |
return \chr(0xE0 | $c >> 12).\chr(0x80 | $c >> 6 & 0x3F).\chr(0x80 | $c & 0x3F); |
| 104 |
} |
| 105 |
|
| 106 |
return \chr(0xF0 | $c >> 18).\chr(0x80 | $c >> 12 & 0x3F).\chr(0x80 | $c >> 6 & 0x3F).\chr(0x80 | $c & 0x3F); |
| 107 |
} |
| 108 |
} |