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 <mail@olegzaytsev.com>
This commit is contained in:
Oleg Zaytsev 2022-05-17 17:49:19 +02:00
parent 8120398f6c
commit c8bce5d8c5
No known key found for this signature in database
GPG key ID: 7E9FE9FD48F512EF
2 changed files with 13 additions and 0 deletions

View file

@ -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 {

View file

@ -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
}