collationfcd.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. // © 2016 and later: Unicode, Inc. and others.
  2. // License & terms of use: http://www.unicode.org/copyright.html
  3. /*
  4. *******************************************************************************
  5. * Copyright (C) 2012-2014, International Business Machines
  6. * Corporation and others. All Rights Reserved.
  7. *******************************************************************************
  8. * collationfcd.h
  9. *
  10. * created on: 2012aug18
  11. * created by: Markus W. Scherer
  12. */
  13. #ifndef __COLLATIONFCD_H__
  14. #define __COLLATIONFCD_H__
  15. #include "unicode/utypes.h"
  16. #if !UCONFIG_NO_COLLATION
  17. #include "unicode/utf16.h"
  18. U_NAMESPACE_BEGIN
  19. /**
  20. * Data and functions for the FCD check fast path.
  21. *
  22. * The fast path looks at a pair of 16-bit code units and checks
  23. * whether there is an FCD boundary between them;
  24. * there is if the first unit has a trailing ccc=0 (!hasTccc(first))
  25. * or the second unit has a leading ccc=0 (!hasLccc(second)),
  26. * or both.
  27. * When the fast path finds a possible non-boundary,
  28. * then the FCD check slow path looks at the actual sequence of FCD values.
  29. *
  30. * This is a pure optimization.
  31. * The fast path must at least find all possible non-boundaries.
  32. * If the fast path is too pessimistic, it costs performance.
  33. *
  34. * For a pair of BMP characters, the fast path tests are precise (1 bit per character).
  35. *
  36. * For a supplementary code point, the two units are its lead and trail surrogates.
  37. * We set hasTccc(lead)=true if any of its 1024 associated supplementary code points
  38. * has lccc!=0 or tccc!=0.
  39. * We set hasLccc(trail)=true for all trail surrogates.
  40. * As a result, we leave the fast path if the lead surrogate might start a
  41. * supplementary code point that is not FCD-inert.
  42. * (So the fast path need not detect that there is a surrogate pair,
  43. * nor look ahead to the next full code point.)
  44. *
  45. * hasLccc(lead)=true if any of its 1024 associated supplementary code points
  46. * has lccc!=0, for fast boundary checking between BMP & supplementary.
  47. *
  48. * hasTccc(trail)=false:
  49. * It should only be tested for unpaired trail surrogates which are FCD-inert.
  50. */
  51. class U_I18N_API CollationFCD {
  52. public:
  53. static inline UBool hasLccc(UChar32 c) {
  54. // assert c <= 0xffff
  55. // c can be negative, e.g., U_SENTINEL from UCharIterator;
  56. // that is handled in the first test.
  57. int32_t i;
  58. return
  59. // U+0300 is the first character with lccc!=0.
  60. c >= 0x300 &&
  61. (i = lcccIndex[c >> 5]) != 0 &&
  62. (lcccBits[i] & ((uint32_t)1 << (c & 0x1f))) != 0;
  63. }
  64. static inline UBool hasTccc(UChar32 c) {
  65. // assert c <= 0xffff
  66. // c can be negative, e.g., U_SENTINEL from UCharIterator;
  67. // that is handled in the first test.
  68. int32_t i;
  69. return
  70. // U+00C0 is the first character with tccc!=0.
  71. c >= 0xc0 &&
  72. (i = tcccIndex[c >> 5]) != 0 &&
  73. (tcccBits[i] & ((uint32_t)1 << (c & 0x1f))) != 0;
  74. }
  75. static inline UBool mayHaveLccc(UChar32 c) {
  76. // Handles all of Unicode 0..10FFFF.
  77. // c can be negative, e.g., U_SENTINEL.
  78. // U+0300 is the first character with lccc!=0.
  79. if(c < 0x300) { return FALSE; }
  80. if(c > 0xffff) { c = U16_LEAD(c); }
  81. int32_t i;
  82. return
  83. (i = lcccIndex[c >> 5]) != 0 &&
  84. (lcccBits[i] & ((uint32_t)1 << (c & 0x1f))) != 0;
  85. }
  86. /**
  87. * Tibetan composite vowel signs (U+0F73, U+0F75, U+0F81)
  88. * must be decomposed before reaching the core collation code,
  89. * or else some sequences including them, even ones passing the FCD check,
  90. * do not yield canonically equivalent results.
  91. *
  92. * This is a fast and imprecise test.
  93. *
  94. * @param c a code point
  95. * @return TRUE if c is U+0F73, U+0F75 or U+0F81 or one of several other Tibetan characters
  96. */
  97. static inline UBool maybeTibetanCompositeVowel(UChar32 c) {
  98. return (c & 0x1fff01) == 0xf01;
  99. }
  100. /**
  101. * Tibetan composite vowel signs (U+0F73, U+0F75, U+0F81)
  102. * must be decomposed before reaching the core collation code,
  103. * or else some sequences including them, even ones passing the FCD check,
  104. * do not yield canonically equivalent results.
  105. *
  106. * They have distinct lccc/tccc combinations: 129/130 or 129/132.
  107. *
  108. * @param fcd16 the FCD value (lccc/tccc combination) of a code point
  109. * @return TRUE if fcd16 is from U+0F73, U+0F75 or U+0F81
  110. */
  111. static inline UBool isFCD16OfTibetanCompositeVowel(uint16_t fcd16) {
  112. return fcd16 == 0x8182 || fcd16 == 0x8184;
  113. }
  114. private:
  115. CollationFCD(); // No instantiation.
  116. static const uint8_t lcccIndex[2048];
  117. static const uint8_t tcccIndex[2048];
  118. static const uint32_t lcccBits[];
  119. static const uint32_t tcccBits[];
  120. };
  121. U_NAMESPACE_END
  122. #endif // !UCONFIG_NO_COLLATION
  123. #endif // __COLLATIONFCD_H__