lib/storage: ignore start text and end text anchors in getOrValues(regexp) function

This is OK, since the anchors are implicitly applied to the whole regexp.
This optimization should improve the speed for regexp series filters with explicit $ and ^ anchors.
For example, `{label="^(foo|bar)$"}`
This commit is contained in:
Aliaksandr Valialkin 2022-08-24 17:10:30 +03:00
parent cdffe401e4
commit fdbf5b5795
No known key found for this signature in database
GPG key ID: A72BEC6CD3D0DED1
2 changed files with 9 additions and 0 deletions

View file

@ -811,6 +811,8 @@ func getOrValuesExt(sre *syntax.Regexp) []string {
return []string{string(sre.Rune)}
case syntax.OpEmptyMatch:
return []string{""}
case syntax.OpBeginText, syntax.OpEndText:
return []string{""}
case syntax.OpAlternate:
a := make([]string, 0, len(sre.Sub))
for _, reSub := range sre.Sub {

View file

@ -1176,6 +1176,13 @@ func TestGetOrValues(t *testing.T) {
f("(a|b|c)(d|e|f)(g|h|k)", nil)
f("(?i)foo", nil)
f("(?i)(foo|bar)", nil)
f("^foo|bar$", []string{"bar", "foo"})
f("^(foo|bar)$", []string{"bar", "foo"})
f("^a(foo|b(?:a|r))$", []string{"aba", "abr", "afoo"})
// This is incorrect conversion, because the regexp matches nothing.
// It is OK for now, since such regexps are uncommon in practice.
// TODO: properly handle this case.
f("^a(^foo|bar$)z$", []string{"abarz", "afooz"})
}
func TestGetRegexpPrefix(t *testing.T) {