From 2508be35ca8bc5440b93d3152cd80ee5f8e159e3 Mon Sep 17 00:00:00 2001 From: Tim Graham Date: Mon, 8 Sep 2014 19:31:21 -0400 Subject: [PATCH] Fixed #23399 -- Optimized django.utils.http.int_to_bas36() Thanks liminspace for the patch and Keryn Knight for benchmarking. --- django/utils/http.py | 24 ++++++++---------------- 1 file 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):