mirror of
https://github.com/tiennm99/serena.git
synced 2026-10-13 14:16:21 +00:00
Fix glob pattern escaping
This commit is contained in:
1 parent
3b04d0b867
commit
e19b0b6d34
2 files changed
+21
-13
No files matched your search
@@ -153,19 +153,9 @@ def search_text(
|
||||
|
||||
# Convert pattern to a compiled regex if it's a string
|
||||
if is_glob and isinstance(pattern, str):
|
||||
# Convert glob pattern to regex
|
||||
# Escape all regex special characters except * and ?
|
||||
regex_special_chars = r"\^$.|+()[{"
|
||||
escaped_pattern = ""
|
||||
for char in pattern:
|
||||
if char in regex_special_chars:
|
||||
escaped_pattern += "\\" + char
|
||||
elif char == "*":
|
||||
escaped_pattern += ".*"
|
||||
elif char == "?":
|
||||
escaped_pattern += "."
|
||||
else:
|
||||
escaped_pattern += char
|
||||
# Convert glob pattern to regex. Use re.escape to handle special
|
||||
# characters and replace glob wildcards afterwards.
|
||||
escaped_pattern = "".join(".*" if ch == "*" else "." if ch == "?" else re.escape(ch) for ch in pattern)
|
||||
# For glob patterns, don't anchor with ^ and $ to allow partial line matches
|
||||
compiled_pattern = re.compile(escaped_pattern)
|
||||
elif isinstance(pattern, str):
|
||||
|
||||
@@ -182,6 +182,24 @@ class TestSearchText:
|
||||
assert any("isinstance(item, dict)" in line for line in instance_matches)
|
||||
assert any("isinstance(item, list)" in line for line in instance_matches)
|
||||
|
||||
def test_search_text_glob_with_special_chars(self):
|
||||
"""Glob patterns containing regex special characters should match literally."""
|
||||
content = """
|
||||
def func_square():
|
||||
print("value[42]")
|
||||
|
||||
def func_curly():
|
||||
print("value{bar}")
|
||||
"""
|
||||
|
||||
matches_square = search_text(r"*\[42\]*", content=content, is_glob=True)
|
||||
assert len(matches_square) == 1
|
||||
assert "[42]" in matches_square[0].lines[0].line_content
|
||||
|
||||
matches_curly = search_text("*{bar}*", content=content, is_glob=True)
|
||||
assert len(matches_curly) == 1
|
||||
assert "{bar}" in matches_curly[0].lines[0].line_content
|
||||
|
||||
def test_search_text_no_matches(self):
|
||||
"""Test searching with a pattern that doesn't match anything."""
|
||||
content = """
|
||||
|
||||
Reference in new issue
Block a user