Skip to content

Commit d0e2235

Browse files
committed
Improve the RLE codec
1 parent 8d7b626 commit d0e2235

1 file changed

Lines changed: 16 additions & 14 deletions

File tree

pyrogram/file_id.py

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -63,20 +63,20 @@ def rle_encode(s: bytes) -> bytes:
6363
Returns:
6464
``bytes``: The encoded bytes
6565
"""
66-
r = b""
67-
n = 0
66+
r: bytes = b""
67+
n: int = 0
6868

6969
for b in s:
70-
if b == 0:
70+
if not b:
7171
n += 1
7272
else:
73-
if n > 0:
73+
if n:
7474
r += bytes([0, n])
7575
n = 0
7676

7777
r += bytes([b])
7878

79-
if n > 0:
79+
if n:
8080
r += bytes([0, n])
8181

8282
return r
@@ -92,17 +92,19 @@ def rle_decode(s: bytes) -> bytes:
9292
Returns:
9393
``bytes``: The encoded bytes
9494
"""
95-
r = b""
96-
i = 0
95+
r: bytes = b""
96+
z: bool = False
9797

98-
while i < len(s):
99-
if s[i] != 0:
100-
r += bytes([s[i]])
101-
else:
102-
r += b"\x00" * s[i + 1]
103-
i += 1
98+
for b in s:
99+
if not b:
100+
z = True
101+
continue
104102

105-
i += 1
103+
if z:
104+
r += b"\x00" * b
105+
z = False
106+
else:
107+
r += bytes([b])
106108

107109
return r
108110

0 commit comments

Comments
 (0)