From b46018e66611226da5b5532a422ba0c255967aa8 Mon Sep 17 00:00:00 2001 From: dgelessus Date: Mon, 6 Jul 2020 18:01:02 +0200 Subject: [PATCH] Use is_printable in definition of _TRANSLATE_NONPRINTABLES --- rsrcfork/__main__.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/rsrcfork/__main__.py b/rsrcfork/__main__.py index a67ba60..1efe412 100644 --- a/rsrcfork/__main__.py +++ b/rsrcfork/__main__.py @@ -11,9 +11,6 @@ from . import __version__, api, compress # The encoding to use when rendering bytes as text (in four-char codes, strings, hex dumps, etc.) or reading a quoted byte string (from the command line). _TEXT_ENCODING = "MacRoman" -# Translation table to replace ASCII non-printable characters with periods. -_TRANSLATE_NONPRINTABLES = {k: "." for k in [*range(0x20), 0x7f]} - _REZ_ATTR_NAMES = { api.ResourceAttrs.resSysRef: None, # "Illegal or reserved attribute" api.ResourceAttrs.resSysHeap: "sysheap", @@ -43,6 +40,10 @@ def is_printable(char: str) -> bool: return char.isprintable() or char == "\uf8ff" +# Translation table to replace non-printable characters with periods. +_TRANSLATE_NONPRINTABLES = {ord(c): "." for c in bytes(range(256)).decode(_TEXT_ENCODING) if not is_printable(c)} + + def bytes_unescape(string: str) -> bytes: """Convert a string containing text (in _TEXT_ENCODING) and hex escapes to a bytestring.