utils_str.go 4.2 KB

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