github.com/TeaOSLab/EdgeNode@v1.3.8/internal/utils/string.go (about) 1 package utils 2 3 import ( 4 "sort" 5 "strings" 6 "unsafe" 7 ) 8 9 // UnsafeBytesToString convert bytes to string 10 func UnsafeBytesToString(bs []byte) string { 11 return *(*string)(unsafe.Pointer(&bs)) 12 } 13 14 // UnsafeStringToBytes convert string to bytes 15 func UnsafeStringToBytes(s string) []byte { 16 return *(*[]byte)(unsafe.Pointer(&s)) 17 } 18 19 // FormatAddress format address 20 func FormatAddress(addr string) string { 21 if strings.HasSuffix(addr, "unix:") { 22 return addr 23 } 24 addr = strings.Replace(addr, " ", "", -1) 25 addr = strings.Replace(addr, "\t", "", -1) 26 addr = strings.Replace(addr, ":", ":", -1) 27 addr = strings.TrimSpace(addr) 28 return addr 29 } 30 31 // FormatAddressList format address list 32 func FormatAddressList(addrList []string) []string { 33 result := []string{} 34 for _, addr := range addrList { 35 result = append(result, FormatAddress(addr)) 36 } 37 return result 38 } 39 40 // ToValidUTF8string 去除字符串中的非UTF-8字符 41 func ToValidUTF8string(v string) string { 42 return strings.ToValidUTF8(v, "") 43 } 44 45 // EqualStrings 检查两个字符串slice内容是否一致 46 func EqualStrings(s1 []string, s2 []string) bool { 47 if len(s1) != len(s2) { 48 return false 49 } 50 sort.Strings(s1) 51 sort.Strings(s2) 52 for index, v1 := range s1 { 53 if v1 != s2[index] { 54 return false 55 } 56 } 57 return true 58 } 59 60 // CutPrefix returns s without the provided leading prefix string 61 // and reports whether it found the prefix. 62 // If s doesn't start with prefix, CutPrefix returns s, false. 63 // If prefix is the empty string, CutPrefix returns s, true. 64 // 65 // copy from go source 66 func CutPrefix(s, prefix string) (after string, found bool) { 67 if !strings.HasPrefix(s, prefix) { 68 return s, false 69 } 70 return s[len(prefix):], true 71 }