|
| 1 | +// Copyright 2024 PingCAP, Inc. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package collate |
| 16 | + |
| 17 | +import ( |
| 18 | + _ "embed" |
| 19 | + "encoding/binary" |
| 20 | + "unicode/utf8" |
| 21 | + |
| 22 | + "github.com/pingcap/tidb/pkg/util/stringutil" |
| 23 | +) |
| 24 | + |
| 25 | +//go:embed gb18030_weight.data |
| 26 | +var gb18030WeightData []byte |
| 27 | + |
| 28 | +const ( |
| 29 | + // Unicode code points up to U+10FFFF can be encoded as GB18030. |
| 30 | + gb18030MaxCodePoint = 0x10FFFF |
| 31 | +) |
| 32 | + |
| 33 | +type gb18030ChineseCICollator struct { |
| 34 | +} |
| 35 | + |
| 36 | +// Clone implements Collator interface. |
| 37 | +func (*gb18030ChineseCICollator) Clone() Collator { |
| 38 | + return new(gb18030ChineseCICollator) |
| 39 | +} |
| 40 | + |
| 41 | +// Compare implements Collator interface. |
| 42 | +func (*gb18030ChineseCICollator) Compare(a, b string) int { |
| 43 | + return compareCommon(a, b, gb18030ChineseCISortKey) |
| 44 | +} |
| 45 | + |
| 46 | +// Key implements Collator interface. |
| 47 | +func (g *gb18030ChineseCICollator) Key(str string) []byte { |
| 48 | + return g.KeyWithoutTrimRightSpace(truncateTailingSpace(str)) |
| 49 | +} |
| 50 | + |
| 51 | +// ImmutableKey implement Collator interface. |
| 52 | +func (g *gb18030ChineseCICollator) ImmutableKey(str string) []byte { |
| 53 | + return g.KeyWithoutTrimRightSpace(truncateTailingSpace(str)) |
| 54 | +} |
| 55 | + |
| 56 | +// KeyWithoutTrimRightSpace implement Collator interface. |
| 57 | +func (*gb18030ChineseCICollator) KeyWithoutTrimRightSpace(str string) []byte { |
| 58 | + buf := make([]byte, 0, len(str)*2) |
| 59 | + i, rLen := 0, 0 |
| 60 | + r := rune(0) |
| 61 | + for i < len(str) { |
| 62 | + // When the byte sequence is not a valid UTF-8 encoding of a rune, Golang returns RuneError('�') and size 1. |
| 63 | + // See https://pkg.go.dev/unicode/utf8#DecodeRune for more details. |
| 64 | + // Here we check both the size and rune to distinguish between invalid byte sequence and valid '�'. |
| 65 | + r, rLen = utf8.DecodeRuneInString(str[i:]) |
| 66 | + invalid := r == utf8.RuneError && rLen == 1 |
| 67 | + if invalid { |
| 68 | + return buf |
| 69 | + } |
| 70 | + |
| 71 | + i = i + rLen |
| 72 | + u32 := gb18030ChineseCISortKey(r) |
| 73 | + if u32 > 0xFFFFFF { |
| 74 | + buf = append(buf, byte(u32>>24)) |
| 75 | + } |
| 76 | + if u32 > 0xFFFF { |
| 77 | + buf = append(buf, byte(u32>>16)) |
| 78 | + } |
| 79 | + if u32 > 0xFF { |
| 80 | + buf = append(buf, byte(u32>>8)) |
| 81 | + } |
| 82 | + buf = append(buf, byte(u32)) |
| 83 | + } |
| 84 | + return buf |
| 85 | +} |
| 86 | + |
| 87 | +// Pattern implements Collator interface. |
| 88 | +func (*gb18030ChineseCICollator) Pattern() WildcardPattern { |
| 89 | + return &gb18030ChineseCIPattern{} |
| 90 | +} |
| 91 | + |
| 92 | +type gb18030ChineseCIPattern struct { |
| 93 | + patChars []rune |
| 94 | + patTypes []byte |
| 95 | +} |
| 96 | + |
| 97 | +// Compile implements WildcardPattern interface. |
| 98 | +func (p *gb18030ChineseCIPattern) Compile(patternStr string, escape byte) { |
| 99 | + p.patChars, p.patTypes = stringutil.CompilePatternInner(patternStr, escape) |
| 100 | +} |
| 101 | + |
| 102 | +// DoMatch implements WildcardPattern interface. |
| 103 | +func (p *gb18030ChineseCIPattern) DoMatch(str string) bool { |
| 104 | + return stringutil.DoMatchCustomized(str, p.patChars, p.patTypes, func(a, b rune) bool { |
| 105 | + return gb18030ChineseCISortKey(a) == gb18030ChineseCISortKey(b) |
| 106 | + }) |
| 107 | +} |
| 108 | + |
| 109 | +func gb18030ChineseCISortKey(r rune) uint32 { |
| 110 | + if r > gb18030MaxCodePoint { |
| 111 | + return 0x3F |
| 112 | + } |
| 113 | + |
| 114 | + return binary.LittleEndian.Uint32(gb18030WeightData[4*r : 4*r+4]) |
| 115 | +} |
0 commit comments