Revert "add utils.parse.shortencode() and utils.parse.shortdecode - effectively base62"

This reverts commit e71f3bbc36.
This commit is contained in:
jesopo 2019-12-06 14:29:26 +00:00
parent e71f3bbc36
commit b212714561

View file

@ -1,4 +1,4 @@
import decimal, io, random, typing import decimal, io, typing
from . import datetime, errors from . import datetime, errors
COMMENT_TYPES = ["#", "//"] COMMENT_TYPES = ["#", "//"]
@ -120,47 +120,3 @@ def timed_args(args, min_args):
return time, args[1:] return time, args[1:]
return None, args return None, args
SHORTENCODE_CHARS = list(
"abcdefghijklmnopqrstuvwxyz"
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
"0123456789")
SHORTENCODE_SALT = "bitbot"
def _shortencode_salt(salt: str):
chars = SHORTENCODE_CHARS.copy()
random.Random(salt).shuffle(chars)
return chars
def _shortencode_tobase(n: int, base: int) -> typing.List[int]:
r = []
while n:
n, remainder = divmod(n, base)
r.append(remainder)
return r
def _shortencode_frombase(ints: typing.List[int], base: int) -> int:
n = 0
for i in ints:
n = (n*base)+i
return n
def shortencode_b(bytes: bytes, salt: str=SHORTENCODE_SALT):
chars = _shortencode_salt(salt)
ints = list(bytes)
n = _shortencode_frombase(ints, 256)
r = _shortencode_tobase(n, len(chars))
return "".join(chars[i] for i in reversed(r))
def shortencode(s: str, salt: str=SHORTENCODE_SALT):
return shortencode_b(s.encode("latin-1"), salt)
def shortdecode_b(s: str, salt: str=SHORTENCODE_SALT):
chars = _shortencode_salt(salt)
ints = [chars.index(c) for c in s]
n = _shortencode_frombase(ints, len(chars))
r = _shortencode_tobase(n, 256)
return bytes(reversed(r))
def shortdecode(s: str, salt: str=SHORTENCODE_SALT):
return shortdecode_b(s, salt).decode("latin-1")