Patchwork [4,of,7] stringutil: extract helper function that splits stringmatcher() pattern

login
register
mail settings
Submitter Yuya Nishihara
Date Oct. 14, 2020, 2:13 p.m.
Message ID <b6c30689b9fe9e5f53bc.1602684814@mimosa>
Download mbox | patch
Permalink /patch/47459/
State Accepted
Headers show

Comments

Yuya Nishihara - Oct. 14, 2020, 2:13 p.m.
# HG changeset patch
# User Yuya Nishihara <yuya@tcha.org>
# Date 1601898814 -32400
#      Mon Oct 05 20:53:34 2020 +0900
# Node ID b6c30689b9fe9e5f53bceb7913c71ae98109c804
# Parent  5b3013b8e40a903e7d31196c1e00877120339242
stringutil: extract helper function that splits stringmatcher() pattern

Patch

diff --git a/mercurial/utils/stringutil.py b/mercurial/utils/stringutil.py
--- a/mercurial/utils/stringutil.py
+++ b/mercurial/utils/stringutil.py
@@ -307,6 +307,14 @@  def binary(s):
     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 @@  def stringmatcher(pattern, casesensitive
     >>> 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 @@  def stringmatcher(pattern, casesensitive
             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):