Mercurial > public > mercurial-scm > hg
comparison mercurial/pure/charencode.py @ 43077:687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Done with
python3.7 contrib/byteify-strings.py -i $(hg files 'set:mercurial/**.py - mercurial/thirdparty/** + hgext/**.py - hgext/fsmonitor/pywatchman/** - mercurial/__init__.py')
black -l 80 -t py33 -S $(hg files 'set:**.py - mercurial/thirdparty/** - "contrib/python-zstandard/**" - hgext/fsmonitor/pywatchman/**')
# skip-blame mass-reformatting only
Differential Revision: https://phab.mercurial-scm.org/D6972
author | Augie Fackler <augie@google.com> |
---|---|
date | Sun, 06 Oct 2019 09:48:39 -0400 |
parents | 2372284d9457 |
children | 9f70512ae2cf |
comparison
equal
deleted
inserted
replaced
43076:2372284d9457 | 43077:687b865b95ad |
---|---|
35 s.decode('ascii') | 35 s.decode('ascii') |
36 return s.upper() | 36 return s.upper() |
37 | 37 |
38 | 38 |
39 _jsonmap = [] | 39 _jsonmap = [] |
40 _jsonmap.extend("\\u%04x" % x for x in range(32)) | 40 _jsonmap.extend(b"\\u%04x" % x for x in range(32)) |
41 _jsonmap.extend(pycompat.bytechr(x) for x in range(32, 127)) | 41 _jsonmap.extend(pycompat.bytechr(x) for x in range(32, 127)) |
42 _jsonmap.append('\\u007f') | 42 _jsonmap.append(b'\\u007f') |
43 _jsonmap[0x09] = '\\t' | 43 _jsonmap[0x09] = b'\\t' |
44 _jsonmap[0x0A] = '\\n' | 44 _jsonmap[0x0A] = b'\\n' |
45 _jsonmap[0x22] = '\\"' | 45 _jsonmap[0x22] = b'\\"' |
46 _jsonmap[0x5C] = '\\\\' | 46 _jsonmap[0x5C] = b'\\\\' |
47 _jsonmap[0x08] = '\\b' | 47 _jsonmap[0x08] = b'\\b' |
48 _jsonmap[0x0C] = '\\f' | 48 _jsonmap[0x0C] = b'\\f' |
49 _jsonmap[0x0D] = '\\r' | 49 _jsonmap[0x0D] = b'\\r' |
50 _paranoidjsonmap = _jsonmap[:] | 50 _paranoidjsonmap = _jsonmap[:] |
51 _paranoidjsonmap[0x3C] = '\\u003c' # '<' (e.g. escape "</script>") | 51 _paranoidjsonmap[0x3C] = b'\\u003c' # '<' (e.g. escape "</script>") |
52 _paranoidjsonmap[0x3E] = '\\u003e' # '>' | 52 _paranoidjsonmap[0x3E] = b'\\u003e' # '>' |
53 _jsonmap.extend(pycompat.bytechr(x) for x in range(128, 256)) | 53 _jsonmap.extend(pycompat.bytechr(x) for x in range(128, 256)) |
54 | 54 |
55 | 55 |
56 def jsonescapeu8fast(u8chars, paranoid): | 56 def jsonescapeu8fast(u8chars, paranoid): |
57 """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) |
61 if paranoid: | 61 if paranoid: |
62 jm = _paranoidjsonmap | 62 jm = _paranoidjsonmap |
63 else: | 63 else: |
64 jm = _jsonmap | 64 jm = _jsonmap |
65 try: | 65 try: |
66 return ''.join(jm[x] for x in bytearray(u8chars)) | 66 return b''.join(jm[x] for x in bytearray(u8chars)) |
67 except IndexError: | 67 except IndexError: |
68 raise ValueError | 68 raise ValueError |
69 | 69 |
70 | 70 |
71 if pycompat.ispy3: | 71 if pycompat.ispy3: |
85 jm = _jsonmap | 85 jm = _jsonmap |
86 # non-BMP char is represented as UTF-16 surrogate pair | 86 # non-BMP char is represented as UTF-16 surrogate pair |
87 u16b = u8chars.decode('utf-8', _utf8strict).encode('utf-16', _utf8strict) | 87 u16b = u8chars.decode('utf-8', _utf8strict).encode('utf-16', _utf8strict) |
88 u16codes = array.array(r'H', u16b) | 88 u16codes = array.array(r'H', u16b) |
89 u16codes.pop(0) # drop BOM | 89 u16codes.pop(0) # drop BOM |
90 return ''.join(jm[x] if x < 128 else '\\u%04x' % x for x in u16codes) | 90 return b''.join(jm[x] if x < 128 else b'\\u%04x' % x for x in u16codes) |