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) 1996-2014, International Business Machines Corporation and 6*0e209d39SAndroid Build Coastguard Worker * others. All Rights Reserved. 7*0e209d39SAndroid Build Coastguard Worker ******************************************************************************* 8*0e209d39SAndroid Build Coastguard Worker */ 9*0e209d39SAndroid Build Coastguard Worker 10*0e209d39SAndroid Build Coastguard Worker #ifndef CANITER_H 11*0e209d39SAndroid Build Coastguard Worker #define CANITER_H 12*0e209d39SAndroid Build Coastguard Worker 13*0e209d39SAndroid Build Coastguard Worker #include "unicode/utypes.h" 14*0e209d39SAndroid Build Coastguard Worker 15*0e209d39SAndroid Build Coastguard Worker #if U_SHOW_CPLUSPLUS_API 16*0e209d39SAndroid Build Coastguard Worker 17*0e209d39SAndroid Build Coastguard Worker #if !UCONFIG_NO_NORMALIZATION 18*0e209d39SAndroid Build Coastguard Worker 19*0e209d39SAndroid Build Coastguard Worker #include "unicode/uobject.h" 20*0e209d39SAndroid Build Coastguard Worker #include "unicode/unistr.h" 21*0e209d39SAndroid Build Coastguard Worker 22*0e209d39SAndroid Build Coastguard Worker /** 23*0e209d39SAndroid Build Coastguard Worker * \file 24*0e209d39SAndroid Build Coastguard Worker * \brief C++ API: Canonical Iterator 25*0e209d39SAndroid Build Coastguard Worker */ 26*0e209d39SAndroid Build Coastguard Worker 27*0e209d39SAndroid Build Coastguard Worker /** Should permutation skip characters with combining class zero 28*0e209d39SAndroid Build Coastguard Worker * Should be either true or false. This is a compile time option 29*0e209d39SAndroid Build Coastguard Worker * @stable ICU 2.4 30*0e209d39SAndroid Build Coastguard Worker */ 31*0e209d39SAndroid Build Coastguard Worker #ifndef CANITER_SKIP_ZEROES 32*0e209d39SAndroid Build Coastguard Worker #define CANITER_SKIP_ZEROES true 33*0e209d39SAndroid Build Coastguard Worker #endif 34*0e209d39SAndroid Build Coastguard Worker 35*0e209d39SAndroid Build Coastguard Worker U_NAMESPACE_BEGIN 36*0e209d39SAndroid Build Coastguard Worker 37*0e209d39SAndroid Build Coastguard Worker class Hashtable; 38*0e209d39SAndroid Build Coastguard Worker class Normalizer2; 39*0e209d39SAndroid Build Coastguard Worker class Normalizer2Impl; 40*0e209d39SAndroid Build Coastguard Worker 41*0e209d39SAndroid Build Coastguard Worker /** 42*0e209d39SAndroid Build Coastguard Worker * This class allows one to iterate through all the strings that are canonically equivalent to a given 43*0e209d39SAndroid Build Coastguard Worker * string. For example, here are some sample results: 44*0e209d39SAndroid Build Coastguard Worker Results for: {LATIN CAPITAL LETTER A WITH RING ABOVE}{LATIN SMALL LETTER D}{COMBINING DOT ABOVE}{COMBINING CEDILLA} 45*0e209d39SAndroid Build Coastguard Worker 1: \\u0041\\u030A\\u0064\\u0307\\u0327 46*0e209d39SAndroid Build Coastguard Worker = {LATIN CAPITAL LETTER A}{COMBINING RING ABOVE}{LATIN SMALL LETTER D}{COMBINING DOT ABOVE}{COMBINING CEDILLA} 47*0e209d39SAndroid Build Coastguard Worker 2: \\u0041\\u030A\\u0064\\u0327\\u0307 48*0e209d39SAndroid Build Coastguard Worker = {LATIN CAPITAL LETTER A}{COMBINING RING ABOVE}{LATIN SMALL LETTER D}{COMBINING CEDILLA}{COMBINING DOT ABOVE} 49*0e209d39SAndroid Build Coastguard Worker 3: \\u0041\\u030A\\u1E0B\\u0327 50*0e209d39SAndroid Build Coastguard Worker = {LATIN CAPITAL LETTER A}{COMBINING RING ABOVE}{LATIN SMALL LETTER D WITH DOT ABOVE}{COMBINING CEDILLA} 51*0e209d39SAndroid Build Coastguard Worker 4: \\u0041\\u030A\\u1E11\\u0307 52*0e209d39SAndroid Build Coastguard Worker = {LATIN CAPITAL LETTER A}{COMBINING RING ABOVE}{LATIN SMALL LETTER D WITH CEDILLA}{COMBINING DOT ABOVE} 53*0e209d39SAndroid Build Coastguard Worker 5: \\u00C5\\u0064\\u0307\\u0327 54*0e209d39SAndroid Build Coastguard Worker = {LATIN CAPITAL LETTER A WITH RING ABOVE}{LATIN SMALL LETTER D}{COMBINING DOT ABOVE}{COMBINING CEDILLA} 55*0e209d39SAndroid Build Coastguard Worker 6: \\u00C5\\u0064\\u0327\\u0307 56*0e209d39SAndroid Build Coastguard Worker = {LATIN CAPITAL LETTER A WITH RING ABOVE}{LATIN SMALL LETTER D}{COMBINING CEDILLA}{COMBINING DOT ABOVE} 57*0e209d39SAndroid Build Coastguard Worker 7: \\u00C5\\u1E0B\\u0327 58*0e209d39SAndroid Build Coastguard Worker = {LATIN CAPITAL LETTER A WITH RING ABOVE}{LATIN SMALL LETTER D WITH DOT ABOVE}{COMBINING CEDILLA} 59*0e209d39SAndroid Build Coastguard Worker 8: \\u00C5\\u1E11\\u0307 60*0e209d39SAndroid Build Coastguard Worker = {LATIN CAPITAL LETTER A WITH RING ABOVE}{LATIN SMALL LETTER D WITH CEDILLA}{COMBINING DOT ABOVE} 61*0e209d39SAndroid Build Coastguard Worker 9: \\u212B\\u0064\\u0307\\u0327 62*0e209d39SAndroid Build Coastguard Worker = {ANGSTROM SIGN}{LATIN SMALL LETTER D}{COMBINING DOT ABOVE}{COMBINING CEDILLA} 63*0e209d39SAndroid Build Coastguard Worker 10: \\u212B\\u0064\\u0327\\u0307 64*0e209d39SAndroid Build Coastguard Worker = {ANGSTROM SIGN}{LATIN SMALL LETTER D}{COMBINING CEDILLA}{COMBINING DOT ABOVE} 65*0e209d39SAndroid Build Coastguard Worker 11: \\u212B\\u1E0B\\u0327 66*0e209d39SAndroid Build Coastguard Worker = {ANGSTROM SIGN}{LATIN SMALL LETTER D WITH DOT ABOVE}{COMBINING CEDILLA} 67*0e209d39SAndroid Build Coastguard Worker 12: \\u212B\\u1E11\\u0307 68*0e209d39SAndroid Build Coastguard Worker = {ANGSTROM SIGN}{LATIN SMALL LETTER D WITH CEDILLA}{COMBINING DOT ABOVE} 69*0e209d39SAndroid Build Coastguard Worker *<br>Note: the code is intended for use with small strings, and is not suitable for larger ones, 70*0e209d39SAndroid Build Coastguard Worker * since it has not been optimized for that situation. 71*0e209d39SAndroid Build Coastguard Worker * Note, CanonicalIterator is not intended to be subclassed. 72*0e209d39SAndroid Build Coastguard Worker * @author M. Davis 73*0e209d39SAndroid Build Coastguard Worker * @author C++ port by V. Weinstein 74*0e209d39SAndroid Build Coastguard Worker * @stable ICU 2.4 75*0e209d39SAndroid Build Coastguard Worker */ 76*0e209d39SAndroid Build Coastguard Worker class U_COMMON_API CanonicalIterator final : public UObject { 77*0e209d39SAndroid Build Coastguard Worker public: 78*0e209d39SAndroid Build Coastguard Worker /** 79*0e209d39SAndroid Build Coastguard Worker * Construct a CanonicalIterator object 80*0e209d39SAndroid Build Coastguard Worker * @param source string to get results for 81*0e209d39SAndroid Build Coastguard Worker * @param status Fill-in parameter which receives the status of this operation. 82*0e209d39SAndroid Build Coastguard Worker * @stable ICU 2.4 83*0e209d39SAndroid Build Coastguard Worker */ 84*0e209d39SAndroid Build Coastguard Worker CanonicalIterator(const UnicodeString &source, UErrorCode &status); 85*0e209d39SAndroid Build Coastguard Worker 86*0e209d39SAndroid Build Coastguard Worker /** Destructor 87*0e209d39SAndroid Build Coastguard Worker * Cleans pieces 88*0e209d39SAndroid Build Coastguard Worker * @stable ICU 2.4 89*0e209d39SAndroid Build Coastguard Worker */ 90*0e209d39SAndroid Build Coastguard Worker virtual ~CanonicalIterator(); 91*0e209d39SAndroid Build Coastguard Worker 92*0e209d39SAndroid Build Coastguard Worker /** 93*0e209d39SAndroid Build Coastguard Worker * Gets the NFD form of the current source we are iterating over. 94*0e209d39SAndroid Build Coastguard Worker * @return gets the source: NOTE: it is the NFD form of source 95*0e209d39SAndroid Build Coastguard Worker * @stable ICU 2.4 96*0e209d39SAndroid Build Coastguard Worker */ 97*0e209d39SAndroid Build Coastguard Worker UnicodeString getSource(); 98*0e209d39SAndroid Build Coastguard Worker 99*0e209d39SAndroid Build Coastguard Worker /** 100*0e209d39SAndroid Build Coastguard Worker * Resets the iterator so that one can start again from the beginning. 101*0e209d39SAndroid Build Coastguard Worker * @stable ICU 2.4 102*0e209d39SAndroid Build Coastguard Worker */ 103*0e209d39SAndroid Build Coastguard Worker void reset(); 104*0e209d39SAndroid Build Coastguard Worker 105*0e209d39SAndroid Build Coastguard Worker /** 106*0e209d39SAndroid Build Coastguard Worker * Get the next canonically equivalent string. 107*0e209d39SAndroid Build Coastguard Worker * <br><b>Warning: The strings are not guaranteed to be in any particular order.</b> 108*0e209d39SAndroid Build Coastguard Worker * @return the next string that is canonically equivalent. A bogus string is returned when 109*0e209d39SAndroid Build Coastguard Worker * the iteration is done. 110*0e209d39SAndroid Build Coastguard Worker * @stable ICU 2.4 111*0e209d39SAndroid Build Coastguard Worker */ 112*0e209d39SAndroid Build Coastguard Worker UnicodeString next(); 113*0e209d39SAndroid Build Coastguard Worker 114*0e209d39SAndroid Build Coastguard Worker /** 115*0e209d39SAndroid Build Coastguard Worker * Set a new source for this iterator. Allows object reuse. 116*0e209d39SAndroid Build Coastguard Worker * @param newSource the source string to iterate against. This allows the same iterator to be used 117*0e209d39SAndroid Build Coastguard Worker * while changing the source string, saving object creation. 118*0e209d39SAndroid Build Coastguard Worker * @param status Fill-in parameter which receives the status of this operation. 119*0e209d39SAndroid Build Coastguard Worker * @stable ICU 2.4 120*0e209d39SAndroid Build Coastguard Worker */ 121*0e209d39SAndroid Build Coastguard Worker void setSource(const UnicodeString &newSource, UErrorCode &status); 122*0e209d39SAndroid Build Coastguard Worker 123*0e209d39SAndroid Build Coastguard Worker #ifndef U_HIDE_INTERNAL_API 124*0e209d39SAndroid Build Coastguard Worker /** 125*0e209d39SAndroid Build Coastguard Worker * Dumb recursive implementation of permutation. 126*0e209d39SAndroid Build Coastguard Worker * TODO: optimize 127*0e209d39SAndroid Build Coastguard Worker * @param source the string to find permutations for 128*0e209d39SAndroid Build Coastguard Worker * @param skipZeros determine if skip zeros 129*0e209d39SAndroid Build Coastguard Worker * @param result the results in a set. 130*0e209d39SAndroid Build Coastguard Worker * @param status Fill-in parameter which receives the status of this operation. 131*0e209d39SAndroid Build Coastguard Worker * @param depth depth of the call. 132*0e209d39SAndroid Build Coastguard Worker * @internal 133*0e209d39SAndroid Build Coastguard Worker */ 134*0e209d39SAndroid Build Coastguard Worker static void U_EXPORT2 permute(UnicodeString &source, UBool skipZeros, Hashtable *result, UErrorCode &status, int32_t depth=0); 135*0e209d39SAndroid Build Coastguard Worker #endif /* U_HIDE_INTERNAL_API */ 136*0e209d39SAndroid Build Coastguard Worker 137*0e209d39SAndroid Build Coastguard Worker /** 138*0e209d39SAndroid Build Coastguard Worker * ICU "poor man's RTTI", returns a UClassID for this class. 139*0e209d39SAndroid Build Coastguard Worker * 140*0e209d39SAndroid Build Coastguard Worker * @stable ICU 2.2 141*0e209d39SAndroid Build Coastguard Worker */ 142*0e209d39SAndroid Build Coastguard Worker static UClassID U_EXPORT2 getStaticClassID(); 143*0e209d39SAndroid Build Coastguard Worker 144*0e209d39SAndroid Build Coastguard Worker /** 145*0e209d39SAndroid Build Coastguard Worker * ICU "poor man's RTTI", returns a UClassID for the actual class. 146*0e209d39SAndroid Build Coastguard Worker * 147*0e209d39SAndroid Build Coastguard Worker * @stable ICU 2.2 148*0e209d39SAndroid Build Coastguard Worker */ 149*0e209d39SAndroid Build Coastguard Worker virtual UClassID getDynamicClassID() const override; 150*0e209d39SAndroid Build Coastguard Worker 151*0e209d39SAndroid Build Coastguard Worker private: 152*0e209d39SAndroid Build Coastguard Worker // ===================== PRIVATES ============================== 153*0e209d39SAndroid Build Coastguard Worker // private default constructor 154*0e209d39SAndroid Build Coastguard Worker CanonicalIterator() = delete; 155*0e209d39SAndroid Build Coastguard Worker 156*0e209d39SAndroid Build Coastguard Worker 157*0e209d39SAndroid Build Coastguard Worker /** 158*0e209d39SAndroid Build Coastguard Worker * Copy constructor. Private for now. 159*0e209d39SAndroid Build Coastguard Worker * @internal (private) 160*0e209d39SAndroid Build Coastguard Worker */ 161*0e209d39SAndroid Build Coastguard Worker CanonicalIterator(const CanonicalIterator& other) = delete; 162*0e209d39SAndroid Build Coastguard Worker 163*0e209d39SAndroid Build Coastguard Worker /** 164*0e209d39SAndroid Build Coastguard Worker * Assignment operator. Private for now. 165*0e209d39SAndroid Build Coastguard Worker * @internal (private) 166*0e209d39SAndroid Build Coastguard Worker */ 167*0e209d39SAndroid Build Coastguard Worker CanonicalIterator& operator=(const CanonicalIterator& other) = delete; 168*0e209d39SAndroid Build Coastguard Worker 169*0e209d39SAndroid Build Coastguard Worker // fields 170*0e209d39SAndroid Build Coastguard Worker UnicodeString source; 171*0e209d39SAndroid Build Coastguard Worker UBool done; 172*0e209d39SAndroid Build Coastguard Worker 173*0e209d39SAndroid Build Coastguard Worker // 2 dimensional array holds the pieces of the string with 174*0e209d39SAndroid Build Coastguard Worker // their different canonically equivalent representations 175*0e209d39SAndroid Build Coastguard Worker UnicodeString **pieces; 176*0e209d39SAndroid Build Coastguard Worker int32_t pieces_length; 177*0e209d39SAndroid Build Coastguard Worker int32_t *pieces_lengths; 178*0e209d39SAndroid Build Coastguard Worker 179*0e209d39SAndroid Build Coastguard Worker // current is used in iterating to combine pieces 180*0e209d39SAndroid Build Coastguard Worker int32_t *current; 181*0e209d39SAndroid Build Coastguard Worker int32_t current_length; 182*0e209d39SAndroid Build Coastguard Worker 183*0e209d39SAndroid Build Coastguard Worker // transient fields 184*0e209d39SAndroid Build Coastguard Worker UnicodeString buffer; 185*0e209d39SAndroid Build Coastguard Worker 186*0e209d39SAndroid Build Coastguard Worker const Normalizer2 *nfd; 187*0e209d39SAndroid Build Coastguard Worker const Normalizer2Impl *nfcImpl; 188*0e209d39SAndroid Build Coastguard Worker 189*0e209d39SAndroid Build Coastguard Worker // we have a segment, in NFD. Find all the strings that are canonically equivalent to it. 190*0e209d39SAndroid Build Coastguard Worker UnicodeString *getEquivalents(const UnicodeString &segment, int32_t &result_len, UErrorCode &status); //private String[] getEquivalents(String segment) 191*0e209d39SAndroid Build Coastguard Worker 192*0e209d39SAndroid Build Coastguard Worker //Set getEquivalents2(String segment); 193*0e209d39SAndroid Build Coastguard Worker Hashtable *getEquivalents2(Hashtable *fillinResult, const char16_t *segment, int32_t segLen, UErrorCode &status); 194*0e209d39SAndroid Build Coastguard Worker //Hashtable *getEquivalents2(const UnicodeString &segment, int32_t segLen, UErrorCode &status); 195*0e209d39SAndroid Build Coastguard Worker 196*0e209d39SAndroid Build Coastguard Worker /** 197*0e209d39SAndroid Build Coastguard Worker * See if the decomposition of cp2 is at segment starting at segmentPos 198*0e209d39SAndroid Build Coastguard Worker * (with canonical rearrangement!) 199*0e209d39SAndroid Build Coastguard Worker * If so, take the remainder, and return the equivalents 200*0e209d39SAndroid Build Coastguard Worker */ 201*0e209d39SAndroid Build Coastguard Worker //Set extract(int comp, String segment, int segmentPos, StringBuffer buffer); 202*0e209d39SAndroid Build Coastguard Worker Hashtable *extract(Hashtable *fillinResult, UChar32 comp, const char16_t *segment, int32_t segLen, int32_t segmentPos, UErrorCode &status); 203*0e209d39SAndroid Build Coastguard Worker //Hashtable *extract(UChar32 comp, const UnicodeString &segment, int32_t segLen, int32_t segmentPos, UErrorCode &status); 204*0e209d39SAndroid Build Coastguard Worker 205*0e209d39SAndroid Build Coastguard Worker void cleanPieces(); 206*0e209d39SAndroid Build Coastguard Worker 207*0e209d39SAndroid Build Coastguard Worker }; 208*0e209d39SAndroid Build Coastguard Worker 209*0e209d39SAndroid Build Coastguard Worker U_NAMESPACE_END 210*0e209d39SAndroid Build Coastguard Worker 211*0e209d39SAndroid Build Coastguard Worker #endif /* #if !UCONFIG_NO_NORMALIZATION */ 212*0e209d39SAndroid Build Coastguard Worker 213*0e209d39SAndroid Build Coastguard Worker #endif /* U_SHOW_CPLUSPLUS_API */ 214*0e209d39SAndroid Build Coastguard Worker 215*0e209d39SAndroid Build Coastguard Worker #endif 216