Debian Codegraph @

intl/icu/source/i18n/collation.h

raw
1 // © 2016 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
3 /*
4 *******************************************************************************
5 * Copyright (C) 2010-2015, International Business Machines
6 * Corporation and others. All Rights Reserved.
7 *******************************************************************************
8 * collation.h
9 *
10 * created on: 2010oct27
11 * created by: Markus W. Scherer
12 */
13
14 #ifndef __COLLATION_H__
15 #define __COLLATION_H__
16
17 #include "unicode/utypes.h"
18
19 #if !UCONFIG_NO_COLLATION
20
21 U_NAMESPACE_BEGIN
22
23 /**
24 * Collation v2 basic definitions and static helper functions.
25 *
26 * Data structures except for expansion tables store 32-bit CEs which are
27 * either specials (see tags below) or are compact forms of 64-bit CEs.
28 */
29 class U_I18N_API Collation {
30 public:
31 // Special sort key bytes for all levels.
32 static const uint8_t TERMINATOR_BYTE = 0;
33 static const uint8_t LEVEL_SEPARATOR_BYTE = 1;
34
35 /** The secondary/tertiary lower limit for tailoring before any root elements. */
36 static const uint32_t BEFORE_WEIGHT16 = 0x0100;
37
38 /**
39 * Merge-sort-key separator.
40 * Same as the unique primary and identical-level weights of U+FFFE.
41 * Must not be used as primary compression low terminator.
42 * Otherwise usable.
43 */
44 static const uint8_t MERGE_SEPARATOR_BYTE = 2;
45 static const uint32_t MERGE_SEPARATOR_PRIMARY = 0x02000000; // U+FFFE
46 static const uint32_t MERGE_SEPARATOR_CE32 = 0x02000505; // U+FFFE
47
48 /**
49 * Primary compression low terminator, must be greater than MERGE_SEPARATOR_BYTE.
50 * Reserved value in primary second byte if the lead byte is compressible.
51 * Otherwise usable in all CE weight bytes.
52 */
53 static const uint8_t PRIMARY_COMPRESSION_LOW_BYTE = 3;
54 /**
55 * Primary compression high terminator.
56 * Reserved value in primary second byte if the lead byte is compressible.
57 * Otherwise usable in all CE weight bytes.
58 */
59 static const uint8_t PRIMARY_COMPRESSION_HIGH_BYTE = 0xff;
60
61 /** Default secondary/tertiary weight lead byte. */
62 static const uint8_t COMMON_BYTE = 5;
63 static const uint32_t COMMON_WEIGHT16 = 0x0500;
64 /** Middle 16 bits of a CE with a common secondary weight. */
65 static const uint32_t COMMON_SECONDARY_CE = 0x05000000;
66 /** Lower 16 bits of a CE with a common tertiary weight. */
67 static const uint32_t COMMON_TERTIARY_CE = 0x0500;
68 /** Lower 32 bits of a CE with common secondary and tertiary weights. */
69 static const uint32_t COMMON_SEC_AND_TER_CE = 0x05000500;
70
71 static const uint32_t SECONDARY_MASK = 0xffff0000;
72 static const uint32_t CASE_MASK = 0xc000;
73 static const uint32_t SECONDARY_AND_CASE_MASK = SECONDARY_MASK | CASE_MASK;
74 /** Only the 2*6 bits for the pure tertiary weight. */
75 static const uint32_t ONLY_TERTIARY_MASK = 0x3f3f;
76 /** Only the secondary & tertiary bits; no case, no quaternary. */
77 static const uint32_t ONLY_SEC_TER_MASK = SECONDARY_MASK | ONLY_TERTIARY_MASK;
78 /** Case bits and tertiary bits. */
79 static const uint32_t CASE_AND_TERTIARY_MASK = CASE_MASK | ONLY_TERTIARY_MASK;
80 static const uint32_t QUATERNARY_MASK = 0xc0;
81 /** Case bits and quaternary bits. */
82 static const uint32_t CASE_AND_QUATERNARY_MASK = CASE_MASK | QUATERNARY_MASK;
83
84 static const uint8_t UNASSIGNED_IMPLICIT_BYTE = 0xfe; // compressible
85 /**
86 * First unassigned: AlphabeticIndex overflow boundary.
87 * We want a 3-byte primary so that it fits into the root elements table.
88 *
89 * This 3-byte primary will not collide with
90 * any unassigned-implicit 4-byte primaries because
91 * the first few hundred Unicode code points all have real mappings.
92 */
93 static const uint32_t FIRST_UNASSIGNED_PRIMARY = 0xfe040200;
94
95 static const uint8_t TRAIL_WEIGHT_BYTE = 0xff; // not compressible
96 static const uint32_t FIRST_TRAILING_PRIMARY = 0xff020200; // [first trailing]
97 static const uint32_t MAX_PRIMARY = 0xffff0000; // U+FFFF
98 static const uint32_t MAX_REGULAR_CE32 = 0xffff0505; // U+FFFF
99
100 // CE32 value for U+FFFD as well as illegal UTF-8 byte sequences (which behave like U+FFFD).
101 // We use the third-highest primary weight for U+FFFD (as in UCA 6.3+).
102 static const uint32_t FFFD_PRIMARY = MAX_PRIMARY - 0x20000;
103 static const uint32_t FFFD_CE32 = MAX_REGULAR_CE32 - 0x20000;
104
105 /**
106 * A CE32 is special if its low byte is this or greater.
107 * Impossible case bits 11 mark special CE32s.
108 * This value itself is used to indicate a fallback to the base collator.
109 */
110 static const uint8_t SPECIAL_CE32_LOW_BYTE = 0xc0;
111 static const uint32_t FALLBACK_CE32 = SPECIAL_CE32_LOW_BYTE;
112 /**
113 * Low byte of a long-primary special CE32.
114 */
115 static const uint8_t LONG_PRIMARY_CE32_LOW_BYTE = 0xc1; // SPECIAL_CE32_LOW_BYTE | LONG_PRIMARY_TAG
116
117 static const uint32_t UNASSIGNED_CE32 = 0xffffffff; // Compute an unassigned-implicit CE.
118
119 static const uint32_t NO_CE32 = 1;
120
121 /** No CE: End of input. Only used in runtime code, not stored in data. */
122 static const uint32_t NO_CE_PRIMARY = 1; // not a left-adjusted weight
123 static const uint32_t NO_CE_WEIGHT16 = 0x0100; // weight of LEVEL_SEPARATOR_BYTE
124 static const int64_t NO_CE = INT64_C(0x101000100); // NO_CE_PRIMARY, NO_CE_WEIGHT16, NO_CE_WEIGHT16
125
126 /** Sort key levels. */
127 enum Level {
128 /** Unspecified level. */
129 NO_LEVEL,
130 PRIMARY_LEVEL,
131 SECONDARY_LEVEL,
132 CASE_LEVEL,
133 TERTIARY_LEVEL,
134 QUATERNARY_LEVEL,
135 IDENTICAL_LEVEL,
136 /** Beyond sort key bytes. */
137 ZERO_LEVEL
138 };
139
140 /**
141 * Sort key level flags: xx_FLAG = 1 << xx_LEVEL.
142 * In Java, use enum Level with flag() getters, or use EnumSet rather than hand-made bit sets.
143 */
144 static const uint32_t NO_LEVEL_FLAG = 1;
145 static const uint32_t PRIMARY_LEVEL_FLAG = 2;
146 static const uint32_t SECONDARY_LEVEL_FLAG = 4;
147 static const uint32_t CASE_LEVEL_FLAG = 8;
148 static const uint32_t TERTIARY_LEVEL_FLAG = 0x10;
149 static const uint32_t QUATERNARY_LEVEL_FLAG = 0x20;
150 static const uint32_t IDENTICAL_LEVEL_FLAG = 0x40;
151 static const uint32_t ZERO_LEVEL_FLAG = 0x80;
152
153 /**
154 * Special-CE32 tags, from bits 3..0 of a special 32-bit CE.
155 * Bits 31..8 are available for tag-specific data.
156 * Bits 5..4: Reserved. May be used in the future to indicate lccc!=0 and tccc!=0.
157 */
158 enum {
159 /**
160 * Fall back to the base collator.
161 * This is the tag value in SPECIAL_CE32_LOW_BYTE and FALLBACK_CE32.
162 * Bits 31..8: Unused, 0.
163 */
164 FALLBACK_TAG = 0,
165 /**
166 * Long-primary CE with COMMON_SEC_AND_TER_CE.
167 * Bits 31..8: Three-byte primary.
168 */
169 LONG_PRIMARY_TAG = 1,
170 /**
171 * Long-secondary CE with zero primary.
172 * Bits 31..16: Secondary weight.
173 * Bits 15.. 8: Tertiary weight.
174 */
175 LONG_SECONDARY_TAG = 2,
176 /**
177 * Unused.
178 * May be used in the future for single-byte secondary CEs (SHORT_SECONDARY_TAG),
179 * storing the secondary in bits 31..24, the ccc in bits 23..16,
180 * and the tertiary in bits 15..8.
181 */
182 RESERVED_TAG_3 = 3,
183 /**
184 * Latin mini expansions of two simple CEs [pp, 05, tt] [00, ss, 05].
185 * Bits 31..24: Single-byte primary weight pp of the first CE.
186 * Bits 23..16: Tertiary weight tt of the first CE.
187 * Bits 15.. 8: Secondary weight ss of the second CE.
188 */
189 LATIN_EXPANSION_TAG = 4,
190 /**
191 * Points to one or more simple/long-primary/long-secondary 32-bit CE32s.
192 * Bits 31..13: Index into uint32_t table.
193 * Bits 12.. 8: Length=1..31.
194 */
195 EXPANSION32_TAG = 5,
196 /**
197 * Points to one or more 64-bit CEs.
198 * Bits 31..13: Index into CE table.
199 * Bits 12.. 8: Length=1..31.
200 */
201 EXPANSION_TAG = 6,
202 /**
203 * Builder data, used only in the CollationDataBuilder, not in runtime data.
204 *
205 * If bit 8 is 0: Builder context, points to a list of context-sensitive mappings.
206 * Bits 31..13: Index to the builder's list of ConditionalCE32 for this character.
207 * Bits 12.. 9: Unused, 0.
208 *
209 * If bit 8 is 1 (IS_BUILDER_JAMO_CE32): Builder-only jamoCE32 value.
210 * The builder fetches the Jamo CE32 from the trie.
211 * Bits 31..13: Jamo code point.
212 * Bits 12.. 9: Unused, 0.
213 */
214 BUILDER_DATA_TAG = 7,
215 /**
216 * Points to prefix trie.
217 * Bits 31..13: Index into prefix/contraction data.
218 * Bits 12.. 8: Unused, 0.
219 */
220 PREFIX_TAG = 8,
221 /**
222 * Points to contraction data.
223 * Bits 31..13: Index into prefix/contraction data.
224 * Bit 12: Unused, 0.
225 * Bit 11: CONTRACT_HAS_STARTER flag. (Used by ICU4X only.)
226 * Bit 10: CONTRACT_TRAILING_CCC flag.
227 * Bit 9: CONTRACT_NEXT_CCC flag.
228 * Bit 8: CONTRACT_SINGLE_CP_NO_MATCH flag.
229 */
230 CONTRACTION_TAG = 9,
231 /**
232 * Decimal digit.
233 * Bits 31..13: Index into uint32_t table for non-numeric-collation CE32.
234 * Bit 12: Unused, 0.
235 * Bits 11.. 8: Digit value 0..9.
236 */
237 DIGIT_TAG = 10,
238 /**
239 * Tag for U+0000, for moving the NUL-termination handling
240 * from the regular fastpath into specials-handling code.
241 * Bits 31..8: Unused, 0.
242 */
243 U0000_TAG = 11,
244 /**
245 * Tag for a Hangul syllable.
246 * Bits 31..9: Unused, 0.
247 * Bit 8: HANGUL_NO_SPECIAL_JAMO flag.
248 */
249 HANGUL_TAG = 12,
250 /**
251 * Tag for a lead surrogate code unit.
252 * Optional optimization for UTF-16 string processing.
253 * Bits 31..10: Unused, 0.
254 * 9.. 8: =0: All associated supplementary code points are unassigned-implicit.
255 * =1: All associated supplementary code points fall back to the base data.
256 * else: (Normally 2) Look up the data for the supplementary code point.
257 */
258 LEAD_SURROGATE_TAG = 13,
259 /**
260 * Tag for CEs with primary weights in code point order.
261 * Bits 31..13: Index into CE table, for one data "CE".
262 * Bits 12.. 8: Unused, 0.
263 *
264 * This data "CE" has the following bit fields:
265 * Bits 63..32: Three-byte primary pppppp00.
266 * 31.. 8: Start/base code point of the in-order range.
267 * 7: Flag isCompressible primary.
268 * 6.. 0: Per-code point primary-weight increment.
269 */
270 OFFSET_TAG = 14,
271 /**
272 * Implicit CE tag. Compute an unassigned-implicit CE.
273 * All bits are set (UNASSIGNED_CE32=0xffffffff).
274 */
275 IMPLICIT_TAG = 15
276 };
277
278 static UBool isAssignedCE32(uint32_t ce32) {
279 return ce32 != FALLBACK_CE32 && ce32 != UNASSIGNED_CE32;
280 }
281
282 /**
283 * We limit the number of CEs in an expansion
284 * so that we can use a small number of length bits in the data structure,
285 * and so that an implementation can copy CEs at runtime without growing a destination buffer.
286 */
287 static const int32_t MAX_EXPANSION_LENGTH = 31;
288 static const int32_t MAX_INDEX = 0x7ffff;
289
290 /**
291 * Set if there is no match for the single (no-suffix) character itself.
292 * This is only possible if there is a prefix.
293 * In this case, discontiguous contraction matching cannot add combining marks
294 * starting from an empty suffix.
295 * The default CE32 is used anyway if there is no suffix match.
296 */
297 static const uint32_t CONTRACT_SINGLE_CP_NO_MATCH = 0x100;
298 /** Set if the first character of every contraction suffix has lccc!=0. */
299 static const uint32_t CONTRACT_NEXT_CCC = 0x200;
300 /** Set if any contraction suffix ends with lccc!=0. */
301 static const uint32_t CONTRACT_TRAILING_CCC = 0x400;
302 /** Set if any contraction suffix contains a starter. (Used by ICU4X only.) */
303 static const uint32_t CONTRACT_HAS_STARTER = 0x800;
304
305 /** For HANGUL_TAG: None of its Jamo CE32s isSpecialCE32(). */
306 static const uint32_t HANGUL_NO_SPECIAL_JAMO = 0x100;
307
308 static const uint32_t LEAD_ALL_UNASSIGNED = 0;
309 static const uint32_t LEAD_ALL_FALLBACK = 0x100;
310 static const uint32_t LEAD_MIXED = 0x200;
311 static const uint32_t LEAD_TYPE_MASK = 0x300;
312
313 static uint32_t makeLongPrimaryCE32(uint32_t p) { return p | LONG_PRIMARY_CE32_LOW_BYTE; }
314
315 /** Turns the long-primary CE32 into a primary weight pppppp00. */
316 static inline uint32_t primaryFromLongPrimaryCE32(uint32_t ce32) {
317 return ce32 & 0xffffff00;
318 }
319 static inline int64_t ceFromLongPrimaryCE32(uint32_t ce32) {
320 return (static_cast<int64_t>(ce32 & 0xffffff00) << 32) | COMMON_SEC_AND_TER_CE;
321 }
322
323 static uint32_t makeLongSecondaryCE32(uint32_t lower32) {
324 return lower32 | SPECIAL_CE32_LOW_BYTE | LONG_SECONDARY_TAG;
325 }
326 static inline int64_t ceFromLongSecondaryCE32(uint32_t ce32) {
327 return ce32 & 0xffffff00;
328 }
329
330 /** Makes a special CE32 with tag, index and length. */
331 static uint32_t makeCE32FromTagIndexAndLength(int32_t tag, int32_t index, int32_t length) {
332 return (index << 13) | (length << 8) | SPECIAL_CE32_LOW_BYTE | tag;
333 }
334 /** Makes a special CE32 with only tag and index. */
335 static uint32_t makeCE32FromTagAndIndex(int32_t tag, int32_t index) {
336 return (index << 13) | SPECIAL_CE32_LOW_BYTE | tag;
337 }
338
339 static inline UBool isSpecialCE32(uint32_t ce32) {
340 return (ce32 & 0xff) >= SPECIAL_CE32_LOW_BYTE;
341 }
342
343 static inline int32_t tagFromCE32(uint32_t ce32) {
344 return static_cast<int32_t>(ce32 & 0xf);
345 }
346
347 static inline UBool hasCE32Tag(uint32_t ce32, int32_t tag) {
348 return isSpecialCE32(ce32) && tagFromCE32(ce32) == tag;
349 }
350
351 static inline UBool isLongPrimaryCE32(uint32_t ce32) {
352 return hasCE32Tag(ce32, LONG_PRIMARY_TAG);
353 }
354
355 static UBool isSimpleOrLongCE32(uint32_t ce32) {
356 return !isSpecialCE32(ce32) ||
357 tagFromCE32(ce32) == LONG_PRIMARY_TAG ||
358 tagFromCE32(ce32) == LONG_SECONDARY_TAG;
359 }
360
361 /**
362 * @return true if the ce32 yields one or more CEs without further data lookups
363 */
364 static UBool isSelfContainedCE32(uint32_t ce32) {
365 return !isSpecialCE32(ce32) ||
366 tagFromCE32(ce32) == LONG_PRIMARY_TAG ||
367 tagFromCE32(ce32) == LONG_SECONDARY_TAG ||
368 tagFromCE32(ce32) == LATIN_EXPANSION_TAG;
369 }
370
371 static inline UBool isPrefixCE32(uint32_t ce32) {
372 return hasCE32Tag(ce32, PREFIX_TAG);
373 }
374
375 static inline UBool isContractionCE32(uint32_t ce32) {
376 return hasCE32Tag(ce32, CONTRACTION_TAG);
377 }
378
379 static inline UBool ce32HasContext(uint32_t ce32) {
380 return isSpecialCE32(ce32) &&
381 (tagFromCE32(ce32) == PREFIX_TAG ||
382 tagFromCE32(ce32) == CONTRACTION_TAG);
383 }
384
385 /**
386 * Get the first of the two Latin-expansion CEs encoded in ce32.
387 * @see LATIN_EXPANSION_TAG
388 */
389 static inline int64_t latinCE0FromCE32(uint32_t ce32) {
390 return (static_cast<int64_t>(ce32 & 0xff000000) << 32) | COMMON_SECONDARY_CE | ((ce32 & 0xff0000) >> 8);
391 }
392
393 /**
394 * Get the second of the two Latin-expansion CEs encoded in ce32.
395 * @see LATIN_EXPANSION_TAG
396 */
397 static inline int64_t latinCE1FromCE32(uint32_t ce32) {
398 return ((ce32 & 0xff00) << 16) | COMMON_TERTIARY_CE;
399 }
400
401 /**
402 * Returns the data index from a special CE32.
403 */
404 static inline int32_t indexFromCE32(uint32_t ce32) {
405 return static_cast<int32_t>(ce32 >> 13);
406 }
407
408 /**
409 * Returns the data length from a ce32.
410 */
411 static inline int32_t lengthFromCE32(uint32_t ce32) {
412 return (ce32 >> 8) & 31;
413 }
414
415 /**
416 * Returns the digit value from a DIGIT_TAG ce32.
417 */
418 static inline char digitFromCE32(uint32_t ce32) {
419 return static_cast<char>((ce32 >> 8) & 0xf);
420 }
421
422 /** Returns a 64-bit CE from a simple CE32 (not special). */
423 static inline int64_t ceFromSimpleCE32(uint32_t ce32) {
424 // normal form ppppsstt -> pppp0000ss00tt00
425 // assert (ce32 & 0xff) < SPECIAL_CE32_LOW_BYTE
426 return (static_cast<int64_t>(ce32 & 0xffff0000) << 32) | ((ce32 & 0xff00) << 16) | ((ce32 & 0xff) << 8);
427 }
428
429 /** Returns a 64-bit CE from a simple/long-primary/long-secondary CE32. */
430 static inline int64_t ceFromCE32(uint32_t ce32) {
431 uint32_t tertiary = ce32 & 0xff;
432 if(tertiary < SPECIAL_CE32_LOW_BYTE) {
433 // normal form ppppsstt -> pppp0000ss00tt00
434 return (static_cast<int64_t>(ce32 & 0xffff0000) << 32) | ((ce32 & 0xff00) << 16) | (tertiary << 8);
435 } else {
436 ce32 -= tertiary;
437 if((tertiary & 0xf) == LONG_PRIMARY_TAG) {
438 // long-primary form ppppppC1 -> pppppp00050000500
439 return (static_cast<int64_t>(ce32) << 32) | COMMON_SEC_AND_TER_CE;
440 } else {
441 // long-secondary form ssssttC2 -> 00000000sssstt00
442 // assert (tertiary & 0xf) == LONG_SECONDARY_TAG
443 return ce32;
444 }
445 }
446 }
447
448 /** Creates a CE from a primary weight. */
449 static inline int64_t makeCE(uint32_t p) {
450 return (static_cast<int64_t>(p) << 32) | COMMON_SEC_AND_TER_CE;
451 }
452 /**
453 * Creates a CE from a primary weight,
454 * 16-bit secondary/tertiary weights, and a 2-bit quaternary.
455 */
456 static inline int64_t makeCE(uint32_t p, uint32_t s, uint32_t t, uint32_t q) {
457 return (static_cast<int64_t>(p) << 32) | (s << 16) | t | (q << 6);
458 }
459
460 /**
461 * Increments a 2-byte primary by a code point offset.
462 */
463 static uint32_t incTwoBytePrimaryByOffset(uint32_t basePrimary, UBool isCompressible,
464 int32_t offset);
465
466 /**
467 * Increments a 3-byte primary by a code point offset.
468 */
469 static uint32_t incThreeBytePrimaryByOffset(uint32_t basePrimary, UBool isCompressible,
470 int32_t offset);
471
472 /**
473 * Decrements a 2-byte primary by one range step (1..0x7f).
474 */
475 static uint32_t decTwoBytePrimaryByOneStep(uint32_t basePrimary, UBool isCompressible, int32_t step);
476
477 /**
478 * Decrements a 3-byte primary by one range step (1..0x7f).
479 */
480 static uint32_t decThreeBytePrimaryByOneStep(uint32_t basePrimary, UBool isCompressible, int32_t step);
481
482 /**
483 * Computes a 3-byte primary for c's OFFSET_TAG data "CE".
484 */
485 static uint32_t getThreeBytePrimaryForOffsetData(UChar32 c, int64_t dataCE);
486
487 /**
488 * Returns the unassigned-character implicit primary weight for any valid code point c.
489 */
490 static uint32_t unassignedPrimaryFromCodePoint(UChar32 c);
491
492 static inline int64_t unassignedCEFromCodePoint(UChar32 c) {
493 return makeCE(unassignedPrimaryFromCodePoint(c));
494 }
495
496 private:
497 Collation() = delete; // No instantiation.
498 };
499
500 U_NAMESPACE_END
501
502 #endif // !UCONFIG_NO_COLLATION
503 #endif // __COLLATION_H__