forked from segmentio/go-camelcase
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsnakebuffer_bench_test.go
167 lines (145 loc) · 3.58 KB
/
snakebuffer_bench_test.go
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
package strcase
import (
"bytes"
"testing"
)
func BenchmarkBufferSnakeUnchangedLong(b *testing.B) {
s := "invite_your_customers_add_invites"
b.SetBytes(int64(len(s)))
for n := 0; n < b.N; n++ {
snakeByteBuffer(s)
}
}
func BenchmarkBufferSnakeUnchangedSimple(b *testing.B) {
s := "sample_text"
b.SetBytes(int64(len(s)))
for n := 0; n < b.N; n++ {
snakeByteBuffer(s)
}
}
func BenchmarkBufferSnakeModifiedUnicode(b *testing.B) {
s := "ß_ƒ_foo"
b.SetBytes(int64(len(s)))
for n := 0; n < b.N; n++ {
snakeByteBuffer(s)
}
}
func BenchmarkBufferSnakeCaseASCIILong(b *testing.B) {
s := "some sample text here_noething:too$amazing"
b.SetBytes(int64(len(s)))
for i := 0; i < b.N; i++ {
snakeByteBuffer(s)
}
}
func BenchmarkBufferSnakeModifiedLong(b *testing.B) {
s := "inviteYourCustomersAddInvites"
b.SetBytes(int64(len(s)))
for n := 0; n < b.N; n++ {
snakeByteBuffer(s)
}
}
func BenchmarkBufferSnakeModifiedLongSpecialChars(b *testing.B) {
s := "FOO:BAR$BAZ__Sample Text___"
b.SetBytes(int64(len(s)))
for n := 0; n < b.N; n++ {
snakeByteBuffer(s)
}
}
func BenchmarkBufferSnakeModifiedSimple(b *testing.B) {
s := "sample text"
b.SetBytes(int64(len(s)))
for n := 0; n < b.N; n++ {
snakeByteBuffer("sample text")
}
}
func BenchmarkBufferSnakeModifiedUnicode2(b *testing.B) {
s := "ẞ•¶§ƒ˚foo˙∆˚¬"
b.SetBytes(int64(len(s)))
for n := 0; n < b.N; n++ {
snakeByteBuffer(s)
}
}
func BenchmarkBufferSnakeLeadingUnderscoresDigitUpper(b *testing.B) {
s := "_5TEst"
b.SetBytes(int64(len(s)))
for n := 0; n < b.N; n++ {
snakeByteBuffer(s)
}
}
func BenchmarkBufferSnakeDigitUpper(b *testing.B) {
s := "5TEst"
b.SetBytes(int64(len(s)))
for n := 0; n < b.N; n++ {
snakeByteBuffer(s)
}
}
func BenchmarkBufferSnakeDigitUpper2(b *testing.B) {
s := "lk0B@bFmjrLQ_Z6YL"
b.SetBytes(int64(len(s)))
for n := 0; n < b.N; n++ {
snakeByteBuffer(s)
}
}
// snakeByteBuffer returns the snake-case of the given string.
// it is the same function as Snake but uses a bytes.Buffer instead of []byte
// in order to test the performance of the bytes.Buffer vs []byte.
func snakeByteBuffer(s string) string { // nolint: unparam
idx := 0
hasLower := false
hasUnderscore := false
lowercaseSinceUnderscore := false
// loop through all good characters:
// - lowercase
// - digit
// - underscore (as long as the next character is lowercase or digit)
for ; idx < len(s); idx++ {
if isLower(s[idx]) {
hasLower = true
if hasUnderscore {
lowercaseSinceUnderscore = true
}
continue
} else if isDigit(s[idx]) {
continue
} else if s[idx] == underscoreByte && idx > 0 && idx < len(s)-1 && (isLower(s[idx+1]) || isDigit(s[idx+1])) {
hasUnderscore = true
lowercaseSinceUnderscore = false
continue
}
break
}
if idx == len(s) {
return s // no changes needed, can just borrow the string
}
// if we get here then we must need to manipulate the string
var b bytes.Buffer
b.WriteString(s[:idx])
if isUpper(s[idx]) && (!hasLower || hasUnderscore && !lowercaseSinceUnderscore) {
for idx < len(s) && (isUpper(s[idx]) || isDigit(s[idx])) {
b.WriteByte(asciiLowercaseArray[s[idx]])
idx++
}
for idx < len(s) && (isLower(s[idx]) || isDigit(s[idx])) {
b.WriteByte(s[idx])
idx++
}
}
for idx < len(s) {
if !isAlphanumeric(s[idx]) {
idx++
continue
}
if b.Len() > 0 {
b.WriteByte(underscoreByte)
}
for idx < len(s) && (isUpper(s[idx]) || isDigit(s[idx])) {
b.WriteByte(asciiLowercaseArray[s[idx]])
idx++
}
for idx < len(s) && (isLower(s[idx]) || isDigit(s[idx])) {
b.WriteByte(s[idx])
idx++
}
}
return b.String()
}