26 # efficiently, we do all our operations inside a buffer created by |
31 # efficiently, we do all our operations inside a buffer created by |
27 # mmap and simply use memmove. This avoids creating a bunch of large |
32 # mmap and simply use memmove. This avoids creating a bunch of large |
28 # temporary string buffers. |
33 # temporary string buffers. |
29 |
34 |
30 |
35 |
31 def _pull(dst, src, l): # pull l bytes from src |
36 def _pull( |
|
37 dst: List[Tuple[int, int]], src: List[Tuple[int, int]], l: int |
|
38 ) -> None: # pull l bytes from src |
32 while l: |
39 while l: |
33 f = src.pop() |
40 f = src.pop() |
34 if f[0] > l: # do we need to split? |
41 if f[0] > l: # do we need to split? |
35 src.append((f[0] - l, f[1] + l)) |
42 src.append((f[0] - l, f[1] + l)) |
36 dst.append((l, f[1])) |
43 dst.append((l, f[1])) |
37 return |
44 return |
38 dst.append(f) |
45 dst.append(f) |
39 l -= f[0] |
46 l -= f[0] |
40 |
47 |
41 |
48 |
42 def _move(m, dest, src, count): |
49 def _move(m: stringio, dest: int, src: int, count: int) -> None: |
43 """move count bytes from src to dest |
50 """move count bytes from src to dest |
44 |
51 |
45 The file pointer is left at the end of dest. |
52 The file pointer is left at the end of dest. |
46 """ |
53 """ |
47 m.seek(src) |
54 m.seek(src) |
48 buf = m.read(count) |
55 buf = m.read(count) |
49 m.seek(dest) |
56 m.seek(dest) |
50 m.write(buf) |
57 m.write(buf) |
51 |
58 |
52 |
59 |
53 def _collect(m, buf, list): |
60 def _collect( |
|
61 m: stringio, buf: int, list: List[Tuple[int, int]] |
|
62 ) -> Tuple[int, int]: |
54 start = buf |
63 start = buf |
55 for l, p in reversed(list): |
64 for l, p in reversed(list): |
56 _move(m, buf, p, l) |
65 _move(m, buf, p, l) |
57 buf += l |
66 buf += l |
58 return (buf - start, start) |
67 return (buf - start, start) |
59 |
68 |
60 |
69 |
61 def patches(a, bins): |
70 def patches(a: bytes, bins: List[bytes]) -> bytes: |
62 if not bins: |
71 if not bins: |
63 return a |
72 return a |
64 |
73 |
65 plens = [len(x) for x in bins] |
74 plens = [len(x) for x in bins] |
66 pl = sum(plens) |
75 pl = sum(plens) |