-
Notifications
You must be signed in to change notification settings - Fork 72
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
87 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import string | ||
|
||
CTRL_CHAR_MAP = {"\t": "\\t", "\r": "\\r", "\n": "\\n"} | ||
|
||
|
||
def to_text_string(txt, add_size=False): | ||
"""convert a string to text representation but replace non-printable chars""" | ||
# replace non printable chars | ||
result = [] | ||
for c in txt: | ||
if c in CTRL_CHAR_MAP: | ||
result.append(CTRL_CHAR_MAP[c]) | ||
elif c in string.printable: | ||
# quote | ||
if c == "'": | ||
result.append("\\'") | ||
# regular char | ||
else: | ||
result.append(c) | ||
else: | ||
result.append(f"\\x{ord(c):02x}") | ||
new_txt = "".join(result) | ||
if add_size: | ||
return f"'{new_txt}'({len(txt)})" | ||
else: | ||
return f"'{new_txt}'" | ||
|
||
|
||
def to_binary_string(txt, crop_after=8): | ||
"""convert a string to binary and crop after a given number""" | ||
result = [] | ||
num = 0 | ||
ellipsis = False | ||
for c in txt: | ||
if c in string.printable and c not in CTRL_CHAR_MAP: | ||
result.append(f"'{c}'") | ||
else: | ||
result.append(f"{ord(c):02x}") | ||
num += 1 | ||
if num >= crop_after: | ||
ellipsis = True | ||
break | ||
new_txt = " ".join(result) | ||
if ellipsis: | ||
new_txt += " ..." | ||
return f"{new_txt} ({len(txt)})" | ||
|
||
|
||
def to_string(txt, txt_ratio=75, add_size=False): | ||
"""convert a string to a human-readable representation""" | ||
printable = 0 | ||
total = len(txt) | ||
for c in txt: | ||
if c in string.printable: | ||
printable += 1 | ||
ratio = printable * 100 // total | ||
# if likely a text? | ||
if ratio >= txt_ratio: | ||
return to_text_string(txt, add_size) | ||
else: | ||
return to_binary_string(txt) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
from amitools.util.strtool import to_text_string, to_binary_string, to_string | ||
|
||
|
||
def util_strtool_to_text_test(): | ||
assert to_text_string("hello, world!") == "'hello, world!'" | ||
assert to_text_string("hello\nworld!\r") == "'hello\\nworld!\\r'" | ||
assert to_text_string("\xa0") == "'\\xa0'" | ||
assert to_text_string("hello, world!", add_size=True) == "'hello, world!'(13)" | ||
|
||
|
||
def util_strtool_to_binary_test(): | ||
assert to_binary_string("abc") == "'a' 'b' 'c' (3)" | ||
assert to_binary_string("\xa0\xa1\xa2") == "a0 a1 a2 (3)" | ||
# ellipsis | ||
assert ( | ||
to_binary_string("hello, world!") == "'h' 'e' 'l' 'l' 'o' ',' ' ' 'w' ... (13)" | ||
) | ||
|
||
|
||
def util_strtool_to_str_test(): | ||
assert to_string("hello, world!") == "'hello, world!'" | ||
assert to_string("\xa0\xa1\xa2") == "a0 a1 a2 (3)" | ||
assert to_string("hello, world!", add_size=True) == "'hello, world!'(13)" |