summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTim Graham <timograham@gmail.com>2014-09-08 19:31:21 -0400
committerTim Graham <timograham@gmail.com>2014-09-08 19:31:21 -0400
commit2508be35ca8bc5440b93d3152cd80ee5f8e159e3 (patch)
tree3e003cc85204e1cad299da2782edb5b4a66eace0
parent5cba975d26d10ff5d85e123fb2084671a009cc9b (diff)
Fixed #23399 -- Optimized django.utils.http.int_to_bas36()
Thanks liminspace for the patch and Keryn Knight for benchmarking.
-rw-r--r--django/utils/http.py24
1 files changed, 8 insertions, 16 deletions
diff --git a/django/utils/http.py b/django/utils/http.py
index aa6d56d34a..c8411e5207 100644
--- a/django/utils/http.py
+++ b/django/utils/http.py
@@ -189,8 +189,7 @@ def int_to_base36(i):
"""
Converts an integer to a base36 string
"""
- digits = "0123456789abcdefghijklmnopqrstuvwxyz"
- factor = 0
+ char_set = '0123456789abcdefghijklmnopqrstuvwxyz'
if i < 0:
raise ValueError("Negative base36 conversion input.")
if six.PY2:
@@ -198,20 +197,13 @@ def int_to_base36(i):
raise TypeError("Non-integer base36 conversion input.")
if i > sys.maxint:
raise ValueError("Base36 conversion input too large.")
- # Find starting factor
- while True:
- factor += 1
- if i < 36 ** factor:
- factor -= 1
- break
- base36 = []
- # Construct base36 representation
- while factor >= 0:
- j = 36 ** factor
- base36.append(digits[i // j])
- i = i % j
- factor -= 1
- return ''.join(base36)
+ if i < 36:
+ return char_set[i]
+ b36 = ''
+ while i != 0:
+ i, n = divmod(i, 36)
+ b36 = char_set[n] + b36
+ return b36
def urlsafe_base64_encode(s):