// © 2016 and later: Unicode, Inc. and others.1// License & terms of use: http://www.unicode.org/copyright.html2/*3*******************************************************************************4* Copyright (C) 2010-2014, International Business Machines5* Corporation and others. All Rights Reserved.6*******************************************************************************7* collationiterator.h8*9* created on: 2010oct2710* created by: Markus W. Scherer11*/1213#ifndef __COLLATIONITERATOR_H__14#define __COLLATIONITERATOR_H__1516#include "unicode/utypes.h"1718#if !UCONFIG_NO_COLLATION1920#include "cmemory.h"21#include "collation.h"22#include "collationdata.h"2324U_NAMESPACE_BEGIN2526class SkippedState;27class UCharsTrie;28class UVector32;2930/* Large enough for CEs of most short strings. */31#define CEBUFFER_INITIAL_CAPACITY 403233// Export an explicit template instantiation of the MaybeStackArray that34// is used as a data member of CEBuffer.35//36// When building DLLs for Windows this is required even though37// no direct access to the MaybeStackArray leaks out of the i18n library.38//39// See digitlst.h, pluralaffix.h, datefmt.h, and others for similar examples.40//41#if U_PF_WINDOWS <= U_PLATFORM && U_PLATFORM <= U_PF_CYGWIN42template class U_I18N_API MaybeStackArray<int64_t, CEBUFFER_INITIAL_CAPACITY>;43#endif4445/**46* Collation element iterator and abstract character iterator.47*48* When a method returns a code point value, it must be in 0..10FFFF,49* except it can be negative as a sentinel value.50*/51class U_I18N_API CollationIterator : public UObject {52private:53class U_I18N_API CEBuffer {54private:55/** Large enough for CEs of most short strings. */56static const int32_t INITIAL_CAPACITY = CEBUFFER_INITIAL_CAPACITY;57public:58CEBuffer() : length(0) {}59~CEBuffer();6061inline void append(int64_t ce, UErrorCode &errorCode) {62if(length < INITIAL_CAPACITY || ensureAppendCapacity(1, errorCode)) {63buffer[length++] = ce;64}65}6667inline void appendUnsafe(int64_t ce) {68buffer[length++] = ce;69}7071UBool ensureAppendCapacity(int32_t appCap, UErrorCode &errorCode);7273inline UBool incLength(UErrorCode &errorCode) {74// Use INITIAL_CAPACITY for a very simple fastpath.75// (Rather than buffer.getCapacity().)76if(length < INITIAL_CAPACITY || ensureAppendCapacity(1, errorCode)) {77++length;78return true;79} else {80return false;81}82}8384inline int64_t set(int32_t i, int64_t ce) {85return buffer[i] = ce;86}87inline int64_t get(int32_t i) const { return buffer[i]; }8889const int64_t *getCEs() const { return buffer.getAlias(); }9091int32_t length;9293private:94CEBuffer(const CEBuffer &) = delete;95void operator=(const CEBuffer &) = delete;9697MaybeStackArray<int64_t, INITIAL_CAPACITY> buffer;98};99100public:101CollationIterator(const CollationData *d, UBool numeric)102: trie(d->trie),103data(d),104cesIndex(0),105skipped(NULL),106numCpFwd(-1),107isNumeric(numeric) {}108109virtual ~CollationIterator();110111virtual bool operator==(const CollationIterator &other) const;112inline bool operator!=(const CollationIterator &other) const {113return !operator==(other);114}115116/**117* Resets the iterator state and sets the position to the specified offset.118* Subclasses must implement, and must call the parent class method,119* or CollationIterator::reset().120*/121virtual void resetToOffset(int32_t newOffset) = 0;122123virtual int32_t getOffset() const = 0;124125/**126* Returns the next collation element.127*/128inline int64_t nextCE(UErrorCode &errorCode) {129if(cesIndex < ceBuffer.length) {130// Return the next buffered CE.131return ceBuffer.get(cesIndex++);132}133// assert cesIndex == ceBuffer.length;134if(!ceBuffer.incLength(errorCode)) {135return Collation::NO_CE;136}137UChar32 c;138uint32_t ce32 = handleNextCE32(c, errorCode);139uint32_t t = ce32 & 0xff;140if(t < Collation::SPECIAL_CE32_LOW_BYTE) { // Forced-inline of isSpecialCE32(ce32).141// Normal CE from the main data.142// Forced-inline of ceFromSimpleCE32(ce32).143return ceBuffer.set(cesIndex++,144((int64_t)(ce32 & 0xffff0000) << 32) | ((ce32 & 0xff00) << 16) | (t << 8));145}146const CollationData *d;147// The compiler should be able to optimize the previous and the following148// comparisons of t with the same constant.149if(t == Collation::SPECIAL_CE32_LOW_BYTE) {150if(c < 0) {151return ceBuffer.set(cesIndex++, Collation::NO_CE);152}153d = data->base;154ce32 = d->getCE32(c);155t = ce32 & 0xff;156if(t < Collation::SPECIAL_CE32_LOW_BYTE) {157// Normal CE from the base data.158return ceBuffer.set(cesIndex++,159((int64_t)(ce32 & 0xffff0000) << 32) | ((ce32 & 0xff00) << 16) | (t << 8));160}161} else {162d = data;163}164if(t == Collation::LONG_PRIMARY_CE32_LOW_BYTE) {165// Forced-inline of ceFromLongPrimaryCE32(ce32).166return ceBuffer.set(cesIndex++,167((int64_t)(ce32 - t) << 32) | Collation::COMMON_SEC_AND_TER_CE);168}169return nextCEFromCE32(d, c, ce32, errorCode);170}171172/**173* Fetches all CEs.174* @return getCEsLength()175*/176int32_t fetchCEs(UErrorCode &errorCode);177178/**179* Overwrites the current CE (the last one returned by nextCE()).180*/181void setCurrentCE(int64_t ce) {182// assert cesIndex > 0;183ceBuffer.set(cesIndex - 1, ce);184}185186/**187* Returns the previous collation element.188*/189int64_t previousCE(UVector32 &offsets, UErrorCode &errorCode);190191inline int32_t getCEsLength() const {192return ceBuffer.length;193}194195inline int64_t getCE(int32_t i) const {196return ceBuffer.get(i);197}198199const int64_t *getCEs() const {200return ceBuffer.getCEs();201}202203void clearCEs() {204cesIndex = ceBuffer.length = 0;205}206207void clearCEsIfNoneRemaining() {208if(cesIndex == ceBuffer.length) { clearCEs(); }209}210211/**212* Returns the next code point (with post-increment).213* Public for identical-level comparison and for testing.214*/215virtual UChar32 nextCodePoint(UErrorCode &errorCode) = 0;216217/**218* Returns the previous code point (with pre-decrement).219* Public for identical-level comparison and for testing.220*/221virtual UChar32 previousCodePoint(UErrorCode &errorCode) = 0;222223protected:224CollationIterator(const CollationIterator &other);225226void reset();227228/**229* Returns the next code point and its local CE32 value.230* Returns Collation::FALLBACK_CE32 at the end of the text (c<0)231* or when c's CE32 value is to be looked up in the base data (fallback).232*233* The code point is used for fallbacks, context and implicit weights.234* It is ignored when the returned CE32 is not special (e.g., FFFD_CE32).235*/236virtual uint32_t handleNextCE32(UChar32 &c, UErrorCode &errorCode);237238/**239* Called when handleNextCE32() returns a LEAD_SURROGATE_TAG for a lead surrogate code unit.240* Returns the trail surrogate in that case and advances past it,241* if a trail surrogate follows the lead surrogate.242* Otherwise returns any other code unit and does not advance.243*/244virtual UChar handleGetTrailSurrogate();245246/**247* Called when handleNextCE32() returns with c==0, to see whether it is a NUL terminator.248* (Not needed in Java.)249*/250virtual UBool foundNULTerminator();251252/**253* @return false if surrogate code points U+D800..U+DFFF254* map to their own implicit primary weights (for UTF-16),255* or true if they map to CE(U+FFFD) (for UTF-8)256*/257virtual UBool forbidSurrogateCodePoints() const;258259virtual void forwardNumCodePoints(int32_t num, UErrorCode &errorCode) = 0;260261virtual void backwardNumCodePoints(int32_t num, UErrorCode &errorCode) = 0;262263/**264* Returns the CE32 from the data trie.265* Normally the same as data->getCE32(), but overridden in the builder.266* Call this only when the faster data->getCE32() cannot be used.267*/268virtual uint32_t getDataCE32(UChar32 c) const;269270virtual uint32_t getCE32FromBuilderData(uint32_t ce32, UErrorCode &errorCode);271272void appendCEsFromCE32(const CollationData *d, UChar32 c, uint32_t ce32,273UBool forward, UErrorCode &errorCode);274275// Main lookup trie of the data object.276const UTrie2 *trie;277const CollationData *data;278279private:280int64_t nextCEFromCE32(const CollationData *d, UChar32 c, uint32_t ce32,281UErrorCode &errorCode);282283uint32_t getCE32FromPrefix(const CollationData *d, uint32_t ce32,284UErrorCode &errorCode);285286UChar32 nextSkippedCodePoint(UErrorCode &errorCode);287288void backwardNumSkipped(int32_t n, UErrorCode &errorCode);289290uint32_t nextCE32FromContraction(291const CollationData *d, uint32_t contractionCE32,292const UChar *p, uint32_t ce32, UChar32 c,293UErrorCode &errorCode);294295uint32_t nextCE32FromDiscontiguousContraction(296const CollationData *d, UCharsTrie &suffixes, uint32_t ce32,297int32_t lookAhead, UChar32 c,298UErrorCode &errorCode);299300/**301* Returns the previous CE when data->isUnsafeBackward(c, isNumeric).302*/303int64_t previousCEUnsafe(UChar32 c, UVector32 &offsets, UErrorCode &errorCode);304305/**306* Turns a string of digits (bytes 0..9)307* into a sequence of CEs that will sort in numeric order.308*309* Starts from this ce32's digit value and consumes the following/preceding digits.310* The digits string must not be empty and must not have leading zeros.311*/312void appendNumericCEs(uint32_t ce32, UBool forward, UErrorCode &errorCode);313314/**315* Turns 1..254 digits into a sequence of CEs.316* Called by appendNumericCEs() for each segment of at most 254 digits.317*/318void appendNumericSegmentCEs(const char *digits, int32_t length, UErrorCode &errorCode);319320CEBuffer ceBuffer;321int32_t cesIndex;322323SkippedState *skipped;324325// Number of code points to read forward, or -1.326// Used as a forward iteration limit in previousCEUnsafe().327int32_t numCpFwd;328// Numeric collation (CollationSettings::NUMERIC).329UBool isNumeric;330};331332U_NAMESPACE_END333334#endif // !UCONFIG_NO_COLLATION335#endif // __COLLATIONITERATOR_H__336337338