github.com/kubesphere/s2irun@v3.2.1+incompatible/pkg/utils/stringutils/string.go (about) 1 package stringutils 2 3 import ( 4 "unicode/utf8" 5 ) 6 7 // Creates an slice of slice values not included in the other given slice. 8 func Diff(base, exclude []string) (result []string) { 9 excludeMap := make(map[string]bool) 10 for _, s := range exclude { 11 excludeMap[s] = true 12 } 13 for _, s := range base { 14 if !excludeMap[s] { 15 result = append(result, s) 16 } 17 } 18 return result 19 } 20 21 func Unique(ss []string) (result []string) { 22 smap := make(map[string]bool) 23 for _, s := range ss { 24 smap[s] = true 25 } 26 for s := range smap { 27 result = append(result, s) 28 } 29 return result 30 } 31 32 func FindString(array []string, str string) int { 33 for index, s := range array { 34 if str == s { 35 return index 36 } 37 } 38 return -1 39 } 40 41 func StringIn(str string, array []string) bool { 42 return FindString(array, str) > -1 43 } 44 45 func Reverse(s string) string { 46 size := len(s) 47 buf := make([]byte, size) 48 for start := 0; start < size; { 49 r, n := utf8.DecodeRuneInString(s[start:]) 50 start += n 51 utf8.EncodeRune(buf[size-start:], r) 52 } 53 return string(buf) 54 }