-
-
Notifications
You must be signed in to change notification settings - Fork 13
/
regions.go
48 lines (38 loc) · 841 Bytes
/
regions.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
package prettyprint
type RegionKind uint8
const (
ParamNameTypeRegion RegionKind = iota + 1
)
type Region struct {
Kind RegionKind
Depth uint8
Start uint16
End uint16
}
func (r Region) SubString(s string) string {
return s[r.Start:r.End]
}
type Regions []Region
type RegionFilter struct {
ExactDepth int
MinimumDepth int //ignored if ExactDepth is set
Kind RegionKind //ignored if zero
}
func (rs Regions) FilteredForEach(filter RegionFilter, fn func(r Region) error) error {
for _, r := range rs {
if filter.Kind != 0 && r.Kind != filter.Kind {
continue
}
if filter.ExactDepth >= 0 && int(r.Depth) != filter.ExactDepth {
continue
}
if filter.MinimumDepth >= 0 && int(r.Depth) < filter.MinimumDepth {
continue
}
err := fn(r)
if err != nil {
return err
}
}
return nil
}