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