github.com/metasources/buildx@v0.0.0-20230418141019-7aa1459cedea/internal/stringset.go (about) 1 package internal 2 3 import "sort" 4 5 // StringSet represents a set of string types. 6 type StringSet map[string]struct{} 7 8 // NewStringSet creates a new empty StringSet. 9 func NewStringSet(start ...string) StringSet { 10 ret := make(StringSet) 11 for _, s := range start { 12 ret.Add(s) 13 } 14 return ret 15 } 16 17 // Add a string to the set. 18 func (s StringSet) Add(i string) { 19 s[i] = struct{}{} 20 } 21 22 // Remove a string from the set. 23 func (s StringSet) Remove(i string) { 24 delete(s, i) 25 } 26 27 // Contains indicates if the given string is contained within the set. 28 func (s StringSet) Contains(i string) bool { 29 _, ok := s[i] 30 return ok 31 } 32 33 // ToSlice returns a sorted slice of strings that are contained within the set. 34 func (s StringSet) ToSlice() []string { 35 ret := make([]string, len(s)) 36 idx := 0 37 for v := range s { 38 ret[idx] = v 39 idx++ 40 } 41 sort.Strings(ret) 42 return ret 43 }