From d0e2235835e8ecdf557df7a876502a16be654eba Mon Sep 17 00:00:00 2001 From: Dan <14043624+delivrance@users.noreply.github.com> Date: Fri, 11 Mar 2022 12:29:05 +0100 Subject: [PATCH] Improve the RLE codec --- pyrogram/file_id.py | 30 ++++++++++++++++-------------- 1 file changed, 16 insertions(+), 14 deletions(-) diff --git a/pyrogram/file_id.py b/pyrogram/file_id.py index ee1ccc4a..95f6ccbf 100644 --- a/pyrogram/file_id.py +++ b/pyrogram/file_id.py @@ -63,20 +63,20 @@ def rle_encode(s: bytes) -> bytes: Returns: ``bytes``: The encoded bytes """ - r = b"" - n = 0 + r: bytes = b"" + n: int = 0 for b in s: - if b == 0: + if not b: n += 1 else: - if n > 0: + if n: r += bytes([0, n]) n = 0 r += bytes([b]) - if n > 0: + if n: r += bytes([0, n]) return r @@ -92,17 +92,19 @@ def rle_decode(s: bytes) -> bytes: Returns: ``bytes``: The encoded bytes """ - r = b"" - i = 0 + r: bytes = b"" + z: bool = False - while i < len(s): - if s[i] != 0: - r += bytes([s[i]]) + for b in s: + if not b: + z = True + continue + + if z: + r += b"\x00" * b + z = False else: - r += b"\x00" * s[i + 1] - i += 1 - - i += 1 + r += bytes([b]) return r