case.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. package strings
  2. import (
  3. "github.com/gofiber/utils/v2/internal/caseconv"
  4. "github.com/gofiber/utils/v2/internal/unsafeconv"
  5. )
  6. // ToLower converts an ASCII string to lower-case without modifying the input.
  7. func ToLower(s string) string {
  8. n := len(s)
  9. if n == 0 {
  10. return s
  11. }
  12. table := caseconv.ToLowerTable
  13. for i := range n {
  14. c := s[i]
  15. low := table[c]
  16. if low != c {
  17. res := make([]byte, n)
  18. copy(res, s[:i])
  19. res[i] = low
  20. j := i + 1
  21. for ; j+3 < n; j += 4 {
  22. res[j+0] = table[s[j+0]]
  23. res[j+1] = table[s[j+1]]
  24. res[j+2] = table[s[j+2]]
  25. res[j+3] = table[s[j+3]]
  26. }
  27. for ; j < n; j++ {
  28. res[j] = table[s[j]]
  29. }
  30. return unsafeconv.UnsafeString(res)
  31. }
  32. }
  33. return s
  34. }
  35. // ToUpper converts an ASCII string to upper-case without modifying the input.
  36. func ToUpper(s string) string {
  37. n := len(s)
  38. if n == 0 {
  39. return s
  40. }
  41. table := caseconv.ToUpperTable
  42. for i := range n {
  43. c := s[i]
  44. up := table[c]
  45. if up != c {
  46. res := make([]byte, n)
  47. copy(res, s[:i])
  48. res[i] = up
  49. j := i + 1
  50. for ; j+3 < n; j += 4 {
  51. res[j+0] = table[s[j+0]]
  52. res[j+1] = table[s[j+1]]
  53. res[j+2] = table[s[j+2]]
  54. res[j+3] = table[s[j+3]]
  55. }
  56. for ; j < n; j++ {
  57. res[j] = table[s[j]]
  58. }
  59. return unsafeconv.UnsafeString(res)
  60. }
  61. }
  62. return s
  63. }
  64. // UnsafeToLower converts an ASCII string to lower-case by mutating its backing bytes in-place.
  65. // This function is unsafe: it breaks string immutability and must only be used when the
  66. // string is known to reference mutable memory.
  67. func UnsafeToLower(s string) string {
  68. b := unsafeconv.UnsafeBytes(s)
  69. table := caseconv.ToLowerTable
  70. n := len(b)
  71. i := 0
  72. limit := n &^ 3
  73. for i < limit {
  74. b0 := b[i+0]
  75. b1 := b[i+1]
  76. b2 := b[i+2]
  77. b3 := b[i+3]
  78. b[i+0] = table[b0]
  79. b[i+1] = table[b1]
  80. b[i+2] = table[b2]
  81. b[i+3] = table[b3]
  82. i += 4
  83. }
  84. for i < n {
  85. b[i] = table[b[i]]
  86. i++
  87. }
  88. return s
  89. }
  90. // UnsafeToUpper converts an ASCII string to upper-case by mutating its backing bytes in-place.
  91. // This function is unsafe: it breaks string immutability and must only be used when the
  92. // string is known to reference mutable memory.
  93. func UnsafeToUpper(s string) string {
  94. b := unsafeconv.UnsafeBytes(s)
  95. table := caseconv.ToUpperTable
  96. n := len(b)
  97. i := 0
  98. limit := n &^ 3
  99. for i < limit {
  100. b0 := b[i+0]
  101. b1 := b[i+1]
  102. b2 := b[i+2]
  103. b3 := b[i+3]
  104. b[i+0] = table[b0]
  105. b[i+1] = table[b1]
  106. b[i+2] = table[b2]
  107. b[i+3] = table[b3]
  108. i += 4
  109. }
  110. for i < n {
  111. b[i] = table[b[i]]
  112. i++
  113. }
  114. return s
  115. }