mirror of
https://github.com/ralsina/tartrazine.git
synced 2025-06-27 14:47:50 -03:00
Squashed 'go-enry/' content from commit 7e3a9a7
git-subtree-dir: go-enry
git-subtree-split: 7e3a9a7241
This commit is contained in:
29
regex/oniguruma.go
Normal file
29
regex/oniguruma.go
Normal file
@ -0,0 +1,29 @@
|
||||
//go:build oniguruma
|
||||
// +build oniguruma
|
||||
|
||||
package regex
|
||||
|
||||
import (
|
||||
rubex "github.com/go-enry/go-oniguruma"
|
||||
)
|
||||
|
||||
const Name = Oniguruma
|
||||
|
||||
type EnryRegexp = *rubex.Regexp
|
||||
|
||||
func MustCompile(s string) EnryRegexp {
|
||||
return rubex.MustCompileASCII(s)
|
||||
}
|
||||
|
||||
// MustCompileMultiline matches in multi-line mode by default with Oniguruma.
|
||||
func MustCompileMultiline(s string) EnryRegexp {
|
||||
return MustCompile(s)
|
||||
}
|
||||
|
||||
func MustCompileRuby(s string) EnryRegexp {
|
||||
return MustCompile(s)
|
||||
}
|
||||
|
||||
func QuoteMeta(s string) string {
|
||||
return rubex.QuoteMeta(s)
|
||||
}
|
9
regex/regex.go
Normal file
9
regex/regex.go
Normal 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"
|
||||
)
|
38
regex/standard.go
Normal file
38
regex/standard.go
Normal file
@ -0,0 +1,38 @@
|
||||
//go:build !oniguruma
|
||||
// +build !oniguruma
|
||||
|
||||
package regex
|
||||
|
||||
import (
|
||||
"regexp"
|
||||
)
|
||||
|
||||
const Name = RE2
|
||||
|
||||
type EnryRegexp = *regexp.Regexp
|
||||
|
||||
func MustCompile(str string) EnryRegexp {
|
||||
return regexp.MustCompile(str)
|
||||
}
|
||||
|
||||
// MustCompileMultiline mimics Ruby defaults for regexp, where ^$ matches begin/end of line.
|
||||
// I.e. it converts Ruby regexp syntaxt to RE2 equivalent
|
||||
func MustCompileMultiline(s string) EnryRegexp {
|
||||
const multilineModeFlag = "(?m)"
|
||||
return regexp.MustCompile(multilineModeFlag + s)
|
||||
}
|
||||
|
||||
// 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 {
|
||||
return nil
|
||||
}
|
||||
|
||||
func QuoteMeta(s string) string {
|
||||
return regexp.QuoteMeta(s)
|
||||
}
|
27
regex/standard_test.go
Normal file
27
regex/standard_test.go
Normal 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(``))
|
||||
}
|
Reference in New Issue
Block a user