heuristics regexp engine configurable #2, skip rules at runtime

This commit is contained in:
Alex Bezzubov
2023-01-19 15:58:43 +01:00
parent d8913b00e9
commit 3aeb9879da
8 changed files with 141 additions and 38 deletions

View File

@ -7,6 +7,8 @@ import (
rubex "github.com/go-enry/go-oniguruma"
)
const Name = Oniguruma
type EnryRegexp = *rubex.Regexp
func MustCompile(s string) EnryRegexp {

9
regex/regex.go Normal file
View File

@ -0,0 +1,9 @@
package regex
// Package regex abstracts regular expression engine
// that can be chosen at compile-time by a build tag.
const (
RE2 = "RE2"
Oniguruma = "Oniguruma"
)

View File

@ -7,6 +7,8 @@ import (
"regexp"
)
const Name = RE2
type EnryRegexp = *regexp.Regexp
func MustCompile(str string) EnryRegexp {
@ -21,9 +23,13 @@ func MustCompileMultiline(s string) EnryRegexp {
}
// MustCompileRuby used for expressions with syntax not supported by RE2.
// Now it's confusing as we use the result as [data/rule.Matcher] and
//
// (*Matcher)(nil) != nil
//
// What is a better way for an expression to indicate unsupported syntax?
// e.g. add .IsValidSyntax() to both, Matcher interface and EnryRegexp implementations?
func MustCompileRuby(s string) EnryRegexp {
// TODO(bzz): find a bettee way?
// This will only trigger a panic on .Match() for the clients
return nil
}

27
regex/standard_test.go Normal file
View File

@ -0,0 +1,27 @@
//go:build !oniguruma
// +build !oniguruma
package regex
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestMustCompileMultiline(t *testing.T) {
const re = `^\.(.*)!$`
want := MustCompileMultiline(re)
assert.Equal(t, "(?m)"+re, want.String())
const s = `.one
.two!
thre!`
if !want.MatchString(s) {
t.Fatalf("MustCompileMultiline(`%s`) must match multiline %q\n", re, s)
}
}
func TestMustCompileRuby(t *testing.T) {
assert.Nil(t, MustCompileRuby(``))
}