Skip to content

Commit 2db25f8

Browse files
committed
Added new search argument to the script
1 parent e8ea85c commit 2db25f8

2 files changed

Lines changed: 19 additions & 2 deletions

File tree

codext/__common__.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -369,7 +369,7 @@ def examples(encoding_regex, number=10):
369369
for s in generate_strings_from_regex(search_function.__pattern__, yield_max=256*number):
370370
temp.append(s)
371371
random.shuffle(temp)
372-
examples.extend(sorted([temp[i] for i in range(min(number, len(temp)))]))
372+
examples.extend(sorted([temp[i] for i in range(min(number, len(temp)))], key=_human_keys))
373373
return examples
374374
codecs.examples = examples
375375

@@ -549,7 +549,7 @@ def search(encoding_regex):
549549
matches.append(encoding_regex)
550550
except:
551551
pass
552-
return sorted(list(set(matches)))
552+
return sorted(list(set(matches)), key=_human_keys)
553553
codecs.search = search
554554

555555

@@ -647,6 +647,14 @@ def __gen_str_from_re(regex, star_plus_max, repeat_max, yield_max, parsed=False)
647647
break
648648

649649

650+
def _human_keys(text):
651+
""" Sorting function for considering strings with numbers (e.g. base2, base10, base100) """
652+
tokens = []
653+
for s in re.split(r"(\d+|\D+)", text):
654+
tokens.append(int(s) if s.isdigit() else s)
655+
return tokens
656+
657+
650658
def generate_strings_from_regex(regex, star_plus_max=STAR_PLUS_MAX, repeat_max=REPEAT_MAX, yield_max=YIELD_MAX):
651659
""" Utility function to generate strings from a regex pattern. """
652660
i = 0

codext/__init__.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ def main():
3838
"\nThis tool allows to encode/decode input strings/files with an extended set of codecs.\n\n" \
3939
.format(__version__, __author__, __email__, __copyright__, __license__, __source__)
4040
examples = "usage examples:\n- " + "\n- ".join([
41+
"codext --search bitcoin",
4142
"codext -d base32 -i file.b32",
4243
"codext morse < to_be_encoded.txt",
4344
"echo \"test\" | codext base100",
@@ -56,7 +57,15 @@ def main():
5657
parser.add_argument("-i", "--input-file", dest="infile", help="input file (if none, take stdin as input)")
5758
parser.add_argument("-o", "--output-file", dest="outfile", help="output file (if none, display result to stdout)")
5859
parser.add_argument("-s", "--strip-newlines", action="store_true", dest="strip", help="strip newlines from input")
60+
parser.add_argument("--search", action="store_true", help="search for encoding names")
5961
args = parser.parse_args()
62+
# if a search pattern is given, only handle it
63+
if args.search:
64+
results = []
65+
for enc in args.encoding:
66+
results.extend(codecs.search(enc))
67+
print(", ".join(results) or "No encoding found")
68+
return
6069
# handle input file or stdin
6170
if args.infile:
6271
with open(args.infile, 'rb') as f:

0 commit comments

Comments
 (0)