-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathstringset.go
51 lines (44 loc) · 1010 Bytes
/
stringset.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
package main
import (
"sort"
"strings"
)
// A stringSet is a set of strings that implements the flag.Value interface.
type stringSet map[string]struct{}
func newStringSet(elements ...string) stringSet {
s := make(stringSet, len(elements))
for _, element := range elements {
s[element] = struct{}{}
}
return s
}
func (s stringSet) Set(value string) error {
for element := range s {
delete(s, element)
}
for _, element := range strings.Split(value, ",") {
s[element] = struct{}{}
}
return nil
}
func (s stringSet) String() string {
elements := make([]string, 0, len(s))
for element := range s {
elements = append(elements, element)
}
sort.Strings(elements)
return strings.Join(elements, ",")
}
func (s stringSet) contains(element string) bool {
_, ok := s[element]
return ok
}
func (s stringSet) subtract(other stringSet) stringSet {
result := make(stringSet)
for element := range s {
if _, ok := other[element]; !ok {
result[element] = struct{}{}
}
}
return result
}