Skip to content

Commit

Permalink
Honor regex flags for case-sensitivity (#767)
Browse files Browse the repository at this point in the history
* Honor regex flags for case-sensitivity

* change

* add test

* pr comment
  • Loading branch information
mmanela authored Apr 29, 2024
1 parent 5411e9b commit 570757e
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 18 deletions.
14 changes: 14 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "Attach to Process (from list)",
"type": "go",
"request": "attach",
"mode": "local"
},
]
}
3 changes: 2 additions & 1 deletion eval.go
Original file line number Diff line number Diff line change
Expand Up @@ -641,7 +641,8 @@ func (d *indexData) regexpToMatchTreeRecursive(r *syntax.Regexp, minTextSize int
case syntax.OpLiteral:
s := string(r.Rune)
if len(s) >= minTextSize {
mt, err := d.newSubstringMatchTree(&query.Substring{Pattern: s, FileName: fileName, CaseSensitive: caseSensitive})
ignoreCase := syntax.FoldCase == (r.Flags & syntax.FoldCase)
mt, err := d.newSubstringMatchTree(&query.Substring{Pattern: s, FileName: fileName, CaseSensitive: !ignoreCase && caseSensitive})
return mt, true, !strings.Contains(s, "\n"), err
}
case syntax.OpCapture:
Expand Down
49 changes: 32 additions & 17 deletions eval_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,57 +59,71 @@ func printRegexp(t *testing.T, r *syntax.Regexp, lvl int) {
}
}

func caseSensitiveSubstrMT(pattern string) matchTree {
d := &indexData{}
mt, _ := d.newSubstringMatchTree(&query.Substring{
Pattern: pattern,
CaseSensitive: true,
})
return mt
}

func substrMT(pattern string) matchTree {
d := &indexData{}
mt, _ := d.newSubstringMatchTree(&query.Substring{
Pattern: pattern,
Pattern: pattern,
CaseSensitive: false,
})
return mt
}

func TestRegexpParse(t *testing.T) {
type testcase struct {
in string
query matchTree
isEquivalent bool
in string
query matchTree
isEquivalent bool
caseSensitive bool
}

cases := []testcase{
{"(foo|)bar", substrMT("bar"), false},
{"(foo|)", &bruteForceMatchTree{}, false},
{"(foo|)bar", substrMT("bar"), false, false},
{"(foo|)", &bruteForceMatchTree{}, false, false},
{"(foo|bar)baz.*bla", &andMatchTree{[]matchTree{
&orMatchTree{[]matchTree{
substrMT("foo"),
substrMT("bar"),
}},
substrMT("baz"),
substrMT("bla"),
}}, false},
}}, false, false},
{
"^[a-z](People)+barrabas$",
&andMatchTree{[]matchTree{
substrMT("People"),
substrMT("barrabas"),
}}, false,
}}, false, false,
},
{"foo", substrMT("foo"), true},
{"^foo", substrMT("foo"), false},
{"(foo) (bar)", &andMatchTree{[]matchTree{substrMT("foo"), substrMT("bar")}}, false},
{"foo", substrMT("foo"), true, false},
{"foo", caseSensitiveSubstrMT("foo"), true, true},
{"(?i)foo", substrMT("FOO"), true, false},
{"(?i)foo", substrMT("FOO"), true, true},
{"^foo", substrMT("foo"), false, false},
{"(foo) (bar)", &andMatchTree{[]matchTree{substrMT("foo"), substrMT("bar")}}, false, false},
{"(thread|needle|haystack)", &orMatchTree{[]matchTree{
substrMT("thread"),
substrMT("needle"),
substrMT("haystack"),
}}, true},
}}, true, false},
{"(foo)(?-s:.)*?(bar)", &andLineMatchTree{andMatchTree{[]matchTree{
substrMT("foo"),
substrMT("bar"),
}}}, false},
}}}, false, false},
{"(foo)(?-s:.)*?[[:space:]](?-s:.)*?(bar)", &andMatchTree{[]matchTree{
substrMT("foo"),
substrMT("bar"),
}}, false},
{"(foo){2,}", substrMT("foo"), false},
{"(...)(...)", &bruteForceMatchTree{}, false},
}}, false, false},
{"(foo){2,}", substrMT("foo"), false, false},
{"(...)(...)", &bruteForceMatchTree{}, false, false},
}

for _, c := range cases {
Expand All @@ -120,7 +134,8 @@ func TestRegexpParse(t *testing.T) {
}
d := indexData{}
q := query.Regexp{
Regexp: r,
Regexp: r,
CaseSensitive: c.caseSensitive,
}
gotQuery, isEq, _, _ := d.regexpToMatchTreeRecursive(q.Regexp, 3, q.FileName, q.CaseSensitive)
if !reflect.DeepEqual(c.query, gotQuery) {
Expand Down

0 comments on commit 570757e

Please sign in to comment.