vomebook commited on
Commit ·
23a4455
1
Parent(s): 65b11be
Accelerate boundary wildcard search
Browse files
backend/search_matching.py
CHANGED
|
@@ -7,6 +7,12 @@ def compile_exact_matcher(query: str) -> Callable[[str], bool]:
|
|
| 7 |
if "*" not in query and "?" not in query:
|
| 8 |
lower = query.lower()
|
| 9 |
return lambda text: lower in text.lower()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
# Bitset NFA: each bit is a pattern position. Collapse adjacent stars so
|
| 11 |
# epsilon closure needs one shift. No input can cause regex backtracking.
|
| 12 |
pattern = re.sub(r"\*+", "*", query)
|
|
|
|
| 7 |
if "*" not in query and "?" not in query:
|
| 8 |
lower = query.lower()
|
| 9 |
return lambda text: lower in text.lower()
|
| 10 |
+
# In substring matching, stars at either boundary can match zero chars.
|
| 11 |
+
# Keep Unicode IGNORECASE behavior, unlike the literal .lower() branch.
|
| 12 |
+
core = query.strip("*")
|
| 13 |
+
if core and "*" not in core and "?" not in core:
|
| 14 |
+
literal = re.compile(re.escape(core), re.IGNORECASE)
|
| 15 |
+
return lambda text: literal.search(text) is not None
|
| 16 |
# Bitset NFA: each bit is a pattern position. Collapse adjacent stars so
|
| 17 |
# epsilon closure needs one shift. No input can cause regex backtracking.
|
| 18 |
pattern = re.sub(r"\*+", "*", query)
|
mutation-equivalent-mutation-gate.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
| 1 |
{
|
| 2 |
"tool": "mutmut 3.8.1",
|
| 3 |
"source": "scripts/check_search_words_mutation.py",
|
| 4 |
-
"source_sha256": "
|
| 5 |
"scope": "Only ordinary built-in dicts with built-in string keys and integer values, without concurrent mutation, dict subclasses, tracing, introspection, or monkeypatching. Comparison is the return value and exceptions of mutation_errors. The initial required-field validation is executed unchanged. These survivors remain survivors, not killed.",
|
| 6 |
"equivalent": {
|
| 7 |
"scripts.check_search_words_mutation.x_mutation_errors__mutmut_22": {
|
|
|
|
| 1 |
{
|
| 2 |
"tool": "mutmut 3.8.1",
|
| 3 |
"source": "scripts/check_search_words_mutation.py",
|
| 4 |
+
"source_sha256": "a7ad103f8bbaa411e9ed45930058304e5cb91cf5780a6d956289a2eceb4b8db6",
|
| 5 |
"scope": "Only ordinary built-in dicts with built-in string keys and integer values, without concurrent mutation, dict subclasses, tracing, introspection, or monkeypatching. Comparison is the return value and exceptions of mutation_errors. The initial required-field validation is executed unchanged. These survivors remain survivors, not killed.",
|
| 6 |
"equivalent": {
|
| 7 |
"scripts.check_search_words_mutation.x_mutation_errors__mutmut_22": {
|
mutation-equivalent-search-matching.json
CHANGED
|
@@ -1,10 +1,10 @@
|
|
| 1 |
{
|
| 2 |
"tool": "mutmut 3.8.1",
|
| 3 |
"source": "backend/search_matching.py",
|
| 4 |
-
"source_sha256": "
|
| 5 |
"equivalent": {
|
| 6 |
-
"backend.search_matching.
|
| 7 |
-
"sha256": "
|
| 8 |
"rationale": "When wildcard is false the changed conditional value is ORed with 1 immediately afterwards; 0 | 1 and 1 | 1 both equal 1. When wildcard is true the changed branch is not evaluated."
|
| 9 |
},
|
| 10 |
"backend.search_matching.x_is_fuzzy_match__mutmut_7": {
|
|
|
|
| 1 |
{
|
| 2 |
"tool": "mutmut 3.8.1",
|
| 3 |
"source": "backend/search_matching.py",
|
| 4 |
+
"source_sha256": "528ddf36278ba56e31f4a35a8cf2c4c02a9086e234ad8e8cb82e677f9fafb0f6",
|
| 5 |
"equivalent": {
|
| 6 |
+
"backend.search_matching.x_compile_exact_matcher__mutmut_109": {
|
| 7 |
+
"sha256": "82cd836e2e189d603a8681880a15585cd5e01a078ab496a7cbc9aa446afc252d",
|
| 8 |
"rationale": "When wildcard is false the changed conditional value is ORed with 1 immediately afterwards; 0 | 1 and 1 | 1 both equal 1. When wildcard is true the changed branch is not evaluated."
|
| 9 |
},
|
| 10 |
"backend.search_matching.x_is_fuzzy_match__mutmut_7": {
|
scripts/check_search_words_mutation.py
CHANGED
|
@@ -28,6 +28,8 @@ MATCHING_TESTS = [
|
|
| 28 |
"test_matching_helpers_keep_wildcard_results_after_large_character_cache",
|
| 29 |
"test_fuzzy_precheck_and_distance_are_bounded",
|
| 30 |
"test_exact_and_wildcard_matchers_keep_unicode_and_newline_semantics",
|
|
|
|
|
|
|
| 31 |
"test_wildcards_agree_with_independent_regular_expression_reference",
|
| 32 |
"test_fuzzy_helpers_match_independent_distance_and_multiset_references",
|
| 33 |
"test_wildcard_cache_keeps_repeated_characters_and_clears_at_limit",
|
|
|
|
| 28 |
"test_matching_helpers_keep_wildcard_results_after_large_character_cache",
|
| 29 |
"test_fuzzy_precheck_and_distance_are_bounded",
|
| 30 |
"test_exact_and_wildcard_matchers_keep_unicode_and_newline_semantics",
|
| 31 |
+
"test_boundary_stars_preserve_case_newlines_and_literal_metacharacters",
|
| 32 |
+
"test_boundary_stars_use_one_literal_scan_and_complex_patterns_keep_nfa",
|
| 33 |
"test_wildcards_agree_with_independent_regular_expression_reference",
|
| 34 |
"test_fuzzy_helpers_match_independent_distance_and_multiset_references",
|
| 35 |
"test_wildcard_cache_keeps_repeated_characters_and_clears_at_limit",
|
tests/test_search_words_contract.py
CHANGED
|
@@ -150,6 +150,48 @@ class SearchWordsContractTests(unittest.TestCase):
|
|
| 150 |
self.assertFalse(compile_exact_matcher("a*c")("a\nc"))
|
| 151 |
self.assertTrue(compile_exact_matcher("İ*")("i-page"))
|
| 152 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 153 |
def test_wildcards_agree_with_independent_regular_expression_reference(self):
|
| 154 |
import itertools
|
| 155 |
import re
|
|
|
|
| 150 |
self.assertFalse(compile_exact_matcher("a*c")("a\nc"))
|
| 151 |
self.assertTrue(compile_exact_matcher("İ*")("i-page"))
|
| 152 |
|
| 153 |
+
def test_boundary_stars_preserve_case_newlines_and_literal_metacharacters(self):
|
| 154 |
+
from backend.search_matching import compile_exact_matcher
|
| 155 |
+
for pattern in ('a*', '*a', '*a*', '**a***'):
|
| 156 |
+
with self.subTest(pattern=pattern):
|
| 157 |
+
self.assertTrue(compile_exact_matcher(pattern)('x\nA\ny'))
|
| 158 |
+
self.assertFalse(compile_exact_matcher(pattern)('xyz'))
|
| 159 |
+
for pattern in ('*', '**', '*****'):
|
| 160 |
+
self.assertTrue(compile_exact_matcher(pattern)(''))
|
| 161 |
+
self.assertTrue(compile_exact_matcher(pattern)('\n'))
|
| 162 |
+
for char in ('İ', 'ı', 'ſ', 'K'):
|
| 163 |
+
self.assertTrue(compile_exact_matcher('*' + char + '*')({'İ':'i', 'ı':'I', 'ſ':'s', 'K':'k'}[char]))
|
| 164 |
+
self.assertTrue(compile_exact_matcher('*[.\\]*')('x[.\\]y'))
|
| 165 |
+
self.assertFalse(compile_exact_matcher('*[.\\]*')('xyz'))
|
| 166 |
+
self.assertFalse(compile_exact_matcher('*ab*')('a\nb'))
|
| 167 |
+
self.assertFalse(compile_exact_matcher('a*b')('a\nb'))
|
| 168 |
+
self.assertTrue(compile_exact_matcher('a?b')('aXb'))
|
| 169 |
+
self.assertTrue(compile_exact_matcher('*X*')('X'))
|
| 170 |
+
self.assertFalse(compile_exact_matcher('*X*')(''))
|
| 171 |
+
# Escaped literal scans cannot introduce wildcard regex backtracking.
|
| 172 |
+
self.assertFalse(compile_exact_matcher('*' + 'a' * 127 + 'b*')('a' * 20000))
|
| 173 |
+
|
| 174 |
+
def test_boundary_stars_use_one_literal_scan_and_complex_patterns_keep_nfa(self):
|
| 175 |
+
from backend import search_matching
|
| 176 |
+
original = search_matching.re.compile
|
| 177 |
+
calls = []
|
| 178 |
+
def compile_counted(expression, flags):
|
| 179 |
+
calls.append((expression, flags))
|
| 180 |
+
return original(expression, flags)
|
| 181 |
+
with patch.object(search_matching.re, 'compile', side_effect=compile_counted):
|
| 182 |
+
matcher = search_matching.compile_exact_matcher('**a.b**')
|
| 183 |
+
self.assertEqual(calls, [(r'a\.b', search_matching.re.IGNORECASE)])
|
| 184 |
+
self.assertTrue(matcher('A.B'))
|
| 185 |
+
calls.clear()
|
| 186 |
+
search_matching.compile_exact_matcher('*X*')
|
| 187 |
+
self.assertEqual(calls, [('X', search_matching.re.IGNORECASE)])
|
| 188 |
+
calls.clear()
|
| 189 |
+
search_matching.compile_exact_matcher('*Xa*')
|
| 190 |
+
self.assertEqual(calls, [('Xa', search_matching.re.IGNORECASE)])
|
| 191 |
+
calls.clear()
|
| 192 |
+
search_matching.compile_exact_matcher('a*b')
|
| 193 |
+
self.assertEqual(len(calls), 2)
|
| 194 |
+
|
| 195 |
def test_wildcards_agree_with_independent_regular_expression_reference(self):
|
| 196 |
import itertools
|
| 197 |
import re
|