github.com/ouraigua/jenkins-library@v0.0.0-20231028010029-fbeaf2f3aa9b/pkg/piperutils/slices.go (about) 1 package piperutils 2 3 import ( 4 "reflect" 5 "strings" 6 ) 7 8 // ContainsInt checks whether the element is part of the slice 9 func ContainsInt(s []int, e int) bool { 10 for _, a := range s { 11 if a == e { 12 return true 13 } 14 } 15 return false 16 } 17 18 // ContainsString checks whether the element is part of the slice 19 func ContainsString(s []string, e string) bool { 20 return FindString(s, e) >= 0 21 } 22 23 // FindString returns the position of element e in the given slice or -1 if it's not in 24 func FindString(s []string, e string) int { 25 for i, a := range s { 26 if a == e { 27 return i 28 } 29 } 30 31 return -1 32 } 33 34 // ContainsStringPart checks whether the element is contained as part of one of the elements of the slice 35 func ContainsStringPart(s []string, part string) bool { 36 for _, a := range s { 37 if strings.Contains(a, part) { 38 return true 39 } 40 } 41 return false 42 } 43 44 // RemoveAll removes all instances of element from the slice and returns a truncated slice as well as 45 // a boolean to indicate whether at least one element was found and removed. 46 func RemoveAll(s []string, e string) ([]string, bool) { 47 var r []string 48 for _, a := range s { 49 if a != e { 50 r = append(r, a) 51 } 52 } 53 return r, len(s) != len(r) 54 } 55 56 // Prefix adds a prefix to each element of the slice 57 func Prefix(in []string, prefix string) []string { 58 return _prefix(in, prefix, true) 59 } 60 61 // PrefixIfNeeded adds a prefix to each element of the slice if not already prefixed 62 func PrefixIfNeeded(in []string, prefix string) []string { 63 return _prefix(in, prefix, false) 64 } 65 66 func _prefix(in []string, prefix string, always bool) (out []string) { 67 for _, element := range in { 68 if always || !strings.HasPrefix(element, prefix) { 69 element = prefix + element 70 } 71 out = append(out, element) 72 } 73 return 74 } 75 76 // Trim removes dangling whitespaces from each element of the slice, empty elements are dropped 77 func Trim(in []string) (out []string) { 78 for _, element := range in { 79 if trimmed := strings.TrimSpace(element); len(trimmed) > 0 { 80 out = append(out, trimmed) 81 } 82 } 83 return 84 } 85 86 // SplitAndTrim iterates over the strings in the given slice and splits each on the provided separator. 87 // Each resulting sub-string is then a separate entry in the returned array. 88 func SplitAndTrim(in []string, separator string) (out []string) { 89 if len(in) == 0 { 90 return in 91 } 92 for _, entry := range in { 93 entryParts := strings.Split(entry, separator) 94 for _, part := range entryParts { 95 part = strings.TrimSpace(part) 96 if part != "" { 97 out = append(out, part) 98 } 99 } 100 } 101 return 102 } 103 104 // UniqueStrings removes duplicates from values 105 func UniqueStrings(values []string) []string { 106 107 u := map[string]bool{} 108 for _, e := range values { 109 u[e] = true 110 } 111 keys := make([]string, len(u)) 112 i := 0 113 for k := range u { 114 keys[i] = k 115 i++ 116 } 117 return keys 118 } 119 120 // CopyAtoB copies the contents of a into slice b given that they are of equal size and compatible type 121 func CopyAtoB(a, b interface{}) { 122 src := reflect.ValueOf(a) 123 tgt := reflect.ValueOf(b) 124 if src.Kind() != reflect.Slice || tgt.Kind() != reflect.Slice { 125 panic("CopyAtoB() given a non-slice type") 126 } 127 128 if src.Len() != tgt.Len() { 129 panic("CopyAtoB() given non equal sized slices") 130 } 131 132 // Keep the distinction between nil and empty slice input 133 if src.IsNil() { 134 return 135 } 136 137 for i := 0; i < src.Len(); i++ { 138 tgt.Index(i).Set(src.Index(i)) 139 } 140 }