Code

Ticket #6965: faster_urlize_r7936_t6965.diff

File faster_urlize_r7936_t6965.diff, 1.3 KB (added by andrewbadr, 6 years ago)

Makes urlize faster

Line 
1Index: django/utils/html.py
2===================================================================
3--- django/utils/html.py        (revision 7936)
4+++ django/utils/html.py        (working copy)
5@@ -94,7 +94,9 @@
6     words = word_split_re.split(force_unicode(text))
7     nofollow_attr = nofollow and ' rel="nofollow"' or ''
8     for i, word in enumerate(words):
9-        match = punctuation_re.match(word)
10+        match = None
11+        if '.' in word or '@' in word or ':' in word:
12+            match = punctuation_re.match(word)
13         if match:
14             lead, middle, trail = match.groups()
15             # Make URL we want to point to.
16@@ -102,7 +104,7 @@
17             if middle.startswith('http://') or middle.startswith('https://'):
18                 url = urlquote(middle, safe='/&=:;#?+*')
19             elif middle.startswith('www.') or ('@' not in middle and \
20-                    len(middle) > 0 and middle[0] in string.ascii_letters + string.digits and \
21+                    middle and middle[0] in string.ascii_letters + string.digits and \
22                     (middle.endswith('.org') or middle.endswith('.net') or middle.endswith('.com'))):
23                 url = urlquote('http://%s' % middle, safe='/&=:;#?+*')
24             elif '@' in middle and not ':' in middle and simple_email_re.match(middle):