improved text encoding

This commit is contained in:
Kelvin Sherlock 2020-07-30 01:07:02 -04:00
parent cac3129e56
commit 47c8e1c704

41
base.py
View File

@ -157,7 +157,34 @@ class rList(rObject):
rv += "\n\t}"
return rv
def _generate_map():
map = { x: "\\${:02x}".format(x) for x in range(0, 256) if x < 32 or x > 126 }
map[0x0d] = "\\n" # intentionally backwards.
map[0x0a] = "\\r" # intentionally backwards.
map[0x09] = "\\t"
# \b \f \v \? also supported.
map[ord('"')] = '\\"'
map[ord("'")] = "\\'"
map[ord("\\")] = "\\\\"
# map[0x7f] = "\\?" # rubout
return map
class rTextObject(rObject):
# _map = { x: "\\${:02x}".format(x) for x in range(0, 256) if x < 32 or x > 126 }
_map = _generate_map()
@staticmethod
def _encode(bb):
map = rTextObject._map
return "".join([map[x] if x in map else chr(x) for x in bb])
def __init__(self, text, *, id=None, attr=None):
super().__init__(id=id, attr=attr)
# text is a string or bytes.
@ -165,12 +192,22 @@ class rTextObject(rObject):
self.text = text
self._text = str_to_bytes(text)
def __len__(self):
return len(self._text)
def __bytes__(self):
return self._text
def _rez_string(self):
# todo - should extended chars be macroman?
return '\t"' + self._text.decode("ascii") + '"'
if not self._text: return '\t""\n'
bb = self._text
data = [bb[x*32:x*32+32] for x in range(0, len(bb)+31>>5)]
rv = ""
rv = "\n".join(['\t"' + self._encode(x) + '"' for x in data])
return rv
class rText(rTextObject):