Debian Codegraph @

internal/colltab/collelem.go

raw
1 // Copyright 2012 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
4
5 package colltab
6
7 import (
8 "fmt"
9 "unicode"
10 )
11
12 // Level identifies the collation comparison level.
13 // The primary level corresponds to the basic sorting of text.
14 // The secondary level corresponds to accents and related linguistic elements.
15 // The tertiary level corresponds to casing and related concepts.
16 // The quaternary level is derived from the other levels by the
17 // various algorithms for handling variable elements.
18 type Level int
19
20 const (
21 Primary Level = iota
22 Secondary
23 Tertiary
24 Quaternary
25 Identity
26
27 NumLevels
28 )
29
30 const (
31 defaultSecondary = 0x20
32 defaultTertiary = 0x2
33 maxTertiary = 0x1F
34 MaxQuaternary = 0x1FFFFF // 21 bits.
35 )
36
37 // Elem is a representation of a collation element. This API provides ways to encode
38 // and decode Elems. Implementations of collation tables may use values greater
39 // or equal to PrivateUse for their own purposes. However, these should never be
40 // returned by AppendNext.
41 type Elem uint32
42
43 const (
44 maxCE Elem = 0xAFFFFFFF
45 PrivateUse = minContract
46 minContract = 0xC0000000
47 maxContract = 0xDFFFFFFF
48 minExpand = 0xE0000000
49 maxExpand = 0xEFFFFFFF
50 minDecomp = 0xF0000000
51 )
52
53 type ceType int
54
55 const (
56 ceNormal ceType = iota // ceNormal includes implicits (ce == 0)
57 ceContractionIndex // rune can be a start of a contraction
58 ceExpansionIndex // rune expands into a sequence of collation elements
59 ceDecompose // rune expands using NFKC decomposition
60 )
61
62 func (ce Elem) ctype() ceType {
63 if ce <= maxCE {
64 return ceNormal
65 }
66 if ce <= maxContract {
67 return ceContractionIndex
68 } else {
69 if ce <= maxExpand {
70 return ceExpansionIndex
71 }
72 return ceDecompose
73 }
74 }
75
76 // For normal collation elements, we assume that a collation element either has
77 // a primary or non-default secondary value, not both.
78 // Collation elements with a primary value are of the form
79 //
80 // 01pppppp pppppppp ppppppp0 ssssssss
81 // - p* is primary collation value
82 // - s* is the secondary collation value
83 // 00pppppp pppppppp ppppppps sssttttt, where
84 // - p* is primary collation value
85 // - s* offset of secondary from default value.
86 // - t* is the tertiary collation value
87 // 100ttttt cccccccc pppppppp pppppppp
88 // - t* is the tertiar collation value
89 // - c* is the canonical combining class
90 // - p* is the primary collation value
91 //
92 // Collation elements with a secondary value are of the form
93 //
94 // 1010cccc ccccssss ssssssss tttttttt, where
95 // - c* is the canonical combining class
96 // - s* is the secondary collation value
97 // - t* is the tertiary collation value
98 // 11qqqqqq qqqqqqqq qqqqqqq0 00000000
99 // - q* quaternary value
100 const (
101 ceTypeMask = 0xC0000000
102 ceTypeMaskExt = 0xE0000000
103 ceIgnoreMask = 0xF00FFFFF
104 ceType1 = 0x40000000
105 ceType2 = 0x00000000
106 ceType3or4 = 0x80000000
107 ceType4 = 0xA0000000
108 ceTypeQ = 0xC0000000
109 Ignore = ceType4
110 firstNonPrimary = 0x80000000
111 lastSpecialPrimary = 0xA0000000
112 secondaryMask = 0x80000000
113 hasTertiaryMask = 0x40000000
114 primaryValueMask = 0x3FFFFE00
115 maxPrimaryBits = 21
116 compactPrimaryBits = 16
117 maxSecondaryBits = 12
118 maxTertiaryBits = 8
119 maxCCCBits = 8
120 maxSecondaryCompactBits = 8
121 maxSecondaryDiffBits = 4
122 maxTertiaryCompactBits = 5
123 primaryShift = 9
124 compactSecondaryShift = 5
125 minCompactSecondary = defaultSecondary - 4
126 )
127
128 func makeImplicitCE(primary int) Elem {
129 return ceType1 | Elem(primary<<primaryShift) | defaultSecondary
130 }
131
132 // MakeElem returns an Elem for the given values. It will return an error
133 // if the given combination of values is invalid.
134 func MakeElem(primary, secondary, tertiary int, ccc uint8) (Elem, error) {
135 if w := primary; w >= 1<<maxPrimaryBits || w < 0 {
136 return 0, fmt.Errorf("makeCE: primary weight out of bounds: %x >= %x", w, 1<<maxPrimaryBits)
137 }
138 if w := secondary; w >= 1<<maxSecondaryBits || w < 0 {
139 return 0, fmt.Errorf("makeCE: secondary weight out of bounds: %x >= %x", w, 1<<maxSecondaryBits)
140 }
141 if w := tertiary; w >= 1<<maxTertiaryBits || w < 0 {
142 return 0, fmt.Errorf("makeCE: tertiary weight out of bounds: %x >= %x", w, 1<<maxTertiaryBits)
143 }
144 ce := Elem(0)
145 if primary != 0 {
146 if ccc != 0 {
147 if primary >= 1<<compactPrimaryBits {
148 return 0, fmt.Errorf("makeCE: primary weight with non-zero CCC out of bounds: %x >= %x", primary, 1<<compactPrimaryBits)
149 }
150 if secondary != defaultSecondary {
151 return 0, fmt.Errorf("makeCE: cannot combine non-default secondary value (%x) with non-zero CCC (%x)", secondary, ccc)
152 }
153 ce = Elem(tertiary << (compactPrimaryBits + maxCCCBits))
154 ce |= Elem(ccc) << compactPrimaryBits
155 ce |= Elem(primary)
156 ce |= ceType3or4
157 } else if tertiary == defaultTertiary {
158 if secondary >= 1<<maxSecondaryCompactBits {
159 return 0, fmt.Errorf("makeCE: secondary weight with non-zero primary out of bounds: %x >= %x", secondary, 1<<maxSecondaryCompactBits)
160 }
161 ce = Elem(primary<<(maxSecondaryCompactBits+1) + secondary)
162 ce |= ceType1
163 } else {
164 d := secondary - defaultSecondary + maxSecondaryDiffBits
165 if d >= 1<<maxSecondaryDiffBits || d < 0 {
166 return 0, fmt.Errorf("makeCE: secondary weight diff out of bounds: %x < 0 || %x > %x", d, d, 1<<maxSecondaryDiffBits)
167 }
168 if tertiary >= 1<<maxTertiaryCompactBits {
169 return 0, fmt.Errorf("makeCE: tertiary weight with non-zero primary out of bounds: %x > %x", tertiary, 1<<maxTertiaryCompactBits)
170 }
171 ce = Elem(primary<<maxSecondaryDiffBits + d)
172 ce = ce<<maxTertiaryCompactBits + Elem(tertiary)
173 }
174 } else {
175 ce = Elem(secondary<<maxTertiaryBits + tertiary)
176 ce += Elem(ccc) << (maxSecondaryBits + maxTertiaryBits)
177 ce |= ceType4
178 }
179 return ce, nil
180 }
181
182 // MakeQuaternary returns an Elem with the given quaternary value.
183 func MakeQuaternary(v int) Elem {
184 return ceTypeQ | Elem(v<<primaryShift)
185 }
186
187 // Mask sets weights for any level smaller than l to 0.
188 // The resulting Elem can be used to test for equality with
189 // other Elems to which the same mask has been applied.
190 func (ce Elem) Mask(l Level) uint32 {
191 return 0
192 }
193
194 // CCC returns the canonical combining class associated with the underlying character,
195 // if applicable, or 0 otherwise.
196 func (ce Elem) CCC() uint8 {
197 if ce&ceType3or4 != 0 {
198 if ce&ceType4 == ceType3or4 {
199 return uint8(ce >> 16)
200 }
201 return uint8(ce >> 20)
202 }
203 return 0
204 }
205
206 // Primary returns the primary collation weight for ce.
207 func (ce Elem) Primary() int {
208 if ce >= firstNonPrimary {
209 if ce > lastSpecialPrimary {
210 return 0
211 }
212 return int(uint16(ce))
213 }
214 return int(ce&primaryValueMask) >> primaryShift
215 }
216
217 // Secondary returns the secondary collation weight for ce.
218 func (ce Elem) Secondary() int {
219 switch ce & ceTypeMask {
220 case ceType1:
221 return int(uint8(ce))
222 case ceType2:
223 return minCompactSecondary + int((ce>>compactSecondaryShift)&0xF)
224 case ceType3or4:
225 if ce < ceType4 {
226 return defaultSecondary
227 }
228 return int(ce>>8) & 0xFFF
229 case ceTypeQ:
230 return 0
231 }
232 panic("should not reach here")
233 }
234
235 // Tertiary returns the tertiary collation weight for ce.
236 func (ce Elem) Tertiary() uint8 {
237 if ce&hasTertiaryMask == 0 {
238 if ce&ceType3or4 == 0 {
239 return uint8(ce & 0x1F)
240 }
241 if ce&ceType4 == ceType4 {
242 return uint8(ce)
243 }
244 return uint8(ce>>24) & 0x1F // type 2
245 } else if ce&ceTypeMask == ceType1 {
246 return defaultTertiary
247 }
248 // ce is a quaternary value.
249 return 0
250 }
251
252 func (ce Elem) updateTertiary(t uint8) Elem {
253 if ce&ceTypeMask == ceType1 {
254 // convert to type 4
255 nce := ce & primaryValueMask
256 nce |= Elem(uint8(ce)-minCompactSecondary) << compactSecondaryShift
257 ce = nce
258 } else if ce&ceTypeMaskExt == ceType3or4 {
259 ce &= ^Elem(maxTertiary << 24)
260 return ce | (Elem(t) << 24)
261 } else {
262 // type 2 or 4
263 ce &= ^Elem(maxTertiary)
264 }
265 return ce | Elem(t)
266 }
267
268 // Quaternary returns the quaternary value if explicitly specified,
269 // 0 if ce == Ignore, or MaxQuaternary otherwise.
270 // Quaternary values are used only for shifted variants.
271 func (ce Elem) Quaternary() int {
272 if ce&ceTypeMask == ceTypeQ {
273 return int(ce&primaryValueMask) >> primaryShift
274 } else if ce&ceIgnoreMask == Ignore {
275 return 0
276 }
277 return MaxQuaternary
278 }
279
280 // Weight returns the collation weight for the given level.
281 func (ce Elem) Weight(l Level) int {
282 switch l {
283 case Primary:
284 return ce.Primary()
285 case Secondary:
286 return ce.Secondary()
287 case Tertiary:
288 return int(ce.Tertiary())
289 case Quaternary:
290 return ce.Quaternary()
291 }
292 return 0 // return 0 (ignore) for undefined levels.
293 }
294
295 // For contractions, collation elements are of the form
296 // 110bbbbb bbbbbbbb iiiiiiii iiiinnnn, where
297 // - n* is the size of the first node in the contraction trie.
298 // - i* is the index of the first node in the contraction trie.
299 // - b* is the offset into the contraction collation element table.
300 //
301 // See contract.go for details on the contraction trie.
302 const (
303 maxNBits = 4
304 maxTrieIndexBits = 12
305 maxContractOffsetBits = 13
306 )
307
308 func splitContractIndex(ce Elem) (index, n, offset int) {
309 n = int(ce & (1<<maxNBits - 1))
310 ce >>= maxNBits
311 index = int(ce & (1<<maxTrieIndexBits - 1))
312 ce >>= maxTrieIndexBits
313 offset = int(ce & (1<<maxContractOffsetBits - 1))
314 return
315 }
316
317 // For expansions, Elems are of the form 11100000 00000000 bbbbbbbb bbbbbbbb,
318 // where b* is the index into the expansion sequence table.
319 const maxExpandIndexBits = 16
320
321 func splitExpandIndex(ce Elem) (index int) {
322 return int(uint16(ce))
323 }
324
325 // Some runes can be expanded using NFKD decomposition. Instead of storing the full
326 // sequence of collation elements, we decompose the rune and lookup the collation
327 // elements for each rune in the decomposition and modify the tertiary weights.
328 // The Elem, in this case, is of the form 11110000 00000000 wwwwwwww vvvvvvvv, where
329 // - v* is the replacement tertiary weight for the first rune,
330 // - w* is the replacement tertiary weight for the second rune,
331 //
332 // Tertiary weights of subsequent runes should be replaced with maxTertiary.
333 // See https://www.unicode.org/reports/tr10/#Compatibility_Decompositions for more details.
334 func splitDecompose(ce Elem) (t1, t2 uint8) {
335 return uint8(ce), uint8(ce >> 8)
336 }
337
338 const (
339 // These constants were taken from https://www.unicode.org/versions/Unicode6.0.0/ch12.pdf.
340 minUnified rune = 0x4E00
341 maxUnified = 0x9FFF
342 minCompatibility = 0xF900
343 maxCompatibility = 0xFAFF
344 minRare = 0x3400
345 maxRare = 0x4DBF
346 )
347 const (
348 commonUnifiedOffset = 0x10000
349 rareUnifiedOffset = 0x20000 // largest rune in common is U+FAFF
350 otherOffset = 0x50000 // largest rune in rare is U+2FA1D
351 illegalOffset = otherOffset + int(unicode.MaxRune)
352 maxPrimary = illegalOffset + 1
353 )
354
355 // implicitPrimary returns the primary weight for the given rune
356 // for which there is no entry for the rune in the collation table.
357 // We take a different approach from the one specified in
358 // https://unicode.org/reports/tr10/#Implicit_Weights,
359 // but preserve the resulting relative ordering of the runes.
360 func implicitPrimary(r rune) int {
361 if unicode.Is(unicode.Ideographic, r) {
362 if r >= minUnified && r <= maxUnified {
363 // The most common case for CJK.
364 return int(r) + commonUnifiedOffset
365 }
366 if r >= minCompatibility && r <= maxCompatibility {
367 // This will typically not hit. The DUCET explicitly specifies mappings
368 // for all characters that do not decompose.
369 return int(r) + commonUnifiedOffset
370 }
371 return int(r) + rareUnifiedOffset
372 }
373 return int(r) + otherOffset
374 }