equal
deleted
inserted
replaced
62 try: |
62 try: |
63 return ''.join(jm[x] for x in bytearray(u8chars)) |
63 return ''.join(jm[x] for x in bytearray(u8chars)) |
64 except IndexError: |
64 except IndexError: |
65 raise ValueError |
65 raise ValueError |
66 |
66 |
|
67 if pycompat.ispy3: |
|
68 _utf8strict = r'surrogatepass' |
|
69 else: |
|
70 _utf8strict = r'strict' |
|
71 |
67 def jsonescapeu8fallback(u8chars, paranoid): |
72 def jsonescapeu8fallback(u8chars, paranoid): |
68 """Convert a UTF-8 byte string to JSON-escaped form (slow path) |
73 """Convert a UTF-8 byte string to JSON-escaped form (slow path) |
69 |
74 |
70 Escapes all non-ASCII characters no matter if paranoid is False. |
75 Escapes all non-ASCII characters no matter if paranoid is False. |
71 """ |
76 """ |
72 if paranoid: |
77 if paranoid: |
73 jm = _paranoidjsonmap |
78 jm = _paranoidjsonmap |
74 else: |
79 else: |
75 jm = _jsonmap |
80 jm = _jsonmap |
76 # non-BMP char is represented as UTF-16 surrogate pair |
81 # non-BMP char is represented as UTF-16 surrogate pair |
77 u16codes = array.array(r'H', u8chars.decode('utf-8').encode('utf-16')) |
82 u16b = u8chars.decode('utf-8', _utf8strict).encode('utf-16', _utf8strict) |
|
83 u16codes = array.array(r'H', u16b) |
78 u16codes.pop(0) # drop BOM |
84 u16codes.pop(0) # drop BOM |
79 return ''.join(jm[x] if x < 128 else '\\u%04x' % x for x in u16codes) |
85 return ''.join(jm[x] if x < 128 else '\\u%04x' % x for x in u16codes) |