7 |
7 |
8 from __future__ import absolute_import |
8 from __future__ import absolute_import |
9 |
9 |
10 import array |
10 import array |
11 |
11 |
12 from .. import ( |
12 from .. import pycompat |
13 pycompat, |
13 |
14 ) |
|
15 |
14 |
16 def isasciistr(s): |
15 def isasciistr(s): |
17 try: |
16 try: |
18 s.decode('ascii') |
17 s.decode('ascii') |
19 return True |
18 return True |
20 except UnicodeDecodeError: |
19 except UnicodeDecodeError: |
21 return False |
20 return False |
22 |
21 |
|
22 |
23 def asciilower(s): |
23 def asciilower(s): |
24 '''convert a string to lowercase if ASCII |
24 '''convert a string to lowercase if ASCII |
25 |
25 |
26 Raises UnicodeDecodeError if non-ASCII characters are found.''' |
26 Raises UnicodeDecodeError if non-ASCII characters are found.''' |
27 s.decode('ascii') |
27 s.decode('ascii') |
28 return s.lower() |
28 return s.lower() |
|
29 |
29 |
30 |
30 def asciiupper(s): |
31 def asciiupper(s): |
31 '''convert a string to uppercase if ASCII |
32 '''convert a string to uppercase if ASCII |
32 |
33 |
33 Raises UnicodeDecodeError if non-ASCII characters are found.''' |
34 Raises UnicodeDecodeError if non-ASCII characters are found.''' |
34 s.decode('ascii') |
35 s.decode('ascii') |
35 return s.upper() |
36 return s.upper() |
36 |
37 |
|
38 |
37 _jsonmap = [] |
39 _jsonmap = [] |
38 _jsonmap.extend("\\u%04x" % x for x in range(32)) |
40 _jsonmap.extend("\\u%04x" % x for x in range(32)) |
39 _jsonmap.extend(pycompat.bytechr(x) for x in range(32, 127)) |
41 _jsonmap.extend(pycompat.bytechr(x) for x in range(32, 127)) |
40 _jsonmap.append('\\u007f') |
42 _jsonmap.append('\\u007f') |
41 _jsonmap[0x09] = '\\t' |
43 _jsonmap[0x09] = '\\t' |
42 _jsonmap[0x0a] = '\\n' |
44 _jsonmap[0x0A] = '\\n' |
43 _jsonmap[0x22] = '\\"' |
45 _jsonmap[0x22] = '\\"' |
44 _jsonmap[0x5c] = '\\\\' |
46 _jsonmap[0x5C] = '\\\\' |
45 _jsonmap[0x08] = '\\b' |
47 _jsonmap[0x08] = '\\b' |
46 _jsonmap[0x0c] = '\\f' |
48 _jsonmap[0x0C] = '\\f' |
47 _jsonmap[0x0d] = '\\r' |
49 _jsonmap[0x0D] = '\\r' |
48 _paranoidjsonmap = _jsonmap[:] |
50 _paranoidjsonmap = _jsonmap[:] |
49 _paranoidjsonmap[0x3c] = '\\u003c' # '<' (e.g. escape "</script>") |
51 _paranoidjsonmap[0x3C] = '\\u003c' # '<' (e.g. escape "</script>") |
50 _paranoidjsonmap[0x3e] = '\\u003e' # '>' |
52 _paranoidjsonmap[0x3E] = '\\u003e' # '>' |
51 _jsonmap.extend(pycompat.bytechr(x) for x in range(128, 256)) |
53 _jsonmap.extend(pycompat.bytechr(x) for x in range(128, 256)) |
|
54 |
52 |
55 |
53 def jsonescapeu8fast(u8chars, paranoid): |
56 def jsonescapeu8fast(u8chars, paranoid): |
54 """Convert a UTF-8 byte string to JSON-escaped form (fast path) |
57 """Convert a UTF-8 byte string to JSON-escaped form (fast path) |
55 |
58 |
56 Raises ValueError if non-ASCII characters have to be escaped. |
59 Raises ValueError if non-ASCII characters have to be escaped. |
62 try: |
65 try: |
63 return ''.join(jm[x] for x in bytearray(u8chars)) |
66 return ''.join(jm[x] for x in bytearray(u8chars)) |
64 except IndexError: |
67 except IndexError: |
65 raise ValueError |
68 raise ValueError |
66 |
69 |
|
70 |
67 if pycompat.ispy3: |
71 if pycompat.ispy3: |
68 _utf8strict = r'surrogatepass' |
72 _utf8strict = r'surrogatepass' |
69 else: |
73 else: |
70 _utf8strict = r'strict' |
74 _utf8strict = r'strict' |
|
75 |
71 |
76 |
72 def jsonescapeu8fallback(u8chars, paranoid): |
77 def jsonescapeu8fallback(u8chars, paranoid): |
73 """Convert a UTF-8 byte string to JSON-escaped form (slow path) |
78 """Convert a UTF-8 byte string to JSON-escaped form (slow path) |
74 |
79 |
75 Escapes all non-ASCII characters no matter if paranoid is False. |
80 Escapes all non-ASCII characters no matter if paranoid is False. |