MCPcopy Create free account
hub / github.com/simplejson/simplejson / py_scanstring

Function py_scanstring

simplejson/decoder.py:68–139  ·  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, encoding=None, strict=True,
        _b=BACKSLASH, _m=STRINGCHUNK.match, _join=u''.join,
        _PY3=PY3, _maxunicode=sys.maxunicode,
        _scan_four_digit_hex=scan_four_digit_hex)

Source from the content-addressed store, hash-verified

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

Callers

nothing calls this directly

Calls 1

JSONDecodeErrorClass · 0.85

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…