github.com/searKing/golang/go@v1.2.117/strings/string_slice.go (about) 1 // Copyright 2020 The searKing Author. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package strings 6 7 import ( 8 "strings" 9 ) 10 11 // SliceCombine combine elements to a new slice. 12 func SliceCombine(ss ...[]string) []string { 13 var total int 14 for _, s := range ss { 15 total += len(s) 16 } 17 if total == 0 { 18 return nil 19 } 20 var tt = make([]string, 0, total) 21 for _, s := range ss { 22 tt = append(tt, s...) 23 } 24 return tt 25 } 26 27 // SliceEqualFold reports whether s and t, interpreted as UTF-8 strings, 28 // are equal under Unicode case-folding, which is a more general 29 // form of case-sensitivity. 30 func SliceEqual(s, t []string) bool { 31 if len(s) != len(t) { 32 return false 33 } 34 for i := 0; i < len(s); i++ { 35 if s[i] != t[i] { 36 return false 37 } 38 } 39 return true 40 } 41 42 // SliceEqualFold reports whether s and t, interpreted as UTF-8 strings, 43 // are equal under Unicode case-folding, which is a more general 44 // form of case-insensitivity. 45 func SliceEqualFold(s, t []string) bool { 46 if len(s) != len(t) { 47 return false 48 } 49 for i := 0; i < len(s); i++ { 50 if !strings.EqualFold(s[i], t[i]) { 51 return false 52 } 53 } 54 return true 55 } 56 57 // SliceTrimEmpty trim empty columns 58 func SliceTrimEmpty(ss ...string) []string { 59 return SliceTrimFunc(ss, func(s string) bool { 60 return s == "" 61 }) 62 } 63 64 // SliceTrim returns a slice of the string ss with tt removed. 65 func SliceTrim(ss []string, tt ...string) []string { 66 return SliceTrimFunc(ss, func(s string) bool { 67 return SliceContains(tt, s) 68 }) 69 } 70 71 // SliceTrimFunc returns a slice of the string ss satisfying f(c) removed. 72 func SliceTrimFunc(ss []string, f func(s string) bool) []string { 73 var trimmed []string 74 for _, s := range ss { 75 if f(s) { 76 continue 77 } 78 trimmed = append(trimmed, s) 79 } 80 return trimmed 81 } 82 83 // SliceContainsAny reports whether any t in tt is within ss. 84 func SliceContainsAny(ss []string, tt ...string) bool { 85 return sliceContains(false, true, ss, tt...) 86 } 87 88 // SliceContains reports whether all t in tt is within ss. 89 func SliceContains(ss []string, tt ...string) bool { 90 return sliceContains(true, false, ss, tt...) 91 } 92 93 func sliceContains(containsBefore bool, any bool, ss []string, tt ...string) bool { 94 var containsFirst bool 95 if len(tt) == 0 { 96 containsFirst = true 97 } else { 98 for _, v := range ss { 99 if v == tt[0] { 100 containsFirst = true 101 break 102 } 103 } 104 } 105 106 // concat before and first string in ss 107 if any { 108 containsBefore = containsBefore || containsFirst 109 } else { // all 110 containsBefore = containsBefore && containsFirst 111 } 112 113 if len(tt) <= 1 { 114 return containsBefore 115 } 116 117 return sliceContains(containsBefore, any, ss, tt[1:]...) 118 } 119 120 // SliceUnique returns the given string slice with unique values. 121 func SliceUnique(s ...string) []string { 122 if len(s) <= 0 { 123 return nil 124 } 125 u := make([]string, 0, len(s)) 126 m := make(map[string]bool) 127 128 for _, val := range s { 129 if _, ok := m[val]; !ok { 130 m[val] = true 131 u = append(u, val) 132 } 133 } 134 return u 135 }