collationiterator.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  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-2014, International Business Machines
  6. * Corporation and others. All Rights Reserved.
  7. *******************************************************************************
  8. * collationiterator.h
  9. *
  10. * created on: 2010oct27
  11. * created by: Markus W. Scherer
  12. */
  13. #ifndef __COLLATIONITERATOR_H__
  14. #define __COLLATIONITERATOR_H__
  15. #include "unicode/utypes.h"
  16. #if !UCONFIG_NO_COLLATION
  17. #include "cmemory.h"
  18. #include "collation.h"
  19. #include "collationdata.h"
  20. U_NAMESPACE_BEGIN
  21. class SkippedState;
  22. class UCharsTrie;
  23. class UVector32;
  24. /* Large enough for CEs of most short strings. */
  25. #define CEBUFFER_INITIAL_CAPACITY 40
  26. // Export an explicit template instantiation of the MaybeStackArray that
  27. // is used as a data member of CEBuffer.
  28. //
  29. // When building DLLs for Windows this is required even though
  30. // no direct access to the MaybeStackArray leaks out of the i18n library.
  31. //
  32. // See digitlst.h, pluralaffix.h, datefmt.h, and others for similar examples.
  33. //
  34. #if U_PF_WINDOWS <= U_PLATFORM && U_PLATFORM <= U_PF_CYGWIN
  35. template class U_I18N_API MaybeStackArray<int64_t, CEBUFFER_INITIAL_CAPACITY>;
  36. #endif
  37. /**
  38. * Collation element iterator and abstract character iterator.
  39. *
  40. * When a method returns a code point value, it must be in 0..10FFFF,
  41. * except it can be negative as a sentinel value.
  42. */
  43. class U_I18N_API CollationIterator : public UObject {
  44. private:
  45. class U_I18N_API CEBuffer {
  46. private:
  47. /** Large enough for CEs of most short strings. */
  48. static const int32_t INITIAL_CAPACITY = CEBUFFER_INITIAL_CAPACITY;
  49. public:
  50. CEBuffer() : length(0) {}
  51. ~CEBuffer();
  52. inline void append(int64_t ce, UErrorCode &errorCode) {
  53. if(length < INITIAL_CAPACITY || ensureAppendCapacity(1, errorCode)) {
  54. buffer[length++] = ce;
  55. }
  56. }
  57. inline void appendUnsafe(int64_t ce) {
  58. buffer[length++] = ce;
  59. }
  60. UBool ensureAppendCapacity(int32_t appCap, UErrorCode &errorCode);
  61. inline UBool incLength(UErrorCode &errorCode) {
  62. // Use INITIAL_CAPACITY for a very simple fastpath.
  63. // (Rather than buffer.getCapacity().)
  64. if(length < INITIAL_CAPACITY || ensureAppendCapacity(1, errorCode)) {
  65. ++length;
  66. return TRUE;
  67. } else {
  68. return FALSE;
  69. }
  70. }
  71. inline int64_t set(int32_t i, int64_t ce) {
  72. return buffer[i] = ce;
  73. }
  74. inline int64_t get(int32_t i) const { return buffer[i]; }
  75. const int64_t *getCEs() const { return buffer.getAlias(); }
  76. int32_t length;
  77. private:
  78. CEBuffer(const CEBuffer &);
  79. void operator=(const CEBuffer &);
  80. MaybeStackArray<int64_t, INITIAL_CAPACITY> buffer;
  81. };
  82. public:
  83. CollationIterator(const CollationData *d, UBool numeric)
  84. : trie(d->trie),
  85. data(d),
  86. cesIndex(0),
  87. skipped(NULL),
  88. numCpFwd(-1),
  89. isNumeric(numeric) {}
  90. virtual ~CollationIterator();
  91. virtual UBool operator==(const CollationIterator &other) const;
  92. inline UBool operator!=(const CollationIterator &other) const {
  93. return !operator==(other);
  94. }
  95. /**
  96. * Resets the iterator state and sets the position to the specified offset.
  97. * Subclasses must implement, and must call the parent class method,
  98. * or CollationIterator::reset().
  99. */
  100. virtual void resetToOffset(int32_t newOffset) = 0;
  101. virtual int32_t getOffset() const = 0;
  102. /**
  103. * Returns the next collation element.
  104. */
  105. inline int64_t nextCE(UErrorCode &errorCode) {
  106. if(cesIndex < ceBuffer.length) {
  107. // Return the next buffered CE.
  108. return ceBuffer.get(cesIndex++);
  109. }
  110. // assert cesIndex == ceBuffer.length;
  111. if(!ceBuffer.incLength(errorCode)) {
  112. return Collation::NO_CE;
  113. }
  114. UChar32 c;
  115. uint32_t ce32 = handleNextCE32(c, errorCode);
  116. uint32_t t = ce32 & 0xff;
  117. if(t < Collation::SPECIAL_CE32_LOW_BYTE) { // Forced-inline of isSpecialCE32(ce32).
  118. // Normal CE from the main data.
  119. // Forced-inline of ceFromSimpleCE32(ce32).
  120. return ceBuffer.set(cesIndex++,
  121. ((int64_t)(ce32 & 0xffff0000) << 32) | ((ce32 & 0xff00) << 16) | (t << 8));
  122. }
  123. const CollationData *d;
  124. // The compiler should be able to optimize the previous and the following
  125. // comparisons of t with the same constant.
  126. if(t == Collation::SPECIAL_CE32_LOW_BYTE) {
  127. if(c < 0) {
  128. return ceBuffer.set(cesIndex++, Collation::NO_CE);
  129. }
  130. d = data->base;
  131. ce32 = d->getCE32(c);
  132. t = ce32 & 0xff;
  133. if(t < Collation::SPECIAL_CE32_LOW_BYTE) {
  134. // Normal CE from the base data.
  135. return ceBuffer.set(cesIndex++,
  136. ((int64_t)(ce32 & 0xffff0000) << 32) | ((ce32 & 0xff00) << 16) | (t << 8));
  137. }
  138. } else {
  139. d = data;
  140. }
  141. if(t == Collation::LONG_PRIMARY_CE32_LOW_BYTE) {
  142. // Forced-inline of ceFromLongPrimaryCE32(ce32).
  143. return ceBuffer.set(cesIndex++,
  144. ((int64_t)(ce32 - t) << 32) | Collation::COMMON_SEC_AND_TER_CE);
  145. }
  146. return nextCEFromCE32(d, c, ce32, errorCode);
  147. }
  148. /**
  149. * Fetches all CEs.
  150. * @return getCEsLength()
  151. */
  152. int32_t fetchCEs(UErrorCode &errorCode);
  153. /**
  154. * Overwrites the current CE (the last one returned by nextCE()).
  155. */
  156. void setCurrentCE(int64_t ce) {
  157. // assert cesIndex > 0;
  158. ceBuffer.set(cesIndex - 1, ce);
  159. }
  160. /**
  161. * Returns the previous collation element.
  162. */
  163. int64_t previousCE(UVector32 &offsets, UErrorCode &errorCode);
  164. inline int32_t getCEsLength() const {
  165. return ceBuffer.length;
  166. }
  167. inline int64_t getCE(int32_t i) const {
  168. return ceBuffer.get(i);
  169. }
  170. const int64_t *getCEs() const {
  171. return ceBuffer.getCEs();
  172. }
  173. void clearCEs() {
  174. cesIndex = ceBuffer.length = 0;
  175. }
  176. void clearCEsIfNoneRemaining() {
  177. if(cesIndex == ceBuffer.length) { clearCEs(); }
  178. }
  179. /**
  180. * Returns the next code point (with post-increment).
  181. * Public for identical-level comparison and for testing.
  182. */
  183. virtual UChar32 nextCodePoint(UErrorCode &errorCode) = 0;
  184. /**
  185. * Returns the previous code point (with pre-decrement).
  186. * Public for identical-level comparison and for testing.
  187. */
  188. virtual UChar32 previousCodePoint(UErrorCode &errorCode) = 0;
  189. protected:
  190. CollationIterator(const CollationIterator &other);
  191. void reset();
  192. /**
  193. * Returns the next code point and its local CE32 value.
  194. * Returns Collation::FALLBACK_CE32 at the end of the text (c<0)
  195. * or when c's CE32 value is to be looked up in the base data (fallback).
  196. *
  197. * The code point is used for fallbacks, context and implicit weights.
  198. * It is ignored when the returned CE32 is not special (e.g., FFFD_CE32).
  199. */
  200. virtual uint32_t handleNextCE32(UChar32 &c, UErrorCode &errorCode);
  201. /**
  202. * Called when handleNextCE32() returns a LEAD_SURROGATE_TAG for a lead surrogate code unit.
  203. * Returns the trail surrogate in that case and advances past it,
  204. * if a trail surrogate follows the lead surrogate.
  205. * Otherwise returns any other code unit and does not advance.
  206. */
  207. virtual UChar handleGetTrailSurrogate();
  208. /**
  209. * Called when handleNextCE32() returns with c==0, to see whether it is a NUL terminator.
  210. * (Not needed in Java.)
  211. */
  212. virtual UBool foundNULTerminator();
  213. /**
  214. * @return FALSE if surrogate code points U+D800..U+DFFF
  215. * map to their own implicit primary weights (for UTF-16),
  216. * or TRUE if they map to CE(U+FFFD) (for UTF-8)
  217. */
  218. virtual UBool forbidSurrogateCodePoints() const;
  219. virtual void forwardNumCodePoints(int32_t num, UErrorCode &errorCode) = 0;
  220. virtual void backwardNumCodePoints(int32_t num, UErrorCode &errorCode) = 0;
  221. /**
  222. * Returns the CE32 from the data trie.
  223. * Normally the same as data->getCE32(), but overridden in the builder.
  224. * Call this only when the faster data->getCE32() cannot be used.
  225. */
  226. virtual uint32_t getDataCE32(UChar32 c) const;
  227. virtual uint32_t getCE32FromBuilderData(uint32_t ce32, UErrorCode &errorCode);
  228. void appendCEsFromCE32(const CollationData *d, UChar32 c, uint32_t ce32,
  229. UBool forward, UErrorCode &errorCode);
  230. // Main lookup trie of the data object.
  231. const UTrie2 *trie;
  232. const CollationData *data;
  233. private:
  234. int64_t nextCEFromCE32(const CollationData *d, UChar32 c, uint32_t ce32,
  235. UErrorCode &errorCode);
  236. uint32_t getCE32FromPrefix(const CollationData *d, uint32_t ce32,
  237. UErrorCode &errorCode);
  238. UChar32 nextSkippedCodePoint(UErrorCode &errorCode);
  239. void backwardNumSkipped(int32_t n, UErrorCode &errorCode);
  240. uint32_t nextCE32FromContraction(
  241. const CollationData *d, uint32_t contractionCE32,
  242. const UChar *p, uint32_t ce32, UChar32 c,
  243. UErrorCode &errorCode);
  244. uint32_t nextCE32FromDiscontiguousContraction(
  245. const CollationData *d, UCharsTrie &suffixes, uint32_t ce32,
  246. int32_t lookAhead, UChar32 c,
  247. UErrorCode &errorCode);
  248. /**
  249. * Returns the previous CE when data->isUnsafeBackward(c, isNumeric).
  250. */
  251. int64_t previousCEUnsafe(UChar32 c, UVector32 &offsets, UErrorCode &errorCode);
  252. /**
  253. * Turns a string of digits (bytes 0..9)
  254. * into a sequence of CEs that will sort in numeric order.
  255. *
  256. * Starts from this ce32's digit value and consumes the following/preceding digits.
  257. * The digits string must not be empty and must not have leading zeros.
  258. */
  259. void appendNumericCEs(uint32_t ce32, UBool forward, UErrorCode &errorCode);
  260. /**
  261. * Turns 1..254 digits into a sequence of CEs.
  262. * Called by appendNumericCEs() for each segment of at most 254 digits.
  263. */
  264. void appendNumericSegmentCEs(const char *digits, int32_t length, UErrorCode &errorCode);
  265. CEBuffer ceBuffer;
  266. int32_t cesIndex;
  267. SkippedState *skipped;
  268. // Number of code points to read forward, or -1.
  269. // Used as a forward iteration limit in previousCEUnsafe().
  270. int32_t numCpFwd;
  271. // Numeric collation (CollationSettings::NUMERIC).
  272. UBool isNumeric;
  273. };
  274. U_NAMESPACE_END
  275. #endif // !UCONFIG_NO_COLLATION
  276. #endif // __COLLATIONITERATOR_H__