Scan the string s for a JSON string. End is the index of the character in s after the quote that started the JSON string. Unescapes all valid JSON string escape sequences and raises ValueError on attempt to decode an invalid string. If strict is False then literal control characters
(s, end, encoding=None, strict=True,
_b=BACKSLASH, _m=STRINGCHUNK.match)
| 94 | DEFAULT_ENCODING = "utf-8" |
| 95 | |
| 96 | def py_scanstring(s, end, encoding=None, strict=True, |
| 97 | _b=BACKSLASH, _m=STRINGCHUNK.match): |
| 98 | """Scan the string s for a JSON string. End is the index of the |
| 99 | character in s after the quote that started the JSON string. |
| 100 | Unescapes all valid JSON string escape sequences and raises ValueError |
| 101 | on attempt to decode an invalid string. If strict is False then literal |
| 102 | control characters are allowed in the string. |
| 103 | |
| 104 | Returns a tuple of the decoded string and the index of the character in s |
| 105 | after the end quote.""" |
| 106 | if encoding is None: |
| 107 | encoding = DEFAULT_ENCODING |
| 108 | chunks = [] |
| 109 | _append = chunks.append |
| 110 | begin = end - 1 |
| 111 | while 1: |
| 112 | chunk = _m(s, end) |
| 113 | if chunk is None: |
| 114 | raise JSONDecodeError( |
| 115 | "Unterminated string starting at", s, begin) |
| 116 | end = chunk.end() |
| 117 | content, terminator = chunk.groups() |
| 118 | # Content is contains zero or more unescaped string characters |
| 119 | if content: |
| 120 | if not isinstance(content, unicode): |
| 121 | content = unicode(content, encoding) |
| 122 | _append(content) |
| 123 | # Terminator is the end of string, a literal control character, |
| 124 | # or a backslash denoting that an escape sequence follows |
| 125 | if terminator == '"': |
| 126 | break |
| 127 | elif terminator != '\\': |
| 128 | if strict: |
| 129 | msg = "Invalid control character %r at" % (terminator,) |
| 130 | #msg = "Invalid control character {0!r} at".format(terminator) |
| 131 | raise JSONDecodeError(msg, s, end) |
| 132 | else: |
| 133 | _append(terminator) |
| 134 | continue |
| 135 | try: |
| 136 | esc = s[end] |
| 137 | except IndexError: |
| 138 | raise JSONDecodeError( |
| 139 | "Unterminated string starting at", s, begin) |
| 140 | # If not a unicode escape sequence, must be in the lookup table |
| 141 | if esc != 'u': |
| 142 | try: |
| 143 | char = _b[esc] |
| 144 | except KeyError: |
| 145 | msg = "Invalid \\escape: " + repr(esc) |
| 146 | raise JSONDecodeError(msg, s, end) |
| 147 | end += 1 |
| 148 | else: |
| 149 | # Unicode escape sequence |
| 150 | esc = s[end + 1:end + 5] |
| 151 | next_end = end + 5 |
| 152 | if len(esc) != 4: |
| 153 | msg = "Invalid \\uXXXX escape" |
nothing calls this directly
no test coverage detected