diff options
author | Yen Chi Hsuan <yan12125@gmail.com> | 2016-02-27 03:19:50 +0800 |
---|---|---|
committer | Yen Chi Hsuan <yan12125@gmail.com> | 2016-02-27 03:22:52 +0800 |
commit | 5eb6bdced4765cdeb70411c6aa93ecb4163a9ffe (patch) | |
tree | 48d5ee30bd44b7d2fe637cea2fbb90dabda3c4f6 /youtube_dl | |
parent | 5633b4d39d178402c6d89146c8c9c34e3bf58619 (diff) |
[utils] Multiple changes to base_n()
1. Renamed to encode_base_n()
2. Allow tables longer than 62 characters
3. Raise ValueError instead of AssertionError for invalid input data
4. Return the first character in the table instead of '0' for number 0
5. Add tests
Diffstat (limited to 'youtube_dl')
-rw-r--r-- | youtube_dl/utils.py | 14 |
1 files changed, 8 insertions, 6 deletions
diff --git a/youtube_dl/utils.py b/youtube_dl/utils.py index 756ad4fd1..606977c58 100644 --- a/youtube_dl/utils.py +++ b/youtube_dl/utils.py @@ -2621,15 +2621,17 @@ def ohdave_rsa_encrypt(data, exponent, modulus): return '%x' % encrypted -def base_n(num, n, table=None): - if num == 0: - return '0' - +def encode_base_n(num, n, table=None): FULL_TABLE = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ' - assert n <= len(FULL_TABLE) if not table: table = FULL_TABLE[:n] + if n > len(table): + raise ValueError('base %d exceeds table length %d' % (n, len(table))) + + if num == 0: + return table[0] + ret = '' while num: ret = table[num % n] + ret @@ -2649,7 +2651,7 @@ def decode_packed_codes(code): while count: count -= 1 - base_n_count = base_n(count, base) + base_n_count = encode_base_n(count, base) symbol_table[base_n_count] = symbols[count] or base_n_count return re.sub( |