forked from suminb/base62
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixed an issue where it fails to encode zero (0)
- Loading branch information
Showing
2 changed files
with
9 additions
and
3 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 |
---|---|---|
|
@@ -5,22 +5,25 @@ | |
|
||
__author__ = 'Sumin Byeon' | ||
__email__ = '[email protected]' | ||
__version__ = '0.1.2' | ||
__version__ = '0.1.3' | ||
|
||
CHARSET = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz' | ||
BASE = 62 | ||
|
||
def encode(n): | ||
"""Encodes a given integer ``n``.""" | ||
|
||
s = [] | ||
while n > 0: | ||
r = n % BASE | ||
n //= BASE | ||
|
||
s.append(CHARSET[r]) | ||
|
||
s.reverse() | ||
if len(s) > 0: | ||
s.reverse() | ||
else: | ||
s.append('0') | ||
|
||
return ''.join(s) | ||
|
||
|
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 |
---|---|---|
@@ -1,5 +1,8 @@ | ||
import base62 | ||
|
||
def test_basic(): | ||
assert base62.encode(0) == '0' | ||
assert base62.decode('0') == 0 | ||
|
||
assert base62.encode(34441886726) == 'base62' | ||
assert base62.decode('base62') == 34441886726 |