Skip to content

Commit 7a1e319

Browse files
committed
Extract regexes and compile in global context to improve performance
1 parent d1ded03 commit 7a1e319

File tree

1 file changed

+29
-28
lines changed

1 file changed

+29
-28
lines changed

giturlparse/parser.py

Lines changed: 29 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,31 @@
3535
'owner',
3636
])
3737

38+
POSSIBLE_REGEXES = (
39+
re.compile(r'^(?P<protocol>https?|git|ssh|rsync)\://'
40+
r'(?:(?P<user>.+)@)*'
41+
r'(?P<resource>[a-z0-9_.-]*)'
42+
r'[:/]*'
43+
r'(?P<port>[\d]+){0,1}'
44+
r'(?P<pathname>\/(?P<owner>.+)/(?P<name>.+).git)'),
45+
re.compile(r'(git\+)?'
46+
r'((?P<protocol>\w+)://)'
47+
r'((?P<user>\w+)@)?'
48+
r'((?P<resource>[\w\.\-]+))'
49+
r'(:(?P<port>\d+))?'
50+
r'(?P<pathname>(\/(?P<owner>\w+)/)?'
51+
r'(\/?(?P<name>[\w\-]+)(\.git)?)?)'),
52+
re.compile(r'^(?:(?P<user>.+)@)*'
53+
r'(?P<resource>[a-z0-9_.-]*)[:/]*'
54+
r'(?P<port>[\d]+){0,1}'
55+
r'[:](?P<pathname>\/?(?P<owner>.+)/(?P<name>.+).git)'),
56+
re.compile(r'((?P<user>\w+)@)?'
57+
r'((?P<resource>[\w\.\-]+))'
58+
r'[\:\/]{1,2}'
59+
r'(?P<pathname>((?P<owner>\w+)/)?'
60+
r'((?P<name>[\w\-]+)(\.git)?)?)'),
61+
)
62+
3863

3964
class ParserError(Exception):
4065
""" Error raised when a URL can't be parsed. """
@@ -68,34 +93,10 @@ def parse(self):
6893
'name': None,
6994
'owner': None,
7095
}
71-
regexes = [
72-
(r'^(?P<protocol>https?|git|ssh|rsync)\://'
73-
r'(?:(?P<user>.+)@)*'
74-
r'(?P<resource>[a-z0-9_.-]*)'
75-
r'[:/]*'
76-
r'(?P<port>[\d]+){0,1}'
77-
r'(?P<pathname>\/(?P<owner>.+)/(?P<name>.+).git)'),
78-
(r'(git\+)?'
79-
r'((?P<protocol>\w+)://)'
80-
r'((?P<user>\w+)@)?'
81-
r'((?P<resource>[\w\.\-]+))'
82-
r'(:(?P<port>\d+))?'
83-
r'(?P<pathname>(\/(?P<owner>\w+)/)?'
84-
r'(\/?(?P<name>[\w\-]+)(\.git)?)?)'),
85-
(r'^(?:(?P<user>.+)@)*'
86-
r'(?P<resource>[a-z0-9_.-]*)[:/]*'
87-
r'(?P<port>[\d]+){0,1}'
88-
r'[:](?P<pathname>\/?(?P<owner>.+)/(?P<name>.+).git)'),
89-
(r'((?P<user>\w+)@)?'
90-
r'((?P<resource>[\w\.\-]+))'
91-
r'[\:\/]{1,2}'
92-
r'(?P<pathname>((?P<owner>\w+)/)?'
93-
r'((?P<name>[\w\-]+)(\.git)?)?)'),
94-
]
95-
for regex in regexes:
96-
if re.search(regex, self._url):
97-
m = re.search(regex, self._url)
98-
d.update(m.groupdict())
96+
for regex in POSSIBLE_REGEXES:
97+
match = regex.search(self._url)
98+
if match:
99+
d.update(match.groupdict())
99100
break
100101
else:
101102
msg = "Invalid URL '{}'".format(self._url)

0 commit comments

Comments
 (0)