byteseq.go 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. package utils
  2. import (
  3. "github.com/gofiber/utils/v2/internal/caseconv"
  4. )
  5. type byteSeq interface {
  6. ~string | ~[]byte
  7. }
  8. // EqualFold tests ascii strings or bytes for equality case-insensitively
  9. func EqualFold[S byteSeq](b, s S) bool {
  10. if len(b) != len(s) {
  11. return false
  12. }
  13. table := caseconv.ToUpperTable
  14. n := len(b)
  15. i := 0
  16. // Unroll by 4 to match other hot paths and drive instruction-level parallelism.
  17. limit := n &^ 3
  18. for i < limit {
  19. b0 := b[i+0]
  20. s0 := s[i+0]
  21. if table[b0] != table[s0] {
  22. return false
  23. }
  24. b1 := b[i+1]
  25. s1 := s[i+1]
  26. if table[b1] != table[s1] {
  27. return false
  28. }
  29. b2 := b[i+2]
  30. s2 := s[i+2]
  31. if table[b2] != table[s2] {
  32. return false
  33. }
  34. b3 := b[i+3]
  35. s3 := s[i+3]
  36. if table[b3] != table[s3] {
  37. return false
  38. }
  39. i += 4
  40. }
  41. for i < n {
  42. if table[b[i]] != table[s[i]] {
  43. return false
  44. }
  45. i++
  46. }
  47. return true
  48. }
  49. // TrimLeft is the equivalent of strings/bytes.TrimLeft
  50. func TrimLeft[S byteSeq](s S, cutset byte) S {
  51. lenStr, start := len(s), 0
  52. for start < lenStr && s[start] == cutset {
  53. start++
  54. }
  55. return s[start:]
  56. }
  57. // Trim is the equivalent of strings/bytes.Trim
  58. func Trim[S byteSeq](s S, cutset byte) S {
  59. i, j := 0, len(s)-1
  60. for ; i <= j; i++ {
  61. if s[i] != cutset {
  62. break
  63. }
  64. }
  65. for ; i < j; j-- {
  66. if s[j] != cutset {
  67. break
  68. }
  69. }
  70. return s[i : j+1]
  71. }
  72. // TrimRight is the equivalent of strings/bytes.TrimRight
  73. func TrimRight[S byteSeq](s S, cutset byte) S {
  74. lenStr := len(s)
  75. for lenStr > 0 && s[lenStr-1] == cutset {
  76. lenStr--
  77. }
  78. return s[:lenStr]
  79. }
  80. // TrimSpace removes leading and trailing whitespace from a string or byte slice.
  81. // This is an optimized version that's faster than strings/bytes.TrimSpace for ASCII strings.
  82. // It removes the following ASCII whitespace characters: space, tab, newline, carriage return, vertical tab, and form feed.
  83. func TrimSpace[S byteSeq](s S) S {
  84. n := len(s)
  85. if n == 0 {
  86. return s
  87. }
  88. i, j := 0, n-1
  89. if !whitespaceTable[s[i]] && !whitespaceTable[s[j]] {
  90. return s
  91. }
  92. // Find first non-whitespace from start
  93. for ; i <= j && whitespaceTable[s[i]]; i++ { //nolint:revive // we want to check for multiple whitespace chars
  94. }
  95. // Find first non-whitespace from end
  96. for ; i < j && whitespaceTable[s[j]]; j-- { //nolint:revive // we want to check for multiple whitespace chars
  97. }
  98. return s[i : j+1]
  99. }