2020-08-01 16:47:48 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# __all__ = ("str_to_bytes", "make_string", "format_rect", "format_point", "format_size")
|
|
|
|
|
2023-12-27 15:58:42 +00:00
|
|
|
# decorators
|
|
|
|
def export_enum(cls):
|
|
|
|
global __all__
|
|
|
|
|
|
|
|
members = cls.__members__
|
|
|
|
globals().update(members)
|
|
|
|
if __all__ != None: __all__.extend(list(members))
|
|
|
|
return cls
|
|
|
|
|
|
|
|
|
2020-08-01 16:47:48 +00:00
|
|
|
# helper functions
|
|
|
|
def str_to_bytes(text):
|
|
|
|
if isinstance(text, str): return text.encode("macroman")
|
|
|
|
if isinstance(text, bytes): return text
|
|
|
|
if isinstance(text, bytearray): return bytes(text)
|
|
|
|
raise TypeError("Bad text type: {}".format(type(text)))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def format_rect(x):
|
|
|
|
return "{{ {:d}, {:d}, {:d}, {:d} }}".format(*x)
|
|
|
|
|
|
|
|
def format_point(x):
|
|
|
|
return "{{ {:d}, {:d} }}".format(*x)
|
|
|
|
|
|
|
|
def format_size(x):
|
|
|
|
return "{{ {:d}, {:d} }}".format(*x)
|
|
|
|
|
|
|
|
|
2020-08-01 19:33:19 +00:00
|
|
|
def _generate_map():
|
|
|
|
map = { x: "\\${:02x}".format(x) for x in range(0, 256) if x < 32 or x > 126 }
|
2023-12-27 15:58:42 +00:00
|
|
|
map[0x0d] = "\\r" # intentionally backwards. -- no more
|
|
|
|
map[0x0a] = "\\n" # intentionally backwards. -- no more
|
2020-08-01 19:33:19 +00:00
|
|
|
map[0x09] = "\\t"
|
|
|
|
# \b \f \v \? also supported.
|
|
|
|
map[ord('"')] = '\\"'
|
|
|
|
map[ord("'")] = "\\'"
|
|
|
|
map[ord("\\")] = "\\\\"
|
|
|
|
# map[0x7f] = "\\?" # rubout
|
|
|
|
|
|
|
|
return map
|
|
|
|
|
|
|
|
_map = _generate_map()
|
|
|
|
|
|
|
|
def format_string(bstring, quote=True):
|
|
|
|
s = "".join([_map[x] if x in _map else chr(x) for x in bstring])
|
|
|
|
if quote: return '"' + s + '"'
|
|
|
|
return s
|
|
|
|
|
2020-08-04 03:42:09 +00:00
|
|
|
# rez "char" is a string of length 1.
|
|
|
|
# 'x' is character literal which is a number.
|
2020-08-03 03:00:35 +00:00
|
|
|
def format_char(x):
|
2020-08-09 04:34:05 +00:00
|
|
|
if x == 0: return '""'
|
2020-08-03 03:00:35 +00:00
|
|
|
c = _map.get(x, chr(x))
|
2020-08-04 03:42:09 +00:00
|
|
|
return '"' + c + '"'
|
2020-08-03 03:00:35 +00:00
|
|
|
|
2020-08-01 19:33:19 +00:00
|
|
|
def multi_format_string(bstring, indent=""):
|
|
|
|
q = '"'
|
|
|
|
|
|
|
|
rv = []
|
|
|
|
tmp = [indent + q]
|
|
|
|
for x in bstring:
|
|
|
|
if x in _map: tmp.append(_map[x])
|
|
|
|
else: tmp.append(chr(x))
|
|
|
|
if len(tmp) > 32 or x == 0x0a:
|
|
|
|
tmp.append(q)
|
|
|
|
rv.append("".join(tmp))
|
|
|
|
tmp = [indent + q]
|
|
|
|
if len(tmp)>1:
|
|
|
|
tmp.append(q)
|
|
|
|
rv.append("".join(tmp))
|
|
|
|
return ("\n").join(rv)
|