MCPcopy Index your code
hub / github.com/RustPython/RustPython / py_scanstring

Function py_scanstring

Lib/json/decoder.py:84–141  ·  view source on GitHub ↗

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, strict=True,
        _b=BACKSLASH, _m=STRINGCHUNK.match)

Source from the content-addressed store, hash-verified

82 raise JSONDecodeError(msg, s, pos)
83
84def py_scanstring(s, end, strict=True,
85 _b=BACKSLASH, _m=STRINGCHUNK.match):
86 """Scan the string s for a JSON string. End is the index of the
87 character in s after the quote that started the JSON string.
88 Unescapes all valid JSON string escape sequences and raises ValueError
89 on attempt to decode an invalid string. If strict is False then literal
90 control characters are allowed in the string.
91
92 Returns a tuple of the decoded string and the index of the character in s
93 after the end quote."""
94 chunks = []
95 _append = chunks.append
96 begin = end - 1
97 while 1:
98 chunk = _m(s, end)
99 if chunk is None:
100 raise JSONDecodeError("Unterminated string starting at", s, begin)
101 end = chunk.end()
102 content, terminator = chunk.groups()
103 # Content is contains zero or more unescaped string characters
104 if content:
105 _append(content)
106 # Terminator is the end of string, a literal control character,
107 # or a backslash denoting that an escape sequence follows
108 if terminator == '"':
109 break
110 elif terminator != '\\':
111 if strict:
112 #msg = "Invalid control character %r at" % (terminator,)
113 msg = "Invalid control character {0!r} at".format(terminator)
114 raise JSONDecodeError(msg, s, end)
115 else:
116 _append(terminator)
117 continue
118 try:
119 esc = s[end]
120 except IndexError:
121 raise JSONDecodeError("Unterminated string starting at",
122 s, begin) from None
123 # If not a unicode escape sequence, must be in the lookup table
124 if esc != 'u':
125 try:
126 char = _b[esc]
127 except KeyError:
128 msg = "Invalid \\escape: {0!r}".format(esc)
129 raise JSONDecodeError(msg, s, end)
130 end += 1
131 else:
132 uni = _decode_uXXXX(s, end)
133 end += 5
134 if 0xd800 <= uni <= 0xdbff and s[end:end + 2] == '\\u':
135 uni2 = _decode_uXXXX(s, end + 1)
136 if 0xdc00 <= uni2 <= 0xdfff:
137 uni = 0x10000 + (((uni - 0xd800) << 10) | (uni2 - 0xdc00))
138 end += 6
139 char = chr(uni)
140 _append(char)
141 return ''.join(chunks), end

Callers

nothing calls this directly

Calls 7

JSONDecodeErrorClass · 0.85
_decode_uXXXXFunction · 0.85
chrFunction · 0.85
endMethod · 0.45
groupsMethod · 0.45
formatMethod · 0.45
joinMethod · 0.45

Tested by

no test coverage detected