stringutil: extract helper function that splits stringmatcher() pattern
authorYuya Nishihara <yuya@tcha.org>
Mon, 05 Oct 2020 20:53:34 +0900
changeset 45722 d502caab76bc
parent 45721 f9d3ff23bfc0
child 45723 edfc5820aae7
stringutil: extract helper function that splits stringmatcher() pattern
mercurial/utils/stringutil.py
--- a/mercurial/utils/stringutil.py	Wed Sep 09 17:17:38 2020 +0900
+++ b/mercurial/utils/stringutil.py	Mon Oct 05 20:53:34 2020 +0900
@@ -307,6 +307,14 @@
     return bool(s and b'\0' in s)
 
 
+def _splitpattern(pattern):
+    if pattern.startswith(b're:'):
+        return b're', pattern[3:]
+    elif pattern.startswith(b'literal:'):
+        return b'literal', pattern[8:]
+    return b'literal', pattern
+
+
 def stringmatcher(pattern, casesensitive=True):
     """
     accepts a string, possibly starting with 're:' or 'literal:' prefix.
@@ -345,8 +353,8 @@
     >>> itest(b'ABCDEFG', b'abc', b'def', b'abcdefg')
     ('literal', 'ABCDEFG', [False, False, True])
     """
-    if pattern.startswith(b're:'):
-        pattern = pattern[3:]
+    kind, pattern = _splitpattern(pattern)
+    if kind == b're':
         try:
             flags = 0
             if not casesensitive:
@@ -354,16 +362,16 @@
             regex = remod.compile(pattern, flags)
         except remod.error as e:
             raise error.ParseError(_(b'invalid regular expression: %s') % e)
-        return b're', pattern, regex.search
-    elif pattern.startswith(b'literal:'):
-        pattern = pattern[8:]
+        return kind, pattern, regex.search
+    elif kind == b'literal':
+        if casesensitive:
+            match = pattern.__eq__
+        else:
+            ipat = encoding.lower(pattern)
+            match = lambda s: ipat == encoding.lower(s)
+        return kind, pattern, match
 
-    match = pattern.__eq__
-
-    if not casesensitive:
-        ipat = encoding.lower(pattern)
-        match = lambda s: ipat == encoding.lower(s)
-    return b'literal', pattern, match
+    raise error.ProgrammingError(b'unhandled pattern kind: %s' % kind)
 
 
 def shortuser(user):