github.com/grahambrereton-form3/tilt@v0.10.18/internal/sliceutils/sliceutils.go (about) 1 package sliceutils 2 3 import ( 4 "fmt" 5 "sort" 6 "strings" 7 ) 8 9 // Deduplicate and sort a slice of strings. 10 func DedupedAndSorted(slice []string) (result []string) { 11 seen := map[string]bool{} 12 13 for _, s := range slice { 14 if !seen[s] { 15 seen[s] = true 16 result = append(result, s) 17 } 18 } 19 sort.Strings(result) 20 return result 21 } 22 23 // Quote each string in the list and separate them by commas. 24 func QuotedStringList(list []string) string { 25 result := make([]string, len(list)) 26 for i, s := range list { 27 result[i] = fmt.Sprintf("%q", s) 28 } 29 return strings.Join(result, ", ") 30 } 31 32 func BulletedIndentedStringList(list []string) string { 33 if len(list) == 0 { 34 return "" 35 } 36 37 result := make([]string, len(list)) 38 for i, s := range list { 39 result[i] = s 40 } 41 return "\t- " + strings.Join(result, "\n\t- ") 42 } 43 44 func StringSliceEquals(a, b []string) bool { 45 if len(a) != len(b) { 46 return false 47 } 48 49 for i, e1 := range a { 50 e2 := b[i] 51 if e1 != e2 { 52 return false 53 } 54 } 55 56 return true 57 } 58 59 // StringSliceStartsWith returns true if slice A starts with the given elem. 60 func StringSliceStartsWith(a []string, elem string) bool { 61 if len(a) == 0 { 62 return false 63 } 64 65 return a[0] == elem 66 } 67 68 // returns a slice that consists of `a`, in order, followed by elements of `b` that are not in `a` 69 func AppendWithoutDupes(a []string, b ...string) []string { 70 seen := make(map[string]bool) 71 for _, s := range a { 72 seen[s] = true 73 } 74 75 ret := append([]string{}, a...) 76 for _, s := range b { 77 if !seen[s] { 78 ret = append(ret, s) 79 } 80 } 81 82 return ret 83 }