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 (124 lines) | stat: -rw-r--r-- 2,501 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
package strings

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

// ToLower converts an ASCII string to lower-case without modifying the input.
func ToLower(s string) string {
	n := len(s)
	if n == 0 {
		return s
	}

	table := caseconv.ToLowerTable
	for i := range n {
		c := s[i]
		low := table[c]
		if low != c {
			res := make([]byte, n)
			copy(res, s[:i])
			res[i] = low
			j := i + 1
			for ; j+3 < n; j += 4 {
				res[j+0] = table[s[j+0]]
				res[j+1] = table[s[j+1]]
				res[j+2] = table[s[j+2]]
				res[j+3] = table[s[j+3]]
			}
			for ; j < n; j++ {
				res[j] = table[s[j]]
			}
			return unsafeconv.UnsafeString(res)
		}
	}
	return s
}

// ToUpper converts an ASCII string to upper-case without modifying the input.
func ToUpper(s string) string {
	n := len(s)
	if n == 0 {
		return s
	}

	table := caseconv.ToUpperTable
	for i := range n {
		c := s[i]
		up := table[c]
		if up != c {
			res := make([]byte, n)
			copy(res, s[:i])
			res[i] = up
			j := i + 1
			for ; j+3 < n; j += 4 {
				res[j+0] = table[s[j+0]]
				res[j+1] = table[s[j+1]]
				res[j+2] = table[s[j+2]]
				res[j+3] = table[s[j+3]]
			}
			for ; j < n; j++ {
				res[j] = table[s[j]]
			}
			return unsafeconv.UnsafeString(res)
		}
	}
	return s
}

// UnsafeToLower converts an ASCII string to lower-case by mutating its backing bytes in-place.
// This function is unsafe: it breaks string immutability and must only be used when the
// string is known to reference mutable memory.
func UnsafeToLower(s string) string {
	b := unsafeconv.UnsafeBytes(s)
	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 s
}

// UnsafeToUpper converts an ASCII string to upper-case by mutating its backing bytes in-place.
// This function is unsafe: it breaks string immutability and must only be used when the
// string is known to reference mutable memory.
func UnsafeToUpper(s string) string {
	b := unsafeconv.UnsafeBytes(s)
	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 s
}