This repository has been archived by the owner on Jan 28, 2021. It is now read-only.
/
regex.go
85 lines (66 loc) · 1.85 KB
/
regex.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
package regex
import errors "gopkg.in/src-d/go-errors.v1"
var (
// ErrRegexAlreadyRegistered is returned when there is a previously
// registered regex engine with the same name.
ErrRegexAlreadyRegistered = errors.NewKind("Regex engine already registered: %s")
// ErrRegexNameEmpty returned when the name is "".
ErrRegexNameEmpty = errors.NewKind("Regex engine name cannot be empty")
// ErrRegexNotFound returned when the regex engine is not registered.
ErrRegexNotFound = errors.NewKind("Regex engine not found: %s")
registry map[string]Constructor
defaultEngine string
)
// Matcher interface is used to compare regexes with strings.
type Matcher interface {
// Match returns true if the text matches the regular expression.
Match(text string) bool
}
// Constructor creates a new Matcher.
type Constructor func(re string) (Matcher, error)
// Register add a new regex engine to the registry.
func Register(name string, c Constructor) error {
if registry == nil {
registry = make(map[string]Constructor)
}
if name == "" {
return ErrRegexNameEmpty.New()
}
_, ok := registry[name]
if ok {
return ErrRegexAlreadyRegistered.New(name)
}
registry[name] = c
return nil
}
// Engines returns the list of regex engines names.
func Engines() []string {
var names []string
for n := range registry {
names = append(names, n)
}
return names
}
// New creates a new Matcher with the specified regex engine.
func New(name, re string) (Matcher, error) {
n, ok := registry[name]
if !ok {
return nil, ErrRegexNotFound.New(name)
}
return n(re)
}
// Default returns the default regex engine.
func Default() string {
if defaultEngine != "" {
return defaultEngine
}
_, ok := registry["oniguruma"]
if ok {
return "oniguruma"
}
return "go"
}
// SetDefault sets the regex engine returned by Default.
func SetDefault(name string) {
defaultEngine = name
}