utils_str.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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. return dotCount <= 1
  70. }
  71. // UcFirst returns a copy of the string s with the first letter mapped to its upper case.
  72. func UcFirst(s string) string {
  73. if len(s) == 0 {
  74. return s
  75. }
  76. if IsLetterLower(s[0]) {
  77. return string(s[0]-32) + s[1:]
  78. }
  79. return s
  80. }
  81. // ReplaceByMap returns a copy of `origin`,
  82. // which is replaced by a map in unordered way, case-sensitively.
  83. func ReplaceByMap(origin string, replaces map[string]string) string {
  84. for k, v := range replaces {
  85. origin = strings.ReplaceAll(origin, k, v)
  86. }
  87. return origin
  88. }
  89. // RemoveSymbols removes all symbols from string and lefts only numbers and letters.
  90. func RemoveSymbols(s string) string {
  91. var b = make([]rune, 0, len(s))
  92. for _, c := range s {
  93. if c > 127 {
  94. b = append(b, c)
  95. } else if (c >= '0' && c <= '9') || (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') {
  96. b = append(b, c)
  97. }
  98. }
  99. return string(b)
  100. }
  101. // EqualFoldWithoutChars checks string `s1` and `s2` equal case-insensitively,
  102. // with/without chars '-'/'_'/'.'/' '.
  103. func EqualFoldWithoutChars(s1, s2 string) bool {
  104. return strings.EqualFold(RemoveSymbols(s1), RemoveSymbols(s2))
  105. }
  106. // SplitAndTrim splits string `str` by a string `delimiter` to an array,
  107. // and calls Trim to every element of this array. It ignores the elements
  108. // which are empty after Trim.
  109. func SplitAndTrim(str, delimiter string, characterMask ...string) []string {
  110. array := make([]string, 0)
  111. for _, v := range strings.Split(str, delimiter) {
  112. v = Trim(v, characterMask...)
  113. if v != "" {
  114. array = append(array, v)
  115. }
  116. }
  117. return array
  118. }
  119. // Trim strips whitespace (or other characters) from the beginning and end of a string.
  120. // The optional parameter `characterMask` specifies the additional stripped characters.
  121. func Trim(str string, characterMask ...string) string {
  122. trimChars := DefaultTrimChars
  123. if len(characterMask) > 0 {
  124. trimChars += characterMask[0]
  125. }
  126. return strings.Trim(str, trimChars)
  127. }
  128. // FormatCmdKey formats string `s` as command key using uniformed format.
  129. func FormatCmdKey(s string) string {
  130. return strings.ToLower(strings.ReplaceAll(s, "_", "."))
  131. }
  132. // FormatEnvKey formats string `s` as environment key using uniformed format.
  133. func FormatEnvKey(s string) string {
  134. return strings.ToUpper(strings.ReplaceAll(s, ".", "_"))
  135. }
  136. // StripSlashes un-quotes a quoted string by AddSlashes.
  137. func StripSlashes(str string) string {
  138. var buf bytes.Buffer
  139. l, skip := len(str), false
  140. for i, char := range str {
  141. if skip {
  142. skip = false
  143. } else if char == '\\' {
  144. if i+1 < l && str[i+1] == '\\' {
  145. skip = true
  146. }
  147. continue
  148. }
  149. buf.WriteRune(char)
  150. }
  151. return buf.String()
  152. }