github.com/lineaje-labs/syft@v0.98.1-0.20231227153149-9e393f60ff1b/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 for _, str := range i { 20 s[str] = struct{}{} 21 } 22 } 23 24 // Remove a string from the set. 25 func (s StringSet) Remove(i string) { 26 delete(s, i) 27 } 28 29 // Contains indicates if the given string is contained within the set. 30 func (s StringSet) Contains(i string) bool { 31 _, ok := s[i] 32 return ok 33 } 34 35 // ToSlice returns a sorted slice of strings that are contained within the set. 36 func (s StringSet) ToSlice() []string { 37 ret := make([]string, len(s)) 38 idx := 0 39 for v := range s { 40 ret[idx] = v 41 idx++ 42 } 43 sort.Strings(ret) 44 return ret 45 } 46 47 func (s StringSet) Equals(o StringSet) bool { 48 if len(s) != len(o) { 49 return false 50 } 51 for k := range s { 52 if !o.Contains(k) { 53 return false 54 } 55 } 56 return true 57 } 58 59 func (s StringSet) Empty() bool { 60 return len(s) < 1 61 }