2
0
mirror of https://github.com/pyrogram/pyrogram synced 2025-08-28 12:57:52 +00:00

Improve the RLE codec

This commit is contained in:
Dan 2022-03-11 12:29:05 +01:00
parent 8d7b62654c
commit d0e2235835

View File

@ -63,20 +63,20 @@ def rle_encode(s: bytes) -> bytes:
Returns: Returns:
``bytes``: The encoded bytes ``bytes``: The encoded bytes
""" """
r = b"" r: bytes = b""
n = 0 n: int = 0
for b in s: for b in s:
if b == 0: if not b:
n += 1 n += 1
else: else:
if n > 0: if n:
r += bytes([0, n]) r += bytes([0, n])
n = 0 n = 0
r += bytes([b]) r += bytes([b])
if n > 0: if n:
r += bytes([0, n]) r += bytes([0, n])
return r return r
@ -92,17 +92,19 @@ def rle_decode(s: bytes) -> bytes:
Returns: Returns:
``bytes``: The encoded bytes ``bytes``: The encoded bytes
""" """
r = b"" r: bytes = b""
i = 0 z: bool = False
while i < len(s): for b in s:
if s[i] != 0: if not b:
r += bytes([s[i]]) z = True
continue
if z:
r += b"\x00" * b
z = False
else: else:
r += b"\x00" * s[i + 1] r += bytes([b])
i += 1
i += 1
return r return r