Debian Codegraph @

bytes/case.go

raw
1 package bytes
2
3 import (
4 "github.com/gofiber/utils/v2/internal/caseconv"
5 )
6
7 // Sub-word inputs convert here, not in caseconv: at these lengths the call costs
8 // more than the table lookups. Delegating them measured +47%/+43% on unchanged
9 // three-byte input and +65%/+60% for the in-place forms (benchstat n=15,
10 // interleaved, Go 1.26, linux/amd64; the http-get cases reproduce it).
11
12 // ToLower converts an ASCII byte slice to lower-case without modifying the input.
13 func ToLower(b []byte) []byte {
14 n := len(b)
15 if n < caseconv.WordLen {
16 table := caseconv.ToLowerTable
17 for i := 0; i < n; i++ {
18 c := b[i]
19 low := table[c]
20 if low != c {
21 dst := make([]byte, n)
22 copy(dst, b[:i])
23 dst[i] = low
24 for i++; i < n; i++ {
25 dst[i] = table[b[i]]
26 }
27 return dst
28 }
29 }
30 return b
31 }
32
33 i := caseconv.FirstUpperIndex(b)
34 if i < 0 {
35 return b
36 }
37 return caseconv.ToLowerFrom(b, i)
38 }
39
40 // ToUpper converts an ASCII byte slice to upper-case without modifying the input.
41 func ToUpper(b []byte) []byte {
42 n := len(b)
43 if n < caseconv.WordLen {
44 table := caseconv.ToUpperTable
45 for i := 0; i < n; i++ {
46 c := b[i]
47 up := table[c]
48 if up != c {
49 dst := make([]byte, n)
50 copy(dst, b[:i])
51 dst[i] = up
52 for i++; i < n; i++ {
53 dst[i] = table[b[i]]
54 }
55 return dst
56 }
57 }
58 return b
59 }
60
61 i := caseconv.FirstLowerIndex(b)
62 if i < 0 {
63 return b
64 }
65 return caseconv.ToUpperFrom(b, i)
66 }
67
68 // UnsafeToLower converts an ASCII byte slice to lower-case in-place.
69 // The passed slice content is modified and the same slice is returned.
70 func UnsafeToLower(b []byte) []byte {
71 if len(b) < caseconv.WordLen {
72 table := caseconv.ToLowerTable
73 for i := range b {
74 b[i] = table[b[i]]
75 }
76 return b
77 }
78 caseconv.ToLowerInPlace(b)
79 return b
80 }
81
82 // UnsafeToUpper converts an ASCII byte slice to upper-case in-place.
83 // The passed slice content is modified and the same slice is returned.
84 func UnsafeToUpper(b []byte) []byte {
85 if len(b) < caseconv.WordLen {
86 table := caseconv.ToUpperTable
87 for i := range b {
88 b[i] = table[b[i]]
89 }
90 return b
91 }
92 caseconv.ToUpperInPlace(b)
93 return b
94 }