Mercurial > hg
comparison mercurial/pure/base85.py @ 43076:2372284d9457
formatting: blacken the codebase
This is using my patch to black
(https://github.com/psf/black/pull/826) so we don't un-wrap collection
literals.
Done with:
hg files 'set:**.py - mercurial/thirdparty/** - "contrib/python-zstandard/**"' | xargs black -S
# skip-blame mass-reformatting only
# no-check-commit reformats foo_bar functions
Differential Revision: https://phab.mercurial-scm.org/D6971
author | Augie Fackler <augie@google.com> |
---|---|
date | Sun, 06 Oct 2019 09:45:02 -0400 |
parents | 80301c90a2dc |
children | 687b865b95ad |
comparison
equal
deleted
inserted
replaced
43075:57875cf423c9 | 43076:2372284d9457 |
---|---|
9 | 9 |
10 import struct | 10 import struct |
11 | 11 |
12 from .. import pycompat | 12 from .. import pycompat |
13 | 13 |
14 _b85chars = pycompat.bytestr("0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdef" | 14 _b85chars = pycompat.bytestr( |
15 "ghijklmnopqrstuvwxyz!#$%&()*+-;<=>?@^_`{|}~") | 15 "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdef" |
16 "ghijklmnopqrstuvwxyz!#$%&()*+-;<=>?@^_`{|}~" | |
17 ) | |
16 _b85chars2 = [(a + b) for a in _b85chars for b in _b85chars] | 18 _b85chars2 = [(a + b) for a in _b85chars for b in _b85chars] |
17 _b85dec = {} | 19 _b85dec = {} |
20 | |
18 | 21 |
19 def _mkb85dec(): | 22 def _mkb85dec(): |
20 for i, c in enumerate(_b85chars): | 23 for i, c in enumerate(_b85chars): |
21 _b85dec[c] = i | 24 _b85dec[c] = i |
25 | |
22 | 26 |
23 def b85encode(text, pad=False): | 27 def b85encode(text, pad=False): |
24 """encode text in base85 format""" | 28 """encode text in base85 format""" |
25 l = len(text) | 29 l = len(text) |
26 r = l % 4 | 30 r = l % 4 |
27 if r: | 31 if r: |
28 text += '\0' * (4 - r) | 32 text += '\0' * (4 - r) |
29 longs = len(text) >> 2 | 33 longs = len(text) >> 2 |
30 words = struct.unpack('>%dL' % (longs), text) | 34 words = struct.unpack('>%dL' % longs, text) |
31 | 35 |
32 out = ''.join(_b85chars[(word // 52200625) % 85] + | 36 out = ''.join( |
33 _b85chars2[(word // 7225) % 7225] + | 37 _b85chars[(word // 52200625) % 85] |
34 _b85chars2[word % 7225] | 38 + _b85chars2[(word // 7225) % 7225] |
35 for word in words) | 39 + _b85chars2[word % 7225] |
40 for word in words | |
41 ) | |
36 | 42 |
37 if pad: | 43 if pad: |
38 return out | 44 return out |
39 | 45 |
40 # Trim padding | 46 # Trim padding |
42 if olen: | 48 if olen: |
43 olen += 1 | 49 olen += 1 |
44 olen += l // 4 * 5 | 50 olen += l // 4 * 5 |
45 return out[:olen] | 51 return out[:olen] |
46 | 52 |
53 | |
47 def b85decode(text): | 54 def b85decode(text): |
48 """decode base85-encoded text""" | 55 """decode base85-encoded text""" |
49 if not _b85dec: | 56 if not _b85dec: |
50 _mkb85dec() | 57 _mkb85dec() |
51 | 58 |
52 l = len(text) | 59 l = len(text) |
53 out = [] | 60 out = [] |
54 for i in range(0, len(text), 5): | 61 for i in range(0, len(text), 5): |
55 chunk = text[i:i + 5] | 62 chunk = text[i : i + 5] |
56 chunk = pycompat.bytestr(chunk) | 63 chunk = pycompat.bytestr(chunk) |
57 acc = 0 | 64 acc = 0 |
58 for j, c in enumerate(chunk): | 65 for j, c in enumerate(chunk): |
59 try: | 66 try: |
60 acc = acc * 85 + _b85dec[c] | 67 acc = acc * 85 + _b85dec[c] |
61 except KeyError: | 68 except KeyError: |
62 raise ValueError('bad base85 character at position %d' | 69 raise ValueError( |
63 % (i + j)) | 70 'bad base85 character at position %d' % (i + j) |
71 ) | |
64 if acc > 4294967295: | 72 if acc > 4294967295: |
65 raise ValueError('Base85 overflow in hunk starting at byte %d' % i) | 73 raise ValueError('Base85 overflow in hunk starting at byte %d' % i) |
66 out.append(acc) | 74 out.append(acc) |
67 | 75 |
68 # Pad final chunk if necessary | 76 # Pad final chunk if necessary |
69 cl = l % 5 | 77 cl = l % 5 |
70 if cl: | 78 if cl: |
71 acc *= 85 ** (5 - cl) | 79 acc *= 85 ** (5 - cl) |
72 if cl > 1: | 80 if cl > 1: |
73 acc += 0xffffff >> (cl - 2) * 8 | 81 acc += 0xFFFFFF >> (cl - 2) * 8 |
74 out[-1] = acc | 82 out[-1] = acc |
75 | 83 |
76 out = struct.pack('>%dL' % (len(out)), *out) | 84 out = struct.pack('>%dL' % (len(out)), *out) |
77 if cl: | 85 if cl: |
78 out = out[:-(5 - cl)] | 86 out = out[: -(5 - cl)] |
79 | 87 |
80 return out | 88 return out |