caniter.h 7.4 KB

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