File: case.go

package info (click to toggle)
golang-github-gofiber-utils 2.0.2-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 432 kB
  • sloc: makefile: 40
file content (131 lines) | stat: -rw-r--r-- 2,232 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
package bytes

import (
	"github.com/gofiber/utils/v2/internal/caseconv"
)

// ToLower converts an ASCII byte slice to lower-case without modifying the input.
func ToLower(b []byte) []byte {
	n := len(b)
	if n == 0 {
		return b
	}

	table := caseconv.ToLowerTable
	i := 0
	for i < n {
		c := b[i]
		low := table[c]
		if low != c {
			dst := make([]byte, n)
			copy(dst, b[:i])
			dst[i] = low
			i++
			limit := i + ((n - i) &^ 3)
			for i < limit {
				dst[i+0] = table[b[i+0]]
				dst[i+1] = table[b[i+1]]
				dst[i+2] = table[b[i+2]]
				dst[i+3] = table[b[i+3]]
				i += 4
			}
			for i < n {
				dst[i] = table[b[i]]
				i++
			}
			return dst
		}
		i++
	}
	return b
}

// ToUpper converts an ASCII byte slice to upper-case without modifying the input.
func ToUpper(b []byte) []byte {
	n := len(b)
	if n == 0 {
		return b
	}

	table := caseconv.ToUpperTable
	i := 0
	for i < n {
		c := b[i]
		up := table[c]
		if up != c {
			dst := make([]byte, n)
			copy(dst, b[:i])
			dst[i] = up
			i++
			limit := i + ((n - i) &^ 3)
			for i < limit {
				dst[i+0] = table[b[i+0]]
				dst[i+1] = table[b[i+1]]
				dst[i+2] = table[b[i+2]]
				dst[i+3] = table[b[i+3]]
				i += 4
			}
			for i < n {
				dst[i] = table[b[i]]
				i++
			}
			return dst
		}
		i++
	}
	return b
}

// UnsafeToLower converts an ASCII byte slice to lower-case in-place.
// The passed slice content is modified and the same slice is returned.
func UnsafeToLower(b []byte) []byte {
	table := caseconv.ToLowerTable
	n := len(b)
	i := 0
	limit := n &^ 3
	for i < limit {
		b0 := b[i+0]
		b1 := b[i+1]
		b2 := b[i+2]
		b3 := b[i+3]

		b[i+0] = table[b0]
		b[i+1] = table[b1]
		b[i+2] = table[b2]
		b[i+3] = table[b3]

		i += 4
	}
	for i < n {
		b[i] = table[b[i]]
		i++
	}
	return b
}

// UnsafeToUpper converts an ASCII byte slice to upper-case in-place.
// The passed slice content is modified and the same slice is returned.
func UnsafeToUpper(b []byte) []byte {
	table := caseconv.ToUpperTable
	n := len(b)
	i := 0
	limit := n &^ 3
	for i < limit {
		b0 := b[i+0]
		b1 := b[i+1]
		b2 := b[i+2]
		b3 := b[i+3]

		b[i+0] = table[b0]
		b[i+1] = table[b1]
		b[i+2] = table[b2]
		b[i+3] = table[b3]

		i += 4
	}
	for i < n {
		b[i] = table[b[i]]
		i++
	}
	return b
}