forked from pingcap/tidb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathunicode_ci.go
155 lines (136 loc) · 3.63 KB
/
unicode_ci.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
// Copyright 2020 PingCAP, Inc.
//
// 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 collate
import (
"github.com/pingcap/tidb/util/stringutil"
)
const (
// magic number indicate weight has 2 uint64, should get from `longRuneMap`
longRune uint64 = 0xFFFD
)
// unicodeCICollator implements UCA. see http://unicode.org/reports/tr10/
type unicodeCICollator struct {
}
// Compare implements Collator interface.
func (uc *unicodeCICollator) Compare(a, b string) int {
a = truncateTailingSpace(a)
b = truncateTailingSpace(b)
// weight of a, b. weight in unicode_ci may has 8 uint16s. xn indicate first 4 u16s, xs indicate last 4 u16s
an, bn := uint64(0), uint64(0)
as, bs := uint64(0), uint64(0)
// rune of a, b
ar, br := rune(0), rune(0)
// decode index of a, b
ai, bi := 0, 0
for {
if an == 0 {
if as == 0 {
for an == 0 && ai < len(a) {
ar, ai = decodeRune(a, ai)
an, as = convertRuneUnicodeCI(ar)
}
} else {
an = as
as = 0
}
}
if bn == 0 {
if bs == 0 {
for bn == 0 && bi < len(b) {
br, bi = decodeRune(b, bi)
bn, bs = convertRuneUnicodeCI(br)
}
} else {
bn = bs
bs = 0
}
}
if an == 0 || bn == 0 {
return sign(int(an) - int(bn))
}
if an == bn {
an, bn = 0, 0
continue
}
for an != 0 && bn != 0 {
if (an^bn)&0xFFFF == 0 {
an >>= 16
bn >>= 16
} else {
return sign(int(an&0xFFFF) - int(bn&0xFFFF))
}
}
}
}
// Key implements Collator interface.
func (uc *unicodeCICollator) Key(str string) []byte {
str = truncateTailingSpace(str)
buf := make([]byte, 0, len(str)*2)
r := rune(0)
si := 0 // decode index of s
sn, ss := uint64(0), uint64(0) // weight of str. weight in unicode_ci may has 8 uint16s. sn indicate first 4 u16s, ss indicate last 4 u16s
for si < len(str) {
r, si = decodeRune(str, si)
sn, ss = convertRuneUnicodeCI(r)
for sn != 0 {
buf = append(buf, byte((sn&0xFF00)>>8), byte(sn))
sn >>= 16
}
for ss != 0 {
buf = append(buf, byte((ss&0xFF00)>>8), byte(ss))
ss >>= 16
}
}
return buf
}
// convert rune to weights.
// `first` represent first 4 uint16 weights of rune
// `second` represent last 4 uint16 weights of rune if exist, 0 if not
func convertRuneUnicodeCI(r rune) (first, second uint64) {
if r > 0xFFFF {
return 0xFFFD, 0
}
if mapTable[r] == longRune {
return longRuneMap[r][0], longRuneMap[r][1]
}
return mapTable[r], 0
}
// Pattern implements Collator interface.
func (uc *unicodeCICollator) Pattern() WildcardPattern {
return &unicodePattern{}
}
type unicodePattern struct {
patChars []rune
patTypes []byte
}
// Compile implements WildcardPattern interface.
func (p *unicodePattern) Compile(patternStr string, escape byte) {
p.patChars, p.patTypes = stringutil.CompilePatternInner(patternStr, escape)
}
// DoMatch implements WildcardPattern interface.
func (p *unicodePattern) DoMatch(str string) bool {
return stringutil.DoMatchInner(str, p.patChars, p.patTypes, func(a, b rune) bool {
if a > 0xFFFF || b > 0xFFFF {
return a == b
}
ar, br := mapTable[a], mapTable[b]
if ar != br {
return false
}
if ar == longRune {
return a == b
}
return true
})
}