1*0e209d39SAndroid Build Coastguard Worker // © 2016 and later: Unicode, Inc. and others. 2*0e209d39SAndroid Build Coastguard Worker // License & terms of use: http://www.unicode.org/copyright.html 3*0e209d39SAndroid Build Coastguard Worker /* 4*0e209d39SAndroid Build Coastguard Worker ******************************************************************************* 5*0e209d39SAndroid Build Coastguard Worker * Copyright (C) 2012-2016, International Business Machines 6*0e209d39SAndroid Build Coastguard Worker * Corporation and others. All Rights Reserved. 7*0e209d39SAndroid Build Coastguard Worker ******************************************************************************* 8*0e209d39SAndroid Build Coastguard Worker * uitercollationiterator.h 9*0e209d39SAndroid Build Coastguard Worker * 10*0e209d39SAndroid Build Coastguard Worker * created on: 2012sep23 (from utf16collationiterator.h) 11*0e209d39SAndroid Build Coastguard Worker * created by: Markus W. Scherer 12*0e209d39SAndroid Build Coastguard Worker */ 13*0e209d39SAndroid Build Coastguard Worker 14*0e209d39SAndroid Build Coastguard Worker #ifndef __UITERCOLLATIONITERATOR_H__ 15*0e209d39SAndroid Build Coastguard Worker #define __UITERCOLLATIONITERATOR_H__ 16*0e209d39SAndroid Build Coastguard Worker 17*0e209d39SAndroid Build Coastguard Worker #include "unicode/utypes.h" 18*0e209d39SAndroid Build Coastguard Worker 19*0e209d39SAndroid Build Coastguard Worker #if !UCONFIG_NO_COLLATION 20*0e209d39SAndroid Build Coastguard Worker 21*0e209d39SAndroid Build Coastguard Worker #include "unicode/uiter.h" 22*0e209d39SAndroid Build Coastguard Worker #include "cmemory.h" 23*0e209d39SAndroid Build Coastguard Worker #include "collation.h" 24*0e209d39SAndroid Build Coastguard Worker #include "collationdata.h" 25*0e209d39SAndroid Build Coastguard Worker #include "collationiterator.h" 26*0e209d39SAndroid Build Coastguard Worker #include "normalizer2impl.h" 27*0e209d39SAndroid Build Coastguard Worker 28*0e209d39SAndroid Build Coastguard Worker U_NAMESPACE_BEGIN 29*0e209d39SAndroid Build Coastguard Worker 30*0e209d39SAndroid Build Coastguard Worker /** 31*0e209d39SAndroid Build Coastguard Worker * UCharIterator-based collation element and character iterator. 32*0e209d39SAndroid Build Coastguard Worker * Handles normalized text inline, with length or NUL-terminated. 33*0e209d39SAndroid Build Coastguard Worker * Unnormalized text is handled by a subclass. 34*0e209d39SAndroid Build Coastguard Worker */ 35*0e209d39SAndroid Build Coastguard Worker class U_I18N_API UIterCollationIterator : public CollationIterator { 36*0e209d39SAndroid Build Coastguard Worker public: UIterCollationIterator(const CollationData * d,UBool numeric,UCharIterator & ui)37*0e209d39SAndroid Build Coastguard Worker UIterCollationIterator(const CollationData *d, UBool numeric, UCharIterator &ui) 38*0e209d39SAndroid Build Coastguard Worker : CollationIterator(d, numeric), iter(ui) {} 39*0e209d39SAndroid Build Coastguard Worker 40*0e209d39SAndroid Build Coastguard Worker virtual ~UIterCollationIterator(); 41*0e209d39SAndroid Build Coastguard Worker 42*0e209d39SAndroid Build Coastguard Worker virtual void resetToOffset(int32_t newOffset) override; 43*0e209d39SAndroid Build Coastguard Worker 44*0e209d39SAndroid Build Coastguard Worker virtual int32_t getOffset() const override; 45*0e209d39SAndroid Build Coastguard Worker 46*0e209d39SAndroid Build Coastguard Worker virtual UChar32 nextCodePoint(UErrorCode &errorCode) override; 47*0e209d39SAndroid Build Coastguard Worker 48*0e209d39SAndroid Build Coastguard Worker virtual UChar32 previousCodePoint(UErrorCode &errorCode) override; 49*0e209d39SAndroid Build Coastguard Worker 50*0e209d39SAndroid Build Coastguard Worker protected: 51*0e209d39SAndroid Build Coastguard Worker virtual uint32_t handleNextCE32(UChar32 &c, UErrorCode &errorCode) override; 52*0e209d39SAndroid Build Coastguard Worker 53*0e209d39SAndroid Build Coastguard Worker virtual char16_t handleGetTrailSurrogate() override; 54*0e209d39SAndroid Build Coastguard Worker 55*0e209d39SAndroid Build Coastguard Worker virtual void forwardNumCodePoints(int32_t num, UErrorCode &errorCode) override; 56*0e209d39SAndroid Build Coastguard Worker 57*0e209d39SAndroid Build Coastguard Worker virtual void backwardNumCodePoints(int32_t num, UErrorCode &errorCode) override; 58*0e209d39SAndroid Build Coastguard Worker 59*0e209d39SAndroid Build Coastguard Worker UCharIterator &iter; 60*0e209d39SAndroid Build Coastguard Worker }; 61*0e209d39SAndroid Build Coastguard Worker 62*0e209d39SAndroid Build Coastguard Worker /** 63*0e209d39SAndroid Build Coastguard Worker * Incrementally checks the input text for FCD and normalizes where necessary. 64*0e209d39SAndroid Build Coastguard Worker */ 65*0e209d39SAndroid Build Coastguard Worker class U_I18N_API FCDUIterCollationIterator : public UIterCollationIterator { 66*0e209d39SAndroid Build Coastguard Worker public: FCDUIterCollationIterator(const CollationData * data,UBool numeric,UCharIterator & ui,int32_t startIndex)67*0e209d39SAndroid Build Coastguard Worker FCDUIterCollationIterator(const CollationData *data, UBool numeric, UCharIterator &ui, int32_t startIndex) 68*0e209d39SAndroid Build Coastguard Worker : UIterCollationIterator(data, numeric, ui), 69*0e209d39SAndroid Build Coastguard Worker state(ITER_CHECK_FWD), start(startIndex), 70*0e209d39SAndroid Build Coastguard Worker nfcImpl(data->nfcImpl) {} 71*0e209d39SAndroid Build Coastguard Worker 72*0e209d39SAndroid Build Coastguard Worker virtual ~FCDUIterCollationIterator(); 73*0e209d39SAndroid Build Coastguard Worker 74*0e209d39SAndroid Build Coastguard Worker virtual void resetToOffset(int32_t newOffset) override; 75*0e209d39SAndroid Build Coastguard Worker 76*0e209d39SAndroid Build Coastguard Worker virtual int32_t getOffset() const override; 77*0e209d39SAndroid Build Coastguard Worker 78*0e209d39SAndroid Build Coastguard Worker virtual UChar32 nextCodePoint(UErrorCode &errorCode) override; 79*0e209d39SAndroid Build Coastguard Worker 80*0e209d39SAndroid Build Coastguard Worker virtual UChar32 previousCodePoint(UErrorCode &errorCode) override; 81*0e209d39SAndroid Build Coastguard Worker 82*0e209d39SAndroid Build Coastguard Worker protected: 83*0e209d39SAndroid Build Coastguard Worker virtual uint32_t handleNextCE32(UChar32 &c, UErrorCode &errorCode) override; 84*0e209d39SAndroid Build Coastguard Worker 85*0e209d39SAndroid Build Coastguard Worker virtual char16_t handleGetTrailSurrogate() override; 86*0e209d39SAndroid Build Coastguard Worker 87*0e209d39SAndroid Build Coastguard Worker 88*0e209d39SAndroid Build Coastguard Worker virtual void forwardNumCodePoints(int32_t num, UErrorCode &errorCode) override; 89*0e209d39SAndroid Build Coastguard Worker 90*0e209d39SAndroid Build Coastguard Worker virtual void backwardNumCodePoints(int32_t num, UErrorCode &errorCode) override; 91*0e209d39SAndroid Build Coastguard Worker 92*0e209d39SAndroid Build Coastguard Worker private: 93*0e209d39SAndroid Build Coastguard Worker /** 94*0e209d39SAndroid Build Coastguard Worker * Switches to forward checking if possible. 95*0e209d39SAndroid Build Coastguard Worker */ 96*0e209d39SAndroid Build Coastguard Worker void switchToForward(); 97*0e209d39SAndroid Build Coastguard Worker 98*0e209d39SAndroid Build Coastguard Worker /** 99*0e209d39SAndroid Build Coastguard Worker * Extends the FCD text segment forward or normalizes around pos. 100*0e209d39SAndroid Build Coastguard Worker * @return true if success 101*0e209d39SAndroid Build Coastguard Worker */ 102*0e209d39SAndroid Build Coastguard Worker UBool nextSegment(UErrorCode &errorCode); 103*0e209d39SAndroid Build Coastguard Worker 104*0e209d39SAndroid Build Coastguard Worker /** 105*0e209d39SAndroid Build Coastguard Worker * Switches to backward checking. 106*0e209d39SAndroid Build Coastguard Worker */ 107*0e209d39SAndroid Build Coastguard Worker void switchToBackward(); 108*0e209d39SAndroid Build Coastguard Worker 109*0e209d39SAndroid Build Coastguard Worker /** 110*0e209d39SAndroid Build Coastguard Worker * Extends the FCD text segment backward or normalizes around pos. 111*0e209d39SAndroid Build Coastguard Worker * @return true if success 112*0e209d39SAndroid Build Coastguard Worker */ 113*0e209d39SAndroid Build Coastguard Worker UBool previousSegment(UErrorCode &errorCode); 114*0e209d39SAndroid Build Coastguard Worker 115*0e209d39SAndroid Build Coastguard Worker UBool normalize(const UnicodeString &s, UErrorCode &errorCode); 116*0e209d39SAndroid Build Coastguard Worker 117*0e209d39SAndroid Build Coastguard Worker enum State { 118*0e209d39SAndroid Build Coastguard Worker /** 119*0e209d39SAndroid Build Coastguard Worker * The input text [start..(iter index)[ passes the FCD check. 120*0e209d39SAndroid Build Coastguard Worker * Moving forward checks incrementally. 121*0e209d39SAndroid Build Coastguard Worker * pos & limit are undefined. 122*0e209d39SAndroid Build Coastguard Worker */ 123*0e209d39SAndroid Build Coastguard Worker ITER_CHECK_FWD, 124*0e209d39SAndroid Build Coastguard Worker /** 125*0e209d39SAndroid Build Coastguard Worker * The input text [(iter index)..limit[ passes the FCD check. 126*0e209d39SAndroid Build Coastguard Worker * Moving backward checks incrementally. 127*0e209d39SAndroid Build Coastguard Worker * start & pos are undefined. 128*0e209d39SAndroid Build Coastguard Worker */ 129*0e209d39SAndroid Build Coastguard Worker ITER_CHECK_BWD, 130*0e209d39SAndroid Build Coastguard Worker /** 131*0e209d39SAndroid Build Coastguard Worker * The input text [start..limit[ passes the FCD check. 132*0e209d39SAndroid Build Coastguard Worker * pos tracks the current text index. 133*0e209d39SAndroid Build Coastguard Worker */ 134*0e209d39SAndroid Build Coastguard Worker ITER_IN_FCD_SEGMENT, 135*0e209d39SAndroid Build Coastguard Worker /** 136*0e209d39SAndroid Build Coastguard Worker * The input text [start..limit[ failed the FCD check and was normalized. 137*0e209d39SAndroid Build Coastguard Worker * pos tracks the current index in the normalized string. 138*0e209d39SAndroid Build Coastguard Worker * The text iterator is at the limit index. 139*0e209d39SAndroid Build Coastguard Worker */ 140*0e209d39SAndroid Build Coastguard Worker IN_NORM_ITER_AT_LIMIT, 141*0e209d39SAndroid Build Coastguard Worker /** 142*0e209d39SAndroid Build Coastguard Worker * The input text [start..limit[ failed the FCD check and was normalized. 143*0e209d39SAndroid Build Coastguard Worker * pos tracks the current index in the normalized string. 144*0e209d39SAndroid Build Coastguard Worker * The text iterator is at the start index. 145*0e209d39SAndroid Build Coastguard Worker */ 146*0e209d39SAndroid Build Coastguard Worker IN_NORM_ITER_AT_START 147*0e209d39SAndroid Build Coastguard Worker }; 148*0e209d39SAndroid Build Coastguard Worker 149*0e209d39SAndroid Build Coastguard Worker State state; 150*0e209d39SAndroid Build Coastguard Worker 151*0e209d39SAndroid Build Coastguard Worker int32_t start; 152*0e209d39SAndroid Build Coastguard Worker int32_t pos; 153*0e209d39SAndroid Build Coastguard Worker int32_t limit; 154*0e209d39SAndroid Build Coastguard Worker 155*0e209d39SAndroid Build Coastguard Worker const Normalizer2Impl &nfcImpl; 156*0e209d39SAndroid Build Coastguard Worker UnicodeString normalized; 157*0e209d39SAndroid Build Coastguard Worker }; 158*0e209d39SAndroid Build Coastguard Worker 159*0e209d39SAndroid Build Coastguard Worker U_NAMESPACE_END 160*0e209d39SAndroid Build Coastguard Worker 161*0e209d39SAndroid Build Coastguard Worker #endif // !UCONFIG_NO_COLLATION 162*0e209d39SAndroid Build Coastguard Worker #endif // __UITERCOLLATIONITERATOR_H__ 163