strings.go 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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 is the equivalent of strings.ToLower
  6. func ToLower(b string) string {
  7. var 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 GetString(res)
  13. }
  14. // ToUpper is the equivalent of strings.ToUpper
  15. func ToUpper(b string) string {
  16. var 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 GetString(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. // DefaultString returns the provided fallback value if string is empty
  55. func DefaultString(value string, defaultValue string) string {
  56. if len(value) <= 0 {
  57. return defaultValue
  58. }
  59. return value
  60. }