##// END OF EJS Templates
py3: don't pass bytes to array.array()
Yuya Nishihara -
r34214:5307cc57 default
parent child Browse files
Show More
@@ -1,79 +1,79 b''
1 1 # charencode.py - miscellaneous character encoding
2 2 #
3 3 # Copyright 2005-2009 Matt Mackall <mpm@selenic.com> and others
4 4 #
5 5 # This software may be used and distributed according to the terms of the
6 6 # GNU General Public License version 2 or any later version.
7 7
8 8 from __future__ import absolute_import
9 9
10 10 import array
11 11
12 12 from .. import (
13 13 pycompat,
14 14 )
15 15
16 16 def isasciistr(s):
17 17 try:
18 18 s.decode('ascii')
19 19 return True
20 20 except UnicodeDecodeError:
21 21 return False
22 22
23 23 def asciilower(s):
24 24 '''convert a string to lowercase if ASCII
25 25
26 26 Raises UnicodeDecodeError if non-ASCII characters are found.'''
27 27 s.decode('ascii')
28 28 return s.lower()
29 29
30 30 def asciiupper(s):
31 31 '''convert a string to uppercase if ASCII
32 32
33 33 Raises UnicodeDecodeError if non-ASCII characters are found.'''
34 34 s.decode('ascii')
35 35 return s.upper()
36 36
37 37 _jsonmap = []
38 38 _jsonmap.extend("\\u%04x" % x for x in range(32))
39 39 _jsonmap.extend(pycompat.bytechr(x) for x in range(32, 127))
40 40 _jsonmap.append('\\u007f')
41 41 _jsonmap[0x09] = '\\t'
42 42 _jsonmap[0x0a] = '\\n'
43 43 _jsonmap[0x22] = '\\"'
44 44 _jsonmap[0x5c] = '\\\\'
45 45 _jsonmap[0x08] = '\\b'
46 46 _jsonmap[0x0c] = '\\f'
47 47 _jsonmap[0x0d] = '\\r'
48 48 _paranoidjsonmap = _jsonmap[:]
49 49 _paranoidjsonmap[0x3c] = '\\u003c' # '<' (e.g. escape "</script>")
50 50 _paranoidjsonmap[0x3e] = '\\u003e' # '>'
51 51 _jsonmap.extend(pycompat.bytechr(x) for x in range(128, 256))
52 52
53 53 def jsonescapeu8fast(u8chars, paranoid):
54 54 """Convert a UTF-8 byte string to JSON-escaped form (fast path)
55 55
56 56 Raises ValueError if non-ASCII characters have to be escaped.
57 57 """
58 58 if paranoid:
59 59 jm = _paranoidjsonmap
60 60 else:
61 61 jm = _jsonmap
62 62 try:
63 63 return ''.join(jm[x] for x in bytearray(u8chars))
64 64 except IndexError:
65 65 raise ValueError
66 66
67 67 def jsonescapeu8fallback(u8chars, paranoid):
68 68 """Convert a UTF-8 byte string to JSON-escaped form (slow path)
69 69
70 70 Escapes all non-ASCII characters no matter if paranoid is False.
71 71 """
72 72 if paranoid:
73 73 jm = _paranoidjsonmap
74 74 else:
75 75 jm = _jsonmap
76 76 # non-BMP char is represented as UTF-16 surrogate pair
77 u16codes = array.array('H', u8chars.decode('utf-8').encode('utf-16'))
77 u16codes = array.array(r'H', u8chars.decode('utf-8').encode('utf-16'))
78 78 u16codes.pop(0) # drop BOM
79 79 return ''.join(jm[x] if x < 128 else '\\u%04x' % x for x in u16codes)
General Comments 0
You need to be logged in to leave comments. Login now