about summary refs log tree commit diff stats
path: root/vendor/phpstan/phpdoc-parser/src/Parser/StringUnescaper.php
diff options
context:
space:
mode:
authorfox <fox@fakecake.org>2026-05-26 06:45:47 +0300
committerfox <fox@fakecake.org>2026-05-26 06:45:47 +0300
commit4ce7212e8c26cd613db0cfc75ef8a5a0ba6eb7be (patch)
tree0ec2e37f6f86d59a904a2f1de2a15294ef1ae349 /vendor/phpstan/phpdoc-parser/src/Parser/StringUnescaper.php
parentcae514dddfc59f533a93a47afc283371d5949843 (diff)
parentdc60880d9cc4074a231fd21c9508d0f78beaf1f1 (diff)
downloadthe-epube-4ce7212e8c26cd613db0cfc75ef8a5a0ba6eb7be.tar.gz
the-epube-4ce7212e8c26cd613db0cfc75ef8a5a0ba6eb7be.tar.xz
Merge branch 'dev' into 'master'
Dev

See merge request git/the-epube!39
Diffstat (limited to 'vendor/phpstan/phpdoc-parser/src/Parser/StringUnescaper.php')
-rw-r--r--vendor/phpstan/phpdoc-parser/src/Parser/StringUnescaper.php100
1 files changed, 100 insertions, 0 deletions
diff --git a/vendor/phpstan/phpdoc-parser/src/Parser/StringUnescaper.php b/vendor/phpstan/phpdoc-parser/src/Parser/StringUnescaper.php
new file mode 100644
index 0000000..e8e0a3d
--- /dev/null
+++ b/vendor/phpstan/phpdoc-parser/src/Parser/StringUnescaper.php
@@ -0,0 +1,100 @@
+<?php declare(strict_types = 1);
+
+namespace PHPStan\PhpDocParser\Parser;
+
+use PHPStan\ShouldNotHappenException;
+use function chr;
+use function hexdec;
+use function octdec;
+use function preg_replace_callback;
+use function str_replace;
+use function substr;
+
+class StringUnescaper
+{
+
+	private const REPLACEMENTS = [
+		'\\' => '\\',
+		'n' => "\n",
+		'r' => "\r",
+		't' => "\t",
+		'f' => "\f",
+		'v' => "\v",
+		'e' => "\x1B",
+	];
+
+	public static function unescapeString(string $string): string
+	{
+		$quote = $string[0];
+
+		if ($quote === '\'') {
+			return str_replace(
+				['\\\\', '\\\''],
+				['\\', '\''],
+				substr($string, 1, -1),
+			);
+		}
+
+		return self::parseEscapeSequences(substr($string, 1, -1), '"');
+	}
+
+	/**
+	 * Implementation based on https://github.com/nikic/PHP-Parser/blob/b0edd4c41111042d43bb45c6c657b2e0db367d9e/lib/PhpParser/Node/Scalar/String_.php#L90-L130
+	 */
+	private static function parseEscapeSequences(string $str, string $quote): string
+	{
+		$str = str_replace('\\' . $quote, $quote, $str);
+
+		return preg_replace_callback(
+			'~\\\\([\\\\nrtfve]|[xX][0-9a-fA-F]{1,2}|[0-7]{1,3}|u\{([0-9a-fA-F]+)\})~',
+			static function ($matches) {
+				$str = $matches[1];
+
+				if (isset(self::REPLACEMENTS[$str])) {
+					return self::REPLACEMENTS[$str];
+				}
+				if ($str[0] === 'x' || $str[0] === 'X') {
+					return chr((int) hexdec(substr($str, 1)));
+				}
+				if ($str[0] === 'u') {
+					if (!isset($matches[2])) {
+						throw new ShouldNotHappenException();
+					}
+					return self::codePointToUtf8((int) hexdec($matches[2]));
+				}
+
+				return chr((int) octdec($str));
+			},
+			$str,
+		);
+	}
+
+	/**
+	 * Implementation based on https://github.com/nikic/PHP-Parser/blob/b0edd4c41111042d43bb45c6c657b2e0db367d9e/lib/PhpParser/Node/Scalar/String_.php#L132-L154
+	 */
+	private static function codePointToUtf8(int $num): string
+	{
+		if ($num <= 0x7F) {
+			return chr($num);
+		}
+		if ($num <= 0x7FF) {
+			return chr(($num >> 6) + 0xC0)
+				. chr(($num & 0x3F) + 0x80);
+		}
+		if ($num <= 0xFFFF) {
+			return chr(($num >> 12) + 0xE0)
+				. chr((($num >> 6) & 0x3F) + 0x80)
+				. chr(($num & 0x3F) + 0x80);
+		}
+		if ($num <= 0x1FFFFF) {
+			return chr(($num >> 18) + 0xF0)
+				. chr((($num >> 12) & 0x3F) + 0x80)
+				. chr((($num >> 6) & 0x3F) + 0x80)
+				. chr(($num & 0x3F) + 0x80);
+		}
+
+		// Invalid UTF-8 codepoint escape sequence: Codepoint too large
+		return "\xef\xbf\xbd";
+	}
+
+}