bmpset.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. // © 2016 and later: Unicode, Inc. and others.
  2. // License & terms of use: http://www.unicode.org/copyright.html
  3. /*
  4. ******************************************************************************
  5. *
  6. * Copyright (C) 2007, International Business Machines
  7. * Corporation and others. All Rights Reserved.
  8. *
  9. ******************************************************************************
  10. * file name: bmpset.h
  11. * encoding: UTF-8
  12. * tab size: 8 (not used)
  13. * indentation:4
  14. *
  15. * created on: 2007jan29
  16. * created by: Markus W. Scherer
  17. */
  18. #ifndef __BMPSET_H__
  19. #define __BMPSET_H__
  20. #include "unicode/utypes.h"
  21. #include "unicode/uniset.h"
  22. U_NAMESPACE_BEGIN
  23. /*
  24. * Helper class for frozen UnicodeSets, implements contains() and span()
  25. * optimized for BMP code points. Structured to be UTF-8-friendly.
  26. *
  27. * Latin-1: Look up bytes.
  28. * 2-byte characters: Bits organized vertically.
  29. * 3-byte characters: Use zero/one/mixed data per 64-block in U+0000..U+FFFF,
  30. * with mixed for illegal ranges.
  31. * Supplementary characters: Binary search over
  32. * the supplementary part of the parent set's inversion list.
  33. */
  34. class BMPSet : public UMemory {
  35. public:
  36. BMPSet(const int32_t *parentList, int32_t parentListLength);
  37. BMPSet(const BMPSet &otherBMPSet, const int32_t *newParentList, int32_t newParentListLength);
  38. virtual ~BMPSet();
  39. virtual UBool contains(UChar32 c) const;
  40. /*
  41. * Span the initial substring for which each character c has spanCondition==contains(c).
  42. * It must be s<limit and spanCondition==0 or 1.
  43. * @return The string pointer which limits the span.
  44. */
  45. const UChar *span(const UChar *s, const UChar *limit, USetSpanCondition spanCondition) const;
  46. /*
  47. * Span the trailing substring for which each character c has spanCondition==contains(c).
  48. * It must be s<limit and spanCondition==0 or 1.
  49. * @return The string pointer which starts the span.
  50. */
  51. const UChar *spanBack(const UChar *s, const UChar *limit, USetSpanCondition spanCondition) const;
  52. /*
  53. * Span the initial substring for which each character c has spanCondition==contains(c).
  54. * It must be length>0 and spanCondition==0 or 1.
  55. * @return The string pointer which limits the span.
  56. */
  57. const uint8_t *spanUTF8(const uint8_t *s, int32_t length, USetSpanCondition spanCondition) const;
  58. /*
  59. * Span the trailing substring for which each character c has spanCondition==contains(c).
  60. * It must be length>0 and spanCondition==0 or 1.
  61. * @return The start of the span.
  62. */
  63. int32_t spanBackUTF8(const uint8_t *s, int32_t length, USetSpanCondition spanCondition) const;
  64. private:
  65. void initBits();
  66. void overrideIllegal();
  67. /**
  68. * Same as UnicodeSet::findCodePoint(UChar32 c) const except that the
  69. * binary search is restricted for finding code points in a certain range.
  70. *
  71. * For restricting the search for finding in the range start..end,
  72. * pass in
  73. * lo=findCodePoint(start) and
  74. * hi=findCodePoint(end)
  75. * with 0<=lo<=hi<len.
  76. * findCodePoint(c) defaults to lo=0 and hi=len-1.
  77. *
  78. * @param c a character in a subrange of MIN_VALUE..MAX_VALUE
  79. * @param lo The lowest index to be returned.
  80. * @param hi The highest index to be returned.
  81. * @return the smallest integer i in the range lo..hi,
  82. * inclusive, such that c < list[i]
  83. */
  84. int32_t findCodePoint(UChar32 c, int32_t lo, int32_t hi) const;
  85. inline UBool containsSlow(UChar32 c, int32_t lo, int32_t hi) const;
  86. /*
  87. * One byte 0 or 1 per Latin-1 character.
  88. */
  89. UBool latin1Contains[0x100];
  90. /* TRUE if contains(U+FFFD). */
  91. UBool containsFFFD;
  92. /*
  93. * One bit per code point from U+0000..U+07FF.
  94. * The bits are organized vertically; consecutive code points
  95. * correspond to the same bit positions in consecutive table words.
  96. * With code point parts
  97. * lead=c{10..6}
  98. * trail=c{5..0}
  99. * it is set.contains(c)==(table7FF[trail] bit lead)
  100. *
  101. * Bits for 0..7F (non-shortest forms) are set to the result of contains(FFFD)
  102. * for faster validity checking at runtime.
  103. */
  104. uint32_t table7FF[64];
  105. /*
  106. * One bit per 64 BMP code points.
  107. * The bits are organized vertically; consecutive 64-code point blocks
  108. * correspond to the same bit position in consecutive table words.
  109. * With code point parts
  110. * lead=c{15..12}
  111. * t1=c{11..6}
  112. * test bits (lead+16) and lead in bmpBlockBits[t1].
  113. * If the upper bit is 0, then the lower bit indicates if contains(c)
  114. * for all code points in the 64-block.
  115. * If the upper bit is 1, then the block is mixed and set.contains(c)
  116. * must be called.
  117. *
  118. * Bits for 0..7FF (non-shortest forms) and D800..DFFF are set to
  119. * the result of contains(FFFD) for faster validity checking at runtime.
  120. */
  121. uint32_t bmpBlockBits[64];
  122. /*
  123. * Inversion list indexes for restricted binary searches in
  124. * findCodePoint(), from
  125. * findCodePoint(U+0800, U+1000, U+2000, .., U+F000, U+10000).
  126. * U+0800 is the first 3-byte-UTF-8 code point. Code points below U+0800 are
  127. * always looked up in the bit tables.
  128. * The last pair of indexes is for finding supplementary code points.
  129. */
  130. int32_t list4kStarts[18];
  131. /*
  132. * The inversion list of the parent set, for the slower contains() implementation
  133. * for mixed BMP blocks and for supplementary code points.
  134. * The list is terminated with list[listLength-1]=0x110000.
  135. */
  136. const int32_t *list;
  137. int32_t listLength;
  138. };
  139. inline UBool BMPSet::containsSlow(UChar32 c, int32_t lo, int32_t hi) const {
  140. return (UBool)(findCodePoint(c, lo, hi) & 1);
  141. }
  142. U_NAMESPACE_END
  143. #endif