Coverage for src/renaissance/common/rewriter.py: 100%
24 statements
« prev ^ index » next coverage.py v7.17.0, created at 2026-11-16 16:04 +0000
« prev ^ index » next coverage.py v7.17.0, created at 2026-11-16 16:04 +0000
1# Copyright (C) 2024-2026 TNO
2#
3# Licensed under the terms specified in the LICENSE file in the root directory
4# of this repository, also available at:
5# https://github.com/TNO/Renaissance.Py/blob/main/LICENSE
7"""AI: Byte-level content rewriter that accumulates and applies pending replacements."""
10class Rewrite:
11 """AI: Represent one pending replacement of a byte range with new content."""
13 def __init__(self, start: int, end: int, replacement: bytes) -> None:
14 """AI: Record a pending replacement of a byte range with new content."""
15 self.start = start
16 self.end = end
17 self.replacement = replacement
20class Rewriter:
21 """A class that allows for modifications to a byte sequence."""
23 def __init__(self, content: bytes) -> None:
24 """AI: Prepare a rewriter that accumulates edits to apply to the given content."""
25 self.__content = content
26 self.__rewrites: list[Rewrite] = []
28 def replace(self, start: int, end: int, new_content: bytes) -> None:
29 """Replace a portion of the content with new content.
31 This method will replace the content between the specified start and end
32 indices with the provided new_content. If there is an existing rewrite
33 that partially overlaps with the specified range, the new content will be
34 appended to the existing replacement, and the range will be adjusted to
35 encompass both the old and new content. If the start or end indices are out
36 of bounds, then new content will be inserted at the end of the byte sequence.
38 Args:
39 start (int): The starting index of the content to be replaced.
40 end (int): The ending index of the content to be replaced.
41 new_content (bytes): The new content to insert in place of the old content.
43 """
44 for r in self.__rewrites:
45 # if r partially overlaps with start and end then append the new content to the existing replacement
46 if r.start <= start <= r.end:
47 r.replacement += new_content
48 r.start = min(r.start, start)
49 r.end = max(r.end, end)
50 return
51 real_start = len(self.__content) if start > len(self.__content) or start < 0 else start
52 real_end = len(self.__content) if end > len(self.__content) or end < 0 else end
53 self.__rewrites.append(Rewrite(real_start, real_end, new_content))
55 def apply(self) -> bytes:
56 """Apply the rewrites to a copied byte sequence.
58 This method reverses the order of the rewrites to ensure that insertions
59 are performed correctly. It then sorts the rewrites by their start position
60 in descending order and applies each rewrite to the byte sequence.
62 Returns:
63 bytes: The modified byte sequence after all rewrites have been applied.
65 """
66 result = bytearray(self.__content[:])
67 for rewrite in sorted(self.__rewrites, key=lambda x: x.start, reverse=True):
68 result[rewrite.start : rewrite.end] = rewrite.replacement
69 return result