From 8db024c9f97ec941fa224e48a4cf2299d2203ed9 Mon Sep 17 00:00:00 2001 From: Nicholas Yip Date: Fri, 2 Feb 2024 11:12:49 +0900 Subject: [PATCH 1/2] Add `test_basic_qwerty` --- tests/test_basic.py | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/tests/test_basic.py b/tests/test_basic.py index bade00f..c261ae9 100644 --- a/tests/test_basic.py +++ b/tests/test_basic.py @@ -3,6 +3,8 @@ import base62 +CHARSET_QWERTY = "1234567890QWERTYUIOPASDFGHJKLZXCVBNMqwertyuiopasdfghjklzxcvbnm" + bytes_int_pairs = [ (b"\x01", 1), (b"\x01\x01", 0x0101), @@ -39,6 +41,18 @@ def test_basic_inverted(): assert base62.decode("base62", **kwargs) == 10231951886 +def test_basic_qwerty(): + kwargs = {"charset": CHARSET_QWERTY} + + assert base62.encode(0, **kwargs) == "1" + assert base62.decode("1", **kwargs) == 0 + assert base62.decode("1111", **kwargs) == 0 + assert base62.decode("111112", **kwargs) == 1 + + assert base62.encode(54742896343, **kwargs) == "base62" + assert base62.decode("base62", **kwargs) == 54742896343 + + @pytest.mark.parametrize("b, i", bytes_int_pairs) def test_bytes_to_int(b, i): assert int.from_bytes(b, "big") == i From 906aa739f231839035c00d48972884ad1a66f8ae Mon Sep 17 00:00:00 2001 From: Nicholas Yip Date: Fri, 2 Feb 2024 11:29:07 +0900 Subject: [PATCH 2/2] Fix hardcoded zeros for `encode(0, ...)` and padding --- base62.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/base62.py b/base62.py index e4426f2..6a8428a 100644 --- a/base62.py +++ b/base62.py @@ -25,7 +25,7 @@ def encode(n, charset=CHARSET_DEFAULT): chs.insert(0, charset[r]) if not chs: - return "0" + return charset[0] return "".join(chs) @@ -50,9 +50,9 @@ def encodebytes(barray, charset=CHARSET_DEFAULT): # Encode the leading zeros as "0" followed by a character indicating the count. # This pattern may occur several times if there are many leading zeros. n, r = divmod(leading_zeros_count, len(charset) - 1) - zero_padding = f"0{charset[-1]}" * n + zero_padding = f"{charset[0]}{charset[-1]}" * n if r: - zero_padding += f"0{charset[r]}" + zero_padding += f"{charset[0]}{charset[r]}" # Special case: the input is empty, or is entirely null bytes. if leading_zeros_count == len(barray): @@ -87,7 +87,7 @@ def decodebytes(encoded, charset=CHARSET_DEFAULT): """ leading_null_bytes = b"" - while encoded.startswith("0") and len(encoded) >= 2: + while encoded.startswith(charset[0]) and len(encoded) >= 2: leading_null_bytes += b"\x00" * _value(encoded[1], charset) encoded = encoded[2:]