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: