From c8bce5d8c5fb8e8721fa2f653e8a0be607208d05 Mon Sep 17 00:00:00 2001 From: Oleg Zaytsev Date: Tue, 17 May 2022 17:49:19 +0200 Subject: [PATCH] When cleaning up the only element, make a nop. We can't remove the only Sub from regexp, since the contract for some operations says that there's at least one Sub, like OpStar or OpPlus. In order to convert a single-sub element into a no-op, we change the operation to OpEmptyString. Signed-off-by: Oleg Zaytsev --- model/labels/matcher_test.go | 10 ++++++++++ model/labels/regexp.go | 3 +++ 2 files changed, 13 insertions(+) diff --git a/model/labels/matcher_test.go b/model/labels/matcher_test.go index eb69e0b23..6a95358f7 100644 --- a/model/labels/matcher_test.go +++ b/model/labels/matcher_test.go @@ -86,6 +86,16 @@ func TestMatcher(t *testing.T) { value: "foo-bar", match: false, }, + { + matcher: mustNewMatcher(t, MatchRegexp, "bar^+"), + value: "foo-bar", + match: false, + }, + { + matcher: mustNewMatcher(t, MatchRegexp, "$+bar"), + value: "foo-bar", + match: false, + }, } for _, test := range tests { diff --git a/model/labels/regexp.go b/model/labels/regexp.go index 6406b5d9b..5c9d033ba 100644 --- a/model/labels/regexp.go +++ b/model/labels/regexp.go @@ -163,6 +163,9 @@ func clearBeginEndText(re *syntax.Regexp) { } if len(re.Sub) == 1 { if re.Sub[0].Op == syntax.OpBeginText || re.Sub[0].Op == syntax.OpEndText { + // We need to remove this element. Since it's the only one, we convert into a matcher of an empty string. + // OpEmptyMatch is regexp's nop operator. + re.Op = syntax.OpEmptyMatch re.Sub = nil return }