diff --git a/mercurial/util.py b/mercurial/util.py --- a/mercurial/util.py +++ b/mercurial/util.py @@ -1996,7 +1996,7 @@ def matchdate(date): start, stop = lower(date), upper(date) return lambda x: x >= start and x <= stop -def stringmatcher(pattern): +def stringmatcher(pattern, casesensitive=True): """ accepts a string, possibly starting with 're:' or 'literal:' prefix. returns the matcher name, pattern, and matcher function. @@ -2006,6 +2006,9 @@ def stringmatcher(pattern): >>> def test(pattern, *tests): ... kind, pattern, matcher = stringmatcher(pattern) ... return (kind, pattern, [bool(matcher(t)) for t in tests]) + >>> def itest(pattern, *tests): + ... kind, pattern, matcher = stringmatcher(pattern, casesensitive=False) + ... return (kind, pattern, [bool(matcher(t)) for t in tests]) exact matching (no prefix): >>> test('abcdefg', 'abc', 'def', 'abcdefg') @@ -2022,18 +2025,35 @@ def stringmatcher(pattern): unknown prefixes are ignored and treated as literals >>> test('foo:bar', 'foo', 'bar', 'foo:bar') ('literal', 'foo:bar', [False, False, True]) + + case insensitive regex matches + >>> itest('re:A.+b', 'nomatch', 'fooadef', 'fooadefBar') + ('re', 'A.+b', [False, False, True]) + + case insensitive literal matches + >>> itest('ABCDEFG', 'abc', 'def', 'abcdefg') + ('literal', 'ABCDEFG', [False, False, True]) """ if pattern.startswith('re:'): pattern = pattern[3:] try: - regex = remod.compile(pattern) + flags = 0 + if not casesensitive: + flags = remod.I + regex = remod.compile(pattern, flags) except remod.error as e: raise error.ParseError(_('invalid regular expression: %s') % e) return 're', pattern, regex.search elif pattern.startswith('literal:'): pattern = pattern[8:] - return 'literal', pattern, pattern.__eq__ + + match = pattern.__eq__ + + if not casesensitive: + ipat = encoding.lower(pattern) + match = lambda s: ipat == encoding.lower(s) + return 'literal', pattern, match def shortuser(user): """Return a short representation of a user name or email address."""