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