github.com/number571/tendermint@v0.34.11-gost/libs/strings/string.go (about) 1 package strings 2 3 import ( 4 "fmt" 5 "strings" 6 ) 7 8 // SplitAndTrimEmpty slices s into all subslices separated by sep and returns a 9 // slice of the string s with all leading and trailing Unicode code points 10 // contained in cutset removed. If sep is empty, SplitAndTrim splits after each 11 // UTF-8 sequence. First part is equivalent to strings.SplitN with a count of 12 // -1. also filter out empty strings, only return non-empty strings. 13 func SplitAndTrimEmpty(s, sep, cutset string) []string { 14 if s == "" { 15 return []string{} 16 } 17 18 spl := strings.Split(s, sep) 19 nonEmptyStrings := make([]string, 0, len(spl)) 20 21 for i := 0; i < len(spl); i++ { 22 element := strings.Trim(spl[i], cutset) 23 if element != "" { 24 nonEmptyStrings = append(nonEmptyStrings, element) 25 } 26 } 27 28 return nonEmptyStrings 29 } 30 31 // StringInSlice returns true if a is found the list. 32 func StringInSlice(a string, list []string) bool { 33 for _, b := range list { 34 if b == a { 35 return true 36 } 37 } 38 return false 39 } 40 41 // SplitAndTrim slices s into all subslices separated by sep and returns a 42 // slice of the string s with all leading and trailing Unicode code points 43 // contained in cutset removed. If sep is empty, SplitAndTrim splits after each 44 // UTF-8 sequence. First part is equivalent to strings.SplitN with a count of 45 // -1. 46 func SplitAndTrim(s, sep, cutset string) []string { 47 if s == "" { 48 return []string{} 49 } 50 51 spl := strings.Split(s, sep) 52 for i := 0; i < len(spl); i++ { 53 spl[i] = strings.Trim(spl[i], cutset) 54 } 55 return spl 56 } 57 58 // Returns true if s is a non-empty printable non-tab ascii character. 59 func IsASCIIText(s string) bool { 60 if len(s) == 0 { 61 return false 62 } 63 for _, b := range []byte(s) { 64 if 32 <= b && b <= 126 { 65 // good 66 } else { 67 return false 68 } 69 } 70 return true 71 } 72 73 // NOTE: Assumes that s is ASCII as per IsASCIIText(), otherwise panics. 74 func ASCIITrim(s string) string { 75 r := make([]byte, 0, len(s)) 76 for _, b := range []byte(s) { 77 switch { 78 case b == 32: 79 continue // skip space 80 case 32 < b && b <= 126: 81 r = append(r, b) 82 default: 83 panic(fmt.Sprintf("non-ASCII (non-tab) char 0x%X", b)) 84 } 85 } 86 return string(r) 87 } 88 89 // StringSliceEqual checks if string slices a and b are equal 90 func StringSliceEqual(a, b []string) bool { 91 if len(a) != len(b) { 92 return false 93 } 94 for i := 0; i < len(a); i++ { 95 if a[i] != b[i] { 96 return false 97 } 98 } 99 return true 100 }