Bug 893976 - Use (cached) regular expressions for mozpack.path.match. r=gps

This commit is contained in:
Mike Hommey
2013-07-30 08:57:27 +09:00
parent 268328ea31
commit 92820ad3ba
4 changed files with 15 additions and 23 deletions

View File

@@ -77,6 +77,8 @@ def basedir(path, bases):
return b
re_cache = {}
def match(path, pattern):
'''
Return whether the given path matches the given pattern.
@@ -94,27 +96,13 @@ def match(path, pattern):
'''
if not pattern:
return True
if isinstance(path, basestring):
path = split(path)
if isinstance(pattern, basestring):
pattern = split(pattern)
if pattern[0] == '**':
if len(pattern) == 1:
return True
return any(match(path[n:], pattern[1:]) for n in xrange(len(path)))
if len(pattern) > 1:
return match(path[:1], pattern[:1]) and match(path[1:], pattern[1:])
if path:
return re.match(translate(pattern[0]), path[0]) is not None
return False
def translate(pattern):
'''
Translate the globbing pattern to a regular expression.
'''
return re.escape(pattern).replace('\*', '.*') + '$'
if not pattern in re_cache:
pattern = re.escape(pattern)
pattern = re.sub(r'(^|\\\/)\\\*\\\*\\\/', r'\1(?:.+/)?', pattern)
pattern = re.sub(r'(^|\\\/)\\\*\\\*$', r'(?:\1.+)?', pattern)
pattern = pattern.replace(r'\*', '[^/]*') + '(?:/.*)?$'
re_cache[pattern] = re.compile(pattern)
return re_cache[pattern].match(path) is not None
def rebase(oldbase, base, relativepath):