MCPcopy Create free account
hub / github.com/RT-Thread/env-windows / py_scanstring

Function py_scanstring

tools/python-3.11.9-amd64/Lib/json/decoder.py:69–126  ·  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 charact

(s, end, strict=True,
        _b=BACKSLASH, _m=STRINGCHUNK.match)

Source from the content-addressed store, hash-verified

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

Callers

nothing calls this directly

Calls 6

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

Tested by

no test coverage detected