-
Notifications
You must be signed in to change notification settings - Fork 87
/
Copy pathrules.go
83 lines (64 loc) · 1.34 KB
/
rules.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
package rules
import (
"regexp"
"time"
)
type Strategy int
const (
Skip Strategy = iota
Merge
Override
)
type Rule interface {
Find(string) *Match
}
type Options struct {
Afternoon, Evening, Morning, Noon int
Distance int
MatchByOrder bool
// TODO
// WeekStartsOn time.Weekday
}
type Match struct {
Left, Right int
Text string
Captures []string
Order float64
Applier func(*Match, *Context, *Options, time.Time) (bool, error)
}
func (m Match) String() string { return m.Text }
func (m *Match) Apply(c *Context, o *Options, t time.Time) (bool, error) {
return m.Applier(m, c, o, t)
}
type F struct {
RegExp *regexp.Regexp
Applier func(*Match, *Context, *Options, time.Time) (bool, error)
}
func (f *F) Find(text string) *Match {
m := &Match{
Applier: f.Applier,
Left: -1,
}
indexes := f.RegExp.FindStringSubmatchIndex(text)
length := len(indexes)
if length <= 2 {
return nil
}
for i := 2; i < length; i += 2 {
if m.Left == -1 && indexes[i] >= 0 {
m.Left = indexes[i]
}
// check if capture was found
if indexes[i] >= 0 && indexes[i+1] >= 0 {
m.Captures = append(m.Captures, text[indexes[i]:indexes[i+1]])
m.Right = indexes[i+1]
} else {
m.Captures = append(m.Captures, "")
}
}
if len(m.Captures) == 0 || m.Left == -1 {
return nil
}
m.Text = text[m.Left:m.Right]
return m
}