|
14 | 14 | # - codepoint2name lives in a different module |
15 | 15 | import platform |
16 | 16 | if platform.python_version_tuple()[0] == '2': |
17 | | - ord_bytes = ord |
| 17 | + bytes_cons = lambda val, enc=None: bytearray(val) |
18 | 18 | from htmlentitydefs import codepoint2name |
19 | 19 | elif platform.python_version_tuple()[0] == '3': |
20 | | - ord_bytes = lambda x:x |
| 20 | + bytes_cons = bytes |
21 | 21 | from html.entities import codepoint2name |
| 22 | +# end compatibility code |
| 23 | + |
22 | 24 | codepoint2name[ord('-')] = 'hyphen'; |
23 | 25 |
|
24 | 26 | # add some custom names to map characters that aren't in HTML |
|
52 | 54 | # this must match the equivalent function in qstr.c |
53 | 55 | def compute_hash(qstr, bytes_hash): |
54 | 56 | hash = 5381 |
55 | | - for char in qstr: |
56 | | - hash = (hash * 33) ^ ord(char) |
| 57 | + for b in qstr: |
| 58 | + hash = (hash * 33) ^ b |
57 | 59 | # Make sure that valid hash is never zero, zero means "hash not computed" |
58 | 60 | return (hash & ((1 << (8 * bytes_hash)) - 1)) or 1 |
59 | 61 |
|
@@ -115,16 +117,15 @@ def parse_input_headers(infiles): |
115 | 117 | return qcfgs, qstrs |
116 | 118 |
|
117 | 119 | def make_bytes(cfg_bytes_len, cfg_bytes_hash, qstr): |
118 | | - qhash = compute_hash(qstr, cfg_bytes_hash) |
| 120 | + qbytes = bytes_cons(qstr, 'utf8') |
| 121 | + qlen = len(qbytes) |
| 122 | + qhash = compute_hash(qbytes, cfg_bytes_hash) |
119 | 123 | if all(32 <= ord(c) <= 126 and c != '\\' and c != '"' for c in qstr): |
120 | 124 | # qstr is all printable ASCII so render it as-is (for easier debugging) |
121 | | - qlen = len(qstr) |
122 | 125 | qdata = qstr |
123 | 126 | else: |
124 | 127 | # qstr contains non-printable codes so render entire thing as hex pairs |
125 | | - qbytes = qstr.encode('utf8') |
126 | | - qlen = len(qbytes) |
127 | | - qdata = ''.join(('\\x%02x' % ord_bytes(b)) for b in qbytes) |
| 128 | + qdata = ''.join(('\\x%02x' % b) for b in qbytes) |
128 | 129 | if qlen >= (1 << (8 * cfg_bytes_len)): |
129 | 130 | print('qstr is too long:', qstr) |
130 | 131 | assert False |
|
0 commit comments