-
Notifications
You must be signed in to change notification settings - Fork 7
/
string.go
162 lines (144 loc) · 4.03 KB
/
string.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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
// Copyright 2015 by Leipzig University Library, http://ub.uni-leipzig.de
// The Finc Authors, http://finc.info
// Martin Czygan, <martin.czygan@uni-leipzig.de>
//
// This file is part of some open source application.
//
// Some open source application is free software: you can redistribute
// it and/or modify it under the terms of the GNU General Public
// License as published by the Free Software Foundation, either
// version 3 of the License, or (at your option) any later version.
//
// Some open source application is distributed in the hope that it will
// be useful, but WITHOUT ANY WARRANTY; without even the implied warranty
// of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Foobar. If not, see <http://www.gnu.org/licenses/>.
//
// @license GPL-3.0+ <http://spdx.org/licenses/GPL-3.0+>
package container
import (
"bufio"
"fmt"
"io"
"sort"
"strings"
)
// MapDefault provides defaults for string map lookups with defaults.
type MapDefault map[string]string
// Lookup map with a default value.
func (m MapDefault) Lookup(key, def string) string {
val, ok := m[key]
if !ok {
return def
}
return val
}
// MapSliceDefault provides defaults for string map lookups.
type MapSliceDefault map[string][]string
// Lookup with default value.
func (m MapSliceDefault) Lookup(key string, def []string) []string {
val, ok := m[key]
if !ok {
return def
}
return val
}
// StringSet is map disguised as set.
type StringSet struct {
Set map[string]struct{}
}
// NewStringSet returns an empty string set. XXX: Make the zero value usable.
func NewStringSet(s ...string) *StringSet {
ss := &StringSet{Set: make(map[string]struct{})}
for _, item := range s {
ss.Add(item)
}
return ss
}
// NewStringSetReader reads from reader linewise, each line corresponding to
// one item in set.
func NewStringSetReader(r io.Reader) (*StringSet, error) {
var (
ss = &StringSet{Set: make(map[string]struct{})}
br = bufio.NewReader(r)
)
for {
line, err := br.ReadString('\n')
if err == io.EOF {
break
}
if err != nil {
return nil, err
}
line = strings.TrimSpace(line)
ss.Add(line)
}
return ss, nil
}
// Add adds a string to a set, returns true if added, false it it already existed (noop).
func (set *StringSet) Add(ss ...string) {
for _, s := range ss {
set.Set[s] = struct{}{}
}
}
// Contains returns true if given string is in the set, false otherwise.
func (set *StringSet) Contains(s string) bool {
_, found := set.Set[s]
return found
}
// Size returns current number of elements in the set.
func (set *StringSet) Size() int {
return len(set.Set)
}
// Values returns the set values as a string slice.
func (set *StringSet) Values() (values []string) {
for k := range set.Set {
values = append(values, k)
}
return values
}
// Values returns the set values as a string slice.
func (set *StringSet) SortedValues() (values []string) {
for k := range set.Set {
values = append(values, k)
}
sort.Strings(values)
return values
}
// Intersection returns the intersection of two string sets.
func (set *StringSet) Intersection(other *StringSet) *StringSet {
isect := NewStringSet()
for k := range set.Set {
if other.Contains(k) {
isect.Add(k)
}
}
return isect
}
// Difference returns all items, that are in set but not in other.
func (set *StringSet) Difference(other *StringSet) *StringSet {
diff := NewStringSet()
for k := range set.Set {
if !other.Contains(k) {
diff.Add(k)
}
}
return diff
}
// Define a type named "StringSlice" as a slice of strings.
// Useful for repeated command line flags.
type StringSlice []string
// Now, for our new type, implement the two methods of
// the flag.Value interface...
// The first method is String() string
func (i *StringSlice) String() string {
return fmt.Sprintf("%s", *i)
}
// The second method is Set(value string) error
func (i *StringSlice) Set(value string) error {
*i = append(*i, value)
return nil
}