Debian Codegraph @

bytes/case.go

raw symbols SCIP
1 package bytes
2
3 import (
4 "github.com/gofiber/utils/v2/internal/caseconv"
5 )
6
7 // swarMinLen is the smallest input length worth routing through the
8 // word-at-a-time (SWAR) helpers; shorter inputs are cheaper byte-by-byte.
9 const swarMinLen = caseconv.WordLen
10
11 // ToLower converts an ASCII byte slice to lower-case without modifying the input.
12 func ToLower(b []byte) []byte {
13 n := len(b)
14 if n < swarMinLen {
15 table := caseconv.ToLowerTable
16 for i := 0; i < n; i++ {
17 c := b[i]
18 low := table[c]
19 if low != c {
20 dst := make([]byte, n)
21 copy(dst, b[:i])
22 dst[i] = low
23 for i++; i < n; i++ {
24 dst[i] = table[b[i]]
25 }
26 return dst
27 }
28 }
29 return b
30 }
31
32 i := caseconv.FirstUpperIndex(b)
33 if i < 0 {
34 return b
35 }
36
37 dst := make([]byte, n)
38 // Copy the unchanged prefix up to the word containing the first
39 // uppercase byte, then convert the rest word-at-a-time.
40 from := i &^ (caseconv.WordLen - 1)
41 copy(dst, b[:from])
42 caseconv.ToLowerCopy(dst, b, from)
43 return dst
44 }
45
46 // ToUpper converts an ASCII byte slice to upper-case without modifying the input.
47 func ToUpper(b []byte) []byte {
48 n := len(b)
49 if n < swarMinLen {
50 table := caseconv.ToUpperTable
51 for i := 0; i < n; i++ {
52 c := b[i]
53 up := table[c]
54 if up != c {
55 dst := make([]byte, n)
56 copy(dst, b[:i])
57 dst[i] = up
58 for i++; i < n; i++ {
59 dst[i] = table[b[i]]
60 }
61 return dst
62 }
63 }
64 return b
65 }
66
67 i := caseconv.FirstLowerIndex(b)
68 if i < 0 {
69 return b
70 }
71
72 dst := make([]byte, n)
73 from := i &^ (caseconv.WordLen - 1)
74 copy(dst, b[:from])
75 caseconv.ToUpperCopy(dst, b, from)
76 return dst
77 }
78
79 // UnsafeToLower converts an ASCII byte slice to lower-case in-place.
80 // The passed slice content is modified and the same slice is returned.
81 func UnsafeToLower(b []byte) []byte {
82 if len(b) < swarMinLen {
83 table := caseconv.ToLowerTable
84 for i := range b {
85 b[i] = table[b[i]]
86 }
87 return b
88 }
89 caseconv.ToLowerInPlace(b)
90 return b
91 }
92
93 // UnsafeToUpper converts an ASCII byte slice to upper-case in-place.
94 // The passed slice content is modified and the same slice is returned.
95 func UnsafeToUpper(b []byte) []byte {
96 if len(b) < swarMinLen {
97 table := caseconv.ToUpperTable
98 for i := range b {
99 b[i] = table[b[i]]
100 }
101 return b
102 }
103 caseconv.ToUpperInPlace(b)
104 return b
105 }