This repository was archived by the owner on Oct 29, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinterval.go
91 lines (81 loc) · 1.88 KB
/
interval.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
86
87
88
89
90
91
package datautils
import "math"
import "regexp"
import "strconv"
import "strings"
var reRanges = regexp.MustCompile(`` +
`^([0-9]*|[0-9]*:[0-9]*|[0-9]*:[0-9]*:[0-9]*)` +
`(,([0-9]*|[0-9]*:[0-9]*|[0-9]*:[0-9]*:[0-9]*))*$`)
var reRangeS = regexp.MustCompile(`^(\d*)$`)
var reRangeM = regexp.MustCompile(`^(\d*):(\d*)$`)
var reRangeL = regexp.MustCompile(`^(\d*):(\d*):(\d*)$`)
// Interval TODO
type Interval struct {
Low int64
High int64
Step int64
}
// NewInterval TODO
func NewInterval(low, high, step string) Interval {
if low == "" {
low = "1"
}
if high == "" {
high = strconv.FormatInt(math.MaxInt64, 10)
}
if step == "" {
step = "1"
}
l, errL := strconv.ParseInt(low, 10, 64)
h, errH := strconv.ParseInt(high, 10, 64)
s, errS := strconv.ParseInt(step, 10, 64)
if (errL != nil) || (errH != nil) || (errS != nil) {
panic("Parameters could not be parsed!")
}
return Interval{
Low: l,
High: h,
Step: s,
}
}
// Contains TODO
func (r *Interval) Contains(i int64) bool {
return (i >= r.Low) && (i <= r.High) && (((i - r.Low) % r.Step) == 0)
}
// String2Intervals TODO
func String2Intervals(s string) []Interval {
if reRanges.MatchString(s) {
result := []Interval{}
for _, r := range strings.Split(s, ",") {
low := ""
high := ""
step := ""
if m := reRangeS.FindStringSubmatch(r); len(m) != 0 {
low = m[1]
high = m[1]
} else if m := reRangeM.FindStringSubmatch(r); len(m) != 0 {
low = m[1]
high = m[2]
} else if m := reRangeL.FindStringSubmatch(r); len(m) != 0 {
low = m[1]
high = m[2]
step = m[3]
} else {
return nil
}
result = append(result, NewInterval(low, high, step))
}
return result
}
return nil
}
// Intervals2Func TODO
func Intervals2Func(rs []Interval) func(int64) bool {
return func(i int64) bool {
result := false
for _, r := range rs {
result = result || r.Contains(i)
}
return result
}
}