github.com/wangyougui/gf/v2@v2.6.5/internal/utils/utils_str.go (about) 1 // Copyright GoFrame Author(https://goframe.org). All Rights Reserved. 2 // 3 // This Source Code Form is subject to the terms of the MIT License. 4 // If a copy of the MIT was not distributed with this file, 5 // You can obtain one at https://github.com/wangyougui/gf. 6 7 package utils 8 9 import ( 10 "bytes" 11 "strings" 12 ) 13 14 var ( 15 // DefaultTrimChars are the characters which are stripped by Trim* functions in default. 16 DefaultTrimChars = string([]byte{ 17 '\t', // Tab. 18 '\v', // Vertical tab. 19 '\n', // New line (line feed). 20 '\r', // Carriage return. 21 '\f', // New page. 22 ' ', // Ordinary space. 23 0x00, // NUL-byte. 24 0x85, // Delete. 25 0xA0, // Non-breaking space. 26 }) 27 ) 28 29 // IsLetterUpper checks whether the given byte b is in upper case. 30 func IsLetterUpper(b byte) bool { 31 if b >= byte('A') && b <= byte('Z') { 32 return true 33 } 34 return false 35 } 36 37 // IsLetterLower checks whether the given byte b is in lower case. 38 func IsLetterLower(b byte) bool { 39 if b >= byte('a') && b <= byte('z') { 40 return true 41 } 42 return false 43 } 44 45 // IsLetter checks whether the given byte b is a letter. 46 func IsLetter(b byte) bool { 47 return IsLetterUpper(b) || IsLetterLower(b) 48 } 49 50 // IsNumeric checks whether the given string s is numeric. 51 // Note that float string like "123.456" is also numeric. 52 func IsNumeric(s string) bool { 53 var ( 54 dotCount = 0 55 length = len(s) 56 ) 57 if length == 0 { 58 return false 59 } 60 for i := 0; i < length; i++ { 61 if s[i] == '-' && i == 0 { 62 continue 63 } 64 if s[i] == '.' { 65 dotCount++ 66 if i > 0 && i < length-1 { 67 continue 68 } else { 69 return false 70 } 71 } 72 if s[i] < '0' || s[i] > '9' { 73 return false 74 } 75 } 76 return dotCount <= 1 77 } 78 79 // UcFirst returns a copy of the string s with the first letter mapped to its upper case. 80 func UcFirst(s string) string { 81 if len(s) == 0 { 82 return s 83 } 84 if IsLetterLower(s[0]) { 85 return string(s[0]-32) + s[1:] 86 } 87 return s 88 } 89 90 // ReplaceByMap returns a copy of `origin`, 91 // which is replaced by a map in unordered way, case-sensitively. 92 func ReplaceByMap(origin string, replaces map[string]string) string { 93 for k, v := range replaces { 94 origin = strings.ReplaceAll(origin, k, v) 95 } 96 return origin 97 } 98 99 // RemoveSymbols removes all symbols from string and lefts only numbers and letters. 100 func RemoveSymbols(s string) string { 101 var b = make([]rune, 0, len(s)) 102 for _, c := range s { 103 if c > 127 { 104 b = append(b, c) 105 } else if (c >= '0' && c <= '9') || (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') { 106 b = append(b, c) 107 } 108 } 109 return string(b) 110 } 111 112 // EqualFoldWithoutChars checks string `s1` and `s2` equal case-insensitively, 113 // with/without chars '-'/'_'/'.'/' '. 114 func EqualFoldWithoutChars(s1, s2 string) bool { 115 return strings.EqualFold(RemoveSymbols(s1), RemoveSymbols(s2)) 116 } 117 118 // SplitAndTrim splits string `str` by a string `delimiter` to an array, 119 // and calls Trim to every element of this array. It ignores the elements 120 // which are empty after Trim. 121 func SplitAndTrim(str, delimiter string, characterMask ...string) []string { 122 array := make([]string, 0) 123 for _, v := range strings.Split(str, delimiter) { 124 v = Trim(v, characterMask...) 125 if v != "" { 126 array = append(array, v) 127 } 128 } 129 return array 130 } 131 132 // Trim strips whitespace (or other characters) from the beginning and end of a string. 133 // The optional parameter `characterMask` specifies the additional stripped characters. 134 func Trim(str string, characterMask ...string) string { 135 trimChars := DefaultTrimChars 136 if len(characterMask) > 0 { 137 trimChars += characterMask[0] 138 } 139 return strings.Trim(str, trimChars) 140 } 141 142 // FormatCmdKey formats string `s` as command key using uniformed format. 143 func FormatCmdKey(s string) string { 144 return strings.ToLower(strings.ReplaceAll(s, "_", ".")) 145 } 146 147 // FormatEnvKey formats string `s` as environment key using uniformed format. 148 func FormatEnvKey(s string) string { 149 return strings.ToUpper(strings.ReplaceAll(s, ".", "_")) 150 } 151 152 // StripSlashes un-quotes a quoted string by AddSlashes. 153 func StripSlashes(str string) string { 154 var buf bytes.Buffer 155 l, skip := len(str), false 156 for i, char := range str { 157 if skip { 158 skip = false 159 } else if char == '\\' { 160 if i+1 < l && str[i+1] == '\\' { 161 skip = true 162 } 163 continue 164 } 165 buf.WriteRune(char) 166 } 167 return buf.String() 168 }