-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
8bit.go
331 lines (265 loc) · 7.38 KB
/
8bit.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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
/*
Copyright 2021 The Vitess Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package collations
import (
"vitess.io/vitess/go/mysql/collations/internal/charset"
)
var sortOrderIdentity [256]byte
func init() {
for i := range sortOrderIdentity {
sortOrderIdentity[i] = byte(i)
}
register(&Collation_binary{})
}
type simpletables struct {
// By default we're not building in the tables for lower/upper-casing and
// character classes, because we're not using them for collation and they
// take up a lot of binary space.
// Uncomment these fields and pass `-full8bit` to `makemysqldata` to generate
// these tables.
tolower *[256]byte
toupper *[256]byte
ctype *[256]byte
sort *[256]byte
}
type Collation_8bit_bin struct {
id ID
name string
simpletables
charset charset.Charset
}
func (c *Collation_8bit_bin) Init() {}
func (c *Collation_8bit_bin) Name() string {
return c.name
}
func (c *Collation_8bit_bin) ID() ID {
return c.id
}
func (c *Collation_8bit_bin) Charset() charset.Charset {
return c.charset
}
func (c *Collation_8bit_bin) IsBinary() bool {
return true
}
func (c *Collation_8bit_bin) Collate(left, right []byte, rightIsPrefix bool) int {
return collationBinary(left, right, rightIsPrefix)
}
func (c *Collation_8bit_bin) WeightString(dst, src []byte, numCodepoints int) []byte {
copyCodepoints := len(src)
var padToMax bool
switch numCodepoints {
case 0:
numCodepoints = copyCodepoints
case PadToMax:
padToMax = true
default:
copyCodepoints = minInt(copyCodepoints, numCodepoints)
}
dst = append(dst, src[:copyCodepoints]...)
return weightStringPadingSimple(' ', dst, numCodepoints-copyCodepoints, padToMax)
}
func (c *Collation_8bit_bin) Hash(src []byte, numCodepoints int) HashCode {
hash := 0x8b8b0000 | uintptr(c.id)
if numCodepoints == 0 {
return memhash(src, hash)
}
tocopy := minInt(len(src), numCodepoints)
hash = memhash(src[:tocopy], hash)
numCodepoints -= tocopy
for numCodepoints > 0 {
hash = memhash8(' ', hash)
numCodepoints--
}
return hash
}
func (c *Collation_8bit_bin) WeightStringLen(numBytes int) int {
return numBytes
}
func (c *Collation_8bit_bin) Wildcard(pat []byte, matchOne rune, matchMany rune, escape rune) WildcardPattern {
return newEightbitWildcardMatcher(&sortOrderIdentity, c.Collate, pat, matchOne, matchMany, escape)
}
func (c *Collation_8bit_bin) ToLower(dst, src []byte) []byte {
lowerTable := c.simpletables.tolower
for _, c := range src {
dst = append(dst, lowerTable[c])
}
return dst
}
func (c *Collation_8bit_bin) ToUpper(dst, src []byte) []byte {
upperTable := c.simpletables.toupper
for _, c := range src {
dst = append(dst, upperTable[c])
}
return dst
}
type Collation_8bit_simple_ci struct {
id ID
name string
simpletables
charset charset.Charset
}
func (c *Collation_8bit_simple_ci) Init() {
if c.sort == nil {
panic("8bit_simple_ci collation without sort table")
}
}
func (c *Collation_8bit_simple_ci) Name() string {
return c.name
}
func (c *Collation_8bit_simple_ci) ID() ID {
return c.id
}
func (c *Collation_8bit_simple_ci) Charset() charset.Charset {
return c.charset
}
func (c *Collation_8bit_simple_ci) IsBinary() bool {
return false
}
func (c *Collation_8bit_simple_ci) Collate(left, right []byte, rightIsPrefix bool) int {
sortOrder := c.sort
cmpLen := minInt(len(left), len(right))
for i := 0; i < cmpLen; i++ {
sortL, sortR := sortOrder[left[i]], sortOrder[right[i]]
if sortL != sortR {
return int(sortL) - int(sortR)
}
}
if rightIsPrefix {
left = left[:cmpLen]
}
return len(left) - len(right)
}
func (c *Collation_8bit_simple_ci) WeightString(dst, src []byte, numCodepoints int) []byte {
padToMax := false
sortOrder := c.sort
copyCodepoints := len(src)
switch numCodepoints {
case 0:
numCodepoints = copyCodepoints
case PadToMax:
padToMax = true
default:
copyCodepoints = minInt(copyCodepoints, numCodepoints)
}
for _, ch := range src[:copyCodepoints] {
dst = append(dst, sortOrder[ch])
}
return weightStringPadingSimple(' ', dst, numCodepoints-copyCodepoints, padToMax)
}
func (c *Collation_8bit_simple_ci) Hash(src []byte, numCodepoints int) HashCode {
sortOrder := c.sort
var tocopy = len(src)
if numCodepoints > 0 {
tocopy = minInt(tocopy, numCodepoints)
}
var hash = uintptr(c.id)
for _, ch := range src[:tocopy] {
hash = memhash8(sortOrder[ch], hash)
}
if numCodepoints > 0 {
numCodepoints -= tocopy
for numCodepoints > 0 {
hash = memhash8(' ', hash)
numCodepoints--
}
}
return hash
}
func (c *Collation_8bit_simple_ci) WeightStringLen(numBytes int) int {
return numBytes
}
func (c *Collation_8bit_simple_ci) Wildcard(pat []byte, matchOne rune, matchMany rune, escape rune) WildcardPattern {
return newEightbitWildcardMatcher(c.sort, c.Collate, pat, matchOne, matchMany, escape)
}
func weightStringPadingSimple(padChar byte, dst []byte, numCodepoints int, padToMax bool) []byte {
if padToMax {
for len(dst) < cap(dst) {
dst = append(dst, padChar)
}
} else {
for numCodepoints > 0 {
dst = append(dst, padChar)
numCodepoints--
}
}
return dst
}
func (c *Collation_8bit_simple_ci) ToLower(dst, src []byte) []byte {
lowerTable := c.simpletables.tolower
for _, c := range src {
dst = append(dst, lowerTable[c])
}
return dst
}
func (c *Collation_8bit_simple_ci) ToUpper(dst, src []byte) []byte {
upperTable := c.simpletables.toupper
for _, c := range src {
dst = append(dst, upperTable[c])
}
return dst
}
type Collation_binary struct{}
func (c *Collation_binary) Init() {}
func (c *Collation_binary) ID() ID {
return CollationBinaryID
}
func (c *Collation_binary) Name() string {
return "binary"
}
func (c *Collation_binary) Charset() charset.Charset {
return charset.Charset_binary{}
}
func (c *Collation_binary) IsBinary() bool {
return true
}
func (c *Collation_binary) Collate(left, right []byte, isPrefix bool) int {
return collationBinary(left, right, isPrefix)
}
func (c *Collation_binary) WeightString(dst, src []byte, numCodepoints int) []byte {
padToMax := false
copyCodepoints := len(src)
switch numCodepoints {
case 0: // no-op
case PadToMax:
padToMax = true
default:
copyCodepoints = minInt(copyCodepoints, numCodepoints)
}
dst = append(dst, src[:copyCodepoints]...)
if padToMax {
for len(dst) < cap(dst) {
dst = append(dst, 0x0)
}
}
return dst
}
func (c *Collation_binary) Hash(src []byte, numCodepoints int) HashCode {
if numCodepoints > 0 {
src = src[:numCodepoints]
}
return memhash(src, 0xBBBBBBBB)
}
func (c *Collation_binary) WeightStringLen(numBytes int) int {
return numBytes
}
func (c *Collation_binary) Wildcard(pat []byte, matchOne rune, matchMany rune, escape rune) WildcardPattern {
return newEightbitWildcardMatcher(&sortOrderIdentity, c.Collate, pat, matchOne, matchMany, escape)
}
func (c *Collation_binary) ToLower(dst, raw []byte) []byte {
dst = append(dst, raw...)
return dst
}
func (c *Collation_binary) ToUpper(dst, raw []byte) []byte {
dst = append(dst, raw...)
return dst
}