-
Notifications
You must be signed in to change notification settings - Fork 3
/
yarex.go
54 lines (46 loc) · 1008 Bytes
/
yarex.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
package yarex
type execer interface {
exec(str string, pos int, onSuccess func(MatchContext)) bool
}
type Regexp struct {
str string
exe execer
}
func Compile(ptn string) (*Regexp, error) {
if r, ok := compiledRegexps[ptn]; ok {
return r, nil
}
ast, err := parse(ptn)
if err != nil {
return nil, err
}
ast = optimizeAst(ast)
op := opCompile(ast)
return &Regexp{ptn, opExecer{op}}, nil
}
func MustCompile(ptn string) *Regexp {
r, err := Compile(ptn)
if err != nil {
panic(err)
}
return r
}
func (re Regexp) String() string {
return re.str
}
func (re Regexp) MatchString(s string) bool {
return re.exe.exec(s, 0, func(_ MatchContext) {})
}
func (re Regexp) FindString(s string) string {
matched := ""
re.exe.exec(s, 0, func(c MatchContext) {
matched, _ = c.GetCaptured(ContextKey{'c', 0})
})
return matched
}
func (re Regexp) FindStringIndex(s string) (loc []int) {
re.exe.exec(s, 0, func(c MatchContext) {
loc = c.GetCapturedIndex(ContextKey{'c', 0})
})
return loc
}