@@ -58,6 +58,16 @@ def errmsg(msg, doc, pos, end=None):
5858 'b' : '\b ' , 'f' : '\f ' , 'n' : '\n ' , 'r' : '\r ' , 't' : '\t ' ,
5959}
6060
61+ def _decode_uXXXX (s , pos ):
62+ esc = s [pos + 1 :pos + 5 ]
63+ if len (esc ) == 4 and esc [1 ] not in 'xX' :
64+ try :
65+ return int (esc , 16 )
66+ except ValueError :
67+ pass
68+ msg = "Invalid \\ uXXXX escape"
69+ raise ValueError (errmsg (msg , s , pos ))
70+
6171def py_scanstring (s , end , strict = True ,
6272 _b = BACKSLASH , _m = STRINGCHUNK .match ):
6373 """Scan the string s for a JSON string. End is the index of the
@@ -107,25 +117,14 @@ def py_scanstring(s, end, strict=True,
107117 raise ValueError (errmsg (msg , s , end ))
108118 end += 1
109119 else :
110- esc = s [end + 1 :end + 5 ]
111- next_end = end + 5
112- if len (esc ) != 4 :
113- msg = "Invalid \\ uXXXX escape"
114- raise ValueError (errmsg (msg , s , end ))
115- uni = int (esc , 16 )
116- if 0xd800 <= uni <= 0xdbff :
117- msg = "Invalid \\ uXXXX\\ uXXXX surrogate pair"
118- if not s [end + 5 :end + 7 ] == '\\ u' :
119- raise ValueError (errmsg (msg , s , end ))
120- esc2 = s [end + 7 :end + 11 ]
121- if len (esc2 ) != 4 :
122- raise ValueError (errmsg (msg , s , end ))
123- uni2 = int (esc2 , 16 )
124- uni = 0x10000 + (((uni - 0xd800 ) << 10 ) | (uni2 - 0xdc00 ))
125- next_end += 6
120+ uni = _decode_uXXXX (s , end )
121+ end += 5
122+ if 0xd800 <= uni <= 0xdbff and s [end :end + 2 ] == '\\ u' :
123+ uni2 = _decode_uXXXX (s , end + 1 )
124+ if 0xdc00 <= uni2 <= 0xdfff :
125+ uni = 0x10000 + (((uni - 0xd800 ) << 10 ) | (uni2 - 0xdc00 ))
126+ end += 6
126127 char = chr (uni )
127-
128- end = next_end
129128 _append (char )
130129 return '' .join (chunks ), end
131130
0 commit comments