From 6e5d7c23951a83de6a9496bcc475837d35e76179 Mon Sep 17 00:00:00 2001 From: Roland van Laar Date: Mon, 9 Aug 2021 00:22:18 +0200 Subject: [PATCH] DEVTOOLS: COMPANION: bring escape inline Original dumper-companion.pl script encoded \x81 to \x81\x79\x81. - added test to check for correct behavior - refactored escape string to handle this special case --- devtools/dumper-companion.py | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/devtools/dumper-companion.py b/devtools/dumper-companion.py index 1cb0dc8f1b3..3ae3aee4534 100755 --- a/devtools/dumper-companion.py +++ b/devtools/dumper-companion.py @@ -52,10 +52,14 @@ def file_to_macbin(f: machfs.File, name: str, encoding: str) -> bytes: def escape_string(s: str) -> str: - new_name = [ - "\x81" + chr(0x80 + ord(i)) if i in '/":*[]+|\\?%<>,;=' or ord(i) < 0x20 else i - for i in s - ] + new_name = "" + for char in s: + if char == "\x81": + new_name += "\x81\x79" + if char in '/":*[]+|\\?%<>,;=' or ord(char) < 0x20: + new_name += "\x81" + chr(0x80 + ord(char)) + else: + new_name += char return "".join(new_name) @@ -190,6 +194,6 @@ def test_decode_name(): def test_escape_string(): - checks = [["\r", "\x81\x8d"]] + checks = [["\r", "\x81\x8d"], ["\x81", "\x81\x79\x81"]] for input, expected in checks: assert escape_string(input) == expected