forked from Python-Markdown/markdown
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Optimize several regexes from quadratic time to linear time
Part of the discussion in Python-Markdown#798. Signed-off-by: Anders Kaseorg <[email protected]>
- Loading branch information
Showing
2 changed files
with
6 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -147,10 +147,10 @@ def build_inlinepatterns(md, **kwargs): | |
NOT_STRONG_RE = r'((^|\s)(\*|_)(\s|$))' | ||
|
||
# <http://www.123.com> | ||
AUTOLINK_RE = r'<((?:[Ff]|[Hh][Tt])[Tt][Pp][Ss]?://[^>]*)>' | ||
AUTOLINK_RE = r'<((?:[Ff]|[Hh][Tt])[Tt][Pp][Ss]?://[^<>]*)>' | ||
|
||
# <[email protected]> | ||
AUTOMAIL_RE = r'<([^> \!]*@[^> ]*)>' | ||
AUTOMAIL_RE = r'<([^<> !]*@[^@<> ]*)>' | ||
|
||
# <...> | ||
HTML_RE = r'(\<([a-zA-Z/][^\>]*?|\!--.*?--)\>)' | ||
|
@@ -433,7 +433,7 @@ def get_stash(m): | |
|
||
class LinkInlineProcessor(InlineProcessor): | ||
""" Return a link element from the given match. """ | ||
RE_LINK = re.compile(r'''\(\s*(?:(<.*?>)\s*(?:(['"])(.*?)\2\s*)?\))?''', re.DOTALL | re.UNICODE) | ||
RE_LINK = re.compile(r'''\(\s*(?:(<[^<>]*>)\s*(?:('[^']*'|"[^"]*")\s*)?\))?''', re.DOTALL | re.UNICODE) | ||
RE_TITLE_CLEAN = re.compile(r'\s') | ||
|
||
def handleMatch(self, m, data): | ||
|
@@ -467,8 +467,8 @@ def getLink(self, data, index): | |
if m and m.group(1): | ||
# Matches [Text](<link> "title") | ||
href = m.group(1)[1:-1].strip() | ||
if m.group(3): | ||
title = m.group(3) | ||
if m.group(2): | ||
title = m.group(2)[1:-1] | ||
index = m.end(0) | ||
handled = True | ||
elif m: | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters