forked from reeflective/readline
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmessage.go
107 lines (88 loc) · 1.95 KB
/
message.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
package readline
import (
"regexp"
"sort"
"strings"
)
type messages struct {
messages map[string]bool
}
func (m *messages) init() {
if m.messages == nil {
m.messages = make(map[string]bool)
}
}
func (m messages) IsEmpty() bool {
// TODO replacement for Action.skipCache - does this need to consider suppressed messages or is this fine?
return len(m.messages) == 0
}
func (m *messages) Add(s string) {
m.init()
m.messages[s] = true
}
func (m messages) Get() []string {
messages := make([]string, 0)
for message := range m.messages {
messages = append(messages, message)
}
sort.Strings(messages)
return messages
}
func (m *messages) Suppress(expr ...string) error {
m.init()
for _, e := range expr {
char, err := regexp.Compile(e)
if err != nil {
return err
}
for key := range m.messages {
if char.MatchString(key) {
delete(m.messages, key)
}
}
}
return nil
}
func (m *messages) Merge(other messages) {
if other.messages == nil {
return
}
for key := range other.messages {
m.Add(key)
}
}
type suffixMatcher struct {
string
pos int // Used to know if the saved suffix matcher is deprecated
}
func (sm *suffixMatcher) Add(suffixes ...rune) {
if strings.Contains(sm.string, "*") || strings.Contains(string(suffixes), "*") {
sm.string = "*"
return
}
unique := []rune(sm.string)
for _, r := range suffixes {
if !strings.Contains(sm.string, string(r)) {
unique = append(unique, r)
}
}
sort.Sort(byRune(unique))
sm.string = string(unique)
}
func (sm *suffixMatcher) Merge(other suffixMatcher) {
for _, r := range other.string {
sm.Add(r)
}
}
func (sm suffixMatcher) Matches(s string) bool {
for _, r := range sm.string {
if r == '*' || strings.HasSuffix(s, string(r)) {
return true
}
}
return false
}
type byRune []rune
func (r byRune) Len() int { return len(r) }
func (r byRune) Swap(i, j int) { r[i], r[j] = r[j], r[i] }
func (r byRune) Less(i, j int) bool { return r[i] < r[j] }