|
| 1 | +r"""Replace `\uXXXX` escape sequences with Unicode code points.""" |
| 2 | + |
| 3 | +from typing import List |
| 4 | +from typing import Tuple |
| 5 | + |
| 6 | +from .exceptions import JSONPathSyntaxError |
| 7 | +from .token import Token |
| 8 | + |
| 9 | + |
| 10 | +def unescape_string(value: str, token: Token, quote: str) -> str: |
| 11 | + """Return `value` with escape sequences replaced with Unicode code points.""" |
| 12 | + unescaped: List[str] = [] |
| 13 | + index = 0 |
| 14 | + |
| 15 | + while index < len(value): |
| 16 | + ch = value[index] |
| 17 | + if ch == "\\": |
| 18 | + index += 1 |
| 19 | + _ch, index = _decode_escape_sequence(value, index, token, quote) |
| 20 | + unescaped.append(_ch) |
| 21 | + else: |
| 22 | + _string_from_codepoint(ord(ch), token) |
| 23 | + unescaped.append(ch) |
| 24 | + index += 1 |
| 25 | + return "".join(unescaped) |
| 26 | + |
| 27 | + |
| 28 | +def _decode_escape_sequence( # noqa: PLR0911 |
| 29 | + value: str, index: int, token: Token, quote: str |
| 30 | +) -> Tuple[str, int]: |
| 31 | + try: |
| 32 | + ch = value[index] |
| 33 | + except IndexError as err: |
| 34 | + raise JSONPathSyntaxError("incomplete escape sequence", token=token) from err |
| 35 | + |
| 36 | + if ch == quote: |
| 37 | + return quote, index |
| 38 | + if ch == "\\": |
| 39 | + return "\\", index |
| 40 | + if ch == "/": |
| 41 | + return "/", index |
| 42 | + if ch == "b": |
| 43 | + return "\x08", index |
| 44 | + if ch == "f": |
| 45 | + return "\x0c", index |
| 46 | + if ch == "n": |
| 47 | + return "\n", index |
| 48 | + if ch == "r": |
| 49 | + return "\r", index |
| 50 | + if ch == "t": |
| 51 | + return "\t", index |
| 52 | + if ch == "u": |
| 53 | + codepoint, index = _decode_hex_char(value, index, token) |
| 54 | + return _string_from_codepoint(codepoint, token), index |
| 55 | + |
| 56 | + raise JSONPathSyntaxError( |
| 57 | + f"unknown escape sequence at index {token.index + index - 1}", |
| 58 | + token=token, |
| 59 | + ) |
| 60 | + |
| 61 | + |
| 62 | +def _decode_hex_char(value: str, index: int, token: Token) -> Tuple[int, int]: |
| 63 | + length = len(value) |
| 64 | + |
| 65 | + if index + 4 >= length: |
| 66 | + raise JSONPathSyntaxError( |
| 67 | + f"incomplete escape sequence at index {token.index + index - 1}", |
| 68 | + token=token, |
| 69 | + ) |
| 70 | + |
| 71 | + index += 1 # move past 'u' |
| 72 | + codepoint = _parse_hex_digits(value[index : index + 4], token) |
| 73 | + |
| 74 | + if _is_low_surrogate(codepoint): |
| 75 | + raise JSONPathSyntaxError( |
| 76 | + f"unexpected low surrogate at index {token.index + index - 1}", |
| 77 | + token=token, |
| 78 | + ) |
| 79 | + |
| 80 | + if _is_high_surrogate(codepoint): |
| 81 | + # expect a surrogate pair |
| 82 | + if not ( |
| 83 | + index + 9 < length and value[index + 4] == "\\" and value[index + 5] == "u" |
| 84 | + ): |
| 85 | + raise JSONPathSyntaxError( |
| 86 | + f"incomplete escape sequence at index {token.index + index - 2}", |
| 87 | + token=token, |
| 88 | + ) |
| 89 | + |
| 90 | + low_surrogate = _parse_hex_digits(value[index + 6 : index + 10], token) |
| 91 | + |
| 92 | + if not _is_low_surrogate(low_surrogate): |
| 93 | + raise JSONPathSyntaxError( |
| 94 | + f"unexpected codepoint at index {token.index + index + 4}", |
| 95 | + token=token, |
| 96 | + ) |
| 97 | + |
| 98 | + codepoint = 0x10000 + (((codepoint & 0x03FF) << 10) | (low_surrogate & 0x03FF)) |
| 99 | + |
| 100 | + return (codepoint, index + 9) |
| 101 | + |
| 102 | + return (codepoint, index + 3) |
| 103 | + |
| 104 | + |
| 105 | +def _parse_hex_digits(digits: str, token: Token) -> int: |
| 106 | + codepoint = 0 |
| 107 | + for digit in digits.encode(): |
| 108 | + codepoint <<= 4 |
| 109 | + if digit >= 48 and digit <= 57: |
| 110 | + codepoint |= digit - 48 |
| 111 | + elif digit >= 65 and digit <= 70: |
| 112 | + codepoint |= digit - 65 + 10 |
| 113 | + elif digit >= 97 and digit <= 102: |
| 114 | + codepoint |= digit - 97 + 10 |
| 115 | + else: |
| 116 | + raise JSONPathSyntaxError( |
| 117 | + "invalid \\uXXXX escape sequence", |
| 118 | + token=token, |
| 119 | + ) |
| 120 | + return codepoint |
| 121 | + |
| 122 | + |
| 123 | +def _string_from_codepoint(codepoint: int, token: Token) -> str: |
| 124 | + if codepoint <= 0x1F: |
| 125 | + raise JSONPathSyntaxError("invalid character", token=token) |
| 126 | + return chr(codepoint) |
| 127 | + |
| 128 | + |
| 129 | +def _is_high_surrogate(codepoint: int) -> bool: |
| 130 | + return codepoint >= 0xD800 and codepoint <= 0xDBFF |
| 131 | + |
| 132 | + |
| 133 | +def _is_low_surrogate(codepoint: int) -> bool: |
| 134 | + return codepoint >= 0xDC00 and codepoint <= 0xDFFF |
0 commit comments