-
Notifications
You must be signed in to change notification settings - Fork 68
Expand file tree
/
Copy pathskipper.go
More file actions
73 lines (58 loc) · 1.72 KB
/
skipper.go
File metadata and controls
73 lines (58 loc) · 1.72 KB
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
package main
import (
"fmt"
"regexp"
)
// Technical note: this might be a bit confusing as met in code, so a quick explaination:
// a - is name actually in skip list
// b - is reverse mode active
//
// a b shouldSkip?
// true, true -> false
// true, false -> true
// false, true -> true
// false, false -> false
//
// So we use: a != b to check this cond.
func (c *Context) ShouldSkipFunc(f CIdentifier) bool {
return c.Skip.Funcs.ShouldSkip(f) != c.preset.ReverseMode
}
func (c *Context) ShouldSkipTypedef(t CIdentifier) bool {
return c.Skip.Typedefs.ShouldSkip(t) != c.preset.ReverseMode
}
func (c *Context) ShouldSkipStruct(t CIdentifier) bool {
return c.Skip.Structs.ShouldSkip(t) != c.preset.ReverseMode
}
func (c *Context) ShouldSkipMethod(t CIdentifier) bool {
return c.Skip.Methods.ShouldSkip(t) != c.preset.ReverseMode
}
func (c *Context) ShouldSkipFile(t string) bool {
return c.Skip.Files.ShouldSkip(t) != c.preset.ReverseMode
}
// Skipper is an advanced method of skipping functions/types basing on a set of regular expressions.
// Regular expressions must be compatible with the `regexp` package in Go.
type Skipper[T ~string] struct {
data []regexp.Regexp
}
func CreateSkipper[T ~string](data []T) (*Skipper[T], error) {
if data == nil {
return &Skipper[T]{}, nil
}
result := &Skipper[T]{data: make([]regexp.Regexp, len(data))}
for i, d := range data {
re, err := regexp.Compile(string(d))
if err != nil {
return nil, fmt.Errorf("Cannot compile regular expression #%d \"%s\": %w", i, d, err)
}
result.data[i] = *re
}
return result, nil
}
func (s *Skipper[T]) ShouldSkip(name T) bool {
for _, re := range s.data {
if re.MatchString(string(name)) {
return true
}
}
return false
}