strings.go 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. // ⚡️ Fiber is an Express inspired web framework written in Go with ☕️
  2. // 🤖 Github Repository: https://github.com/gofiber/fiber
  3. // 📌 API Documentation: https://docs.gofiber.io
  4. package utils
  5. // ToLower converts ascii string to lower-case
  6. func ToLower(b string) string {
  7. res := make([]byte, len(b))
  8. copy(res, b)
  9. for i := 0; i < len(res); i++ {
  10. res[i] = toLowerTable[res[i]]
  11. }
  12. return UnsafeString(res)
  13. }
  14. // ToUpper converts ascii string to upper-case
  15. func ToUpper(b string) string {
  16. res := make([]byte, len(b))
  17. copy(res, b)
  18. for i := 0; i < len(res); i++ {
  19. res[i] = toUpperTable[res[i]]
  20. }
  21. return UnsafeString(res)
  22. }
  23. // TrimLeft is the equivalent of strings.TrimLeft
  24. func TrimLeft(s string, cutset byte) string {
  25. lenStr, start := len(s), 0
  26. for start < lenStr && s[start] == cutset {
  27. start++
  28. }
  29. return s[start:]
  30. }
  31. // Trim is the equivalent of strings.Trim
  32. func Trim(s string, cutset byte) string {
  33. i, j := 0, len(s)-1
  34. for ; i <= j; i++ {
  35. if s[i] != cutset {
  36. break
  37. }
  38. }
  39. for ; i < j; j-- {
  40. if s[j] != cutset {
  41. break
  42. }
  43. }
  44. return s[i : j+1]
  45. }
  46. // TrimRight is the equivalent of strings.TrimRight
  47. func TrimRight(s string, cutset byte) string {
  48. lenStr := len(s)
  49. for lenStr > 0 && s[lenStr-1] == cutset {
  50. lenStr--
  51. }
  52. return s[:lenStr]
  53. }
  54. // EqualFold tests ascii strings for equality case-insensitively
  55. func EqualFold(b, s string) bool {
  56. if len(b) != len(s) {
  57. return false
  58. }
  59. for i := len(b) - 1; i >= 0; i-- {
  60. if toUpperTable[b[i]] != toUpperTable[s[i]] {
  61. return false
  62. }
  63. }
  64. return true
  65. }