caniter.h 7.2 KB

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