From c5ed6bfe190e3e8aa84b25b1655de6dbe65b8b37 Mon Sep 17 00:00:00 2001 From: "google-labs-jules[bot]" <161369871+google-labs-jules[bot]@users.noreply.github.com> Date: Mon, 8 Jun 2026 00:35:38 +0000 Subject: [PATCH] =?UTF-8?q?=E2=9A=A1=20Bolt:=20optimize=20yEnc=20decoding?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Replace manual byte-by-byte iteration with C-backed `bytes.find()` and `bytearray.translate()` - Create precomputed translation table `_YENC_TRANS` for fast transformation Co-authored-by: xbmc4lyfe <273732874+xbmc4lyfe@users.noreply.github.com> --- verify_nzb.py | 34 +++++++++++++++++++++++----------- 1 file changed, 23 insertions(+), 11 deletions(-) diff --git a/verify_nzb.py b/verify_nzb.py index 953dccd..0f730cb 100644 --- a/verify_nzb.py +++ b/verify_nzb.py @@ -115,20 +115,32 @@ def _parse_yenc_attrs(line: bytes) -> dict[str, str]: return attrs +_YENC_TRANS = bytes((i - 42) % 256 for i in range(256)) + + def _decode_yenc_lines(lines: Iterable[bytes]) -> bytes: + # ⚡ Bolt Optimization: Use C-backed `bytes.find()` and `translate()` + # instead of manual byte-by-byte iteration for significant speedup. decoded = bytearray() for line in lines: - index = 0 - while index < len(line): - byte = line[index] - if byte == 61: - index += 1 - if index >= len(line): - raise ValueError("dangling yEnc escape") - byte = (line[index] - 64) % 256 - decoded.append((byte - 42) % 256) - index += 1 - return bytes(decoded) + pos = line.find(b"=") + if pos == -1: + decoded.extend(line) + continue + + start = 0 + line_len = len(line) + while pos != -1: + decoded.extend(line[start:pos]) + pos += 1 + if pos >= line_len: + raise ValueError("dangling yEnc escape") + decoded.append((line[pos] - 64) % 256) + start = pos + 1 + pos = line.find(b"=", start) + decoded.extend(line[start:]) + + return bytes(decoded.translate(_YENC_TRANS)) def validate_yenc_body(lines: Iterable[bytes | str]) -> YencValidationResult: