caniter.h 7.5 KB

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