Source code for contextbench.core.fileio

"""File I/O and byte offset computation."""

from typing import List, Optional, Tuple

[docs] def line_to_byte(file_path: str, start_line: int, end_line: int) -> Optional[Tuple[int, int]]: """Convert line span to byte offsets. Returns (start_byte, end_byte) inclusive.""" try: with open(file_path, 'rb') as f: content = f.read() except (OSError, IOError): return None if not content: return (0, 0) # Build line offset table offsets = [0] for i, byte in enumerate(content): if byte == ord('\n'): offsets.append(i + 1) start_line = max(1, start_line) end_line = max(start_line, end_line) start_idx = start_line - 1 end_idx = end_line - 1 if start_idx >= len(offsets): return None start_byte = offsets[start_idx] end_byte = offsets[end_idx + 1] - 1 if end_idx + 1 < len(offsets) else len(content) - 1 end_byte = max(start_byte, end_byte) return (start_byte, end_byte)