Collapse multiple subsequent identical lines in hex dumps

This commit is contained in:
dgelessus
2019-09-13 10:40:03 +02:00
parent 752ec9e828
commit b2fa5f8b0f
2 changed files with 15 additions and 4 deletions

View File

@@ -130,7 +130,7 @@ Changelog
Version 1.2.1 (next version) Version 1.2.1 (next version)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
* (no changes yet) * Changed ``--format=dump`` output to collapse multiple subsequent identical lines into a single ``*``.
Version 1.2.0 Version 1.2.0
^^^^^^^^^^^^^ ^^^^^^^^^^^^^

View File

@@ -155,11 +155,22 @@ def _filter_resources(rf: api.ResourceFile, filters: typing.Sequence[str]) -> ty
return list(matching.values()) return list(matching.values())
def _hexdump(data: bytes): def _hexdump(data: bytes):
last_line = None
asterisk_shown = False
for i in range(0, len(data), 16): for i in range(0, len(data), 16):
line = data[i:i + 16] line = data[i:i + 16]
line_hex = " ".join(f"{byte:02x}" for byte in line) # If the same 16-byte lines appear multiple times, print only the first one, and replace all further lines with a single line with an asterisk.
line_char = line.decode(_TEXT_ENCODING).translate(_TRANSLATE_NONPRINTABLES) # This is unambiguous - to find out how many lines were collapsed this way, the user can compare the addresses of the lines before and after the asterisk.
print(f"{i:08x} {line_hex:<{16*2+15}} |{line_char}|") if line == last_line:
if not asterisk_shown:
print("*")
asterisk_shown = True
else:
line_hex = " ".join(f"{byte:02x}" for byte in line)
line_char = line.decode(_TEXT_ENCODING).translate(_TRANSLATE_NONPRINTABLES)
print(f"{i:08x} {line_hex:<{16*2+15}} |{line_char}|")
asterisk_shown = False
last_line = line
if data: if data:
print(f"{len(data):08x}") print(f"{len(data):08x}")