michael@0: /*
michael@0: *******************************************************************************
michael@0: * Copyright (C) 1996-2011, International Business Machines Corporation and
michael@0: * others. All Rights Reserved.
michael@0: *******************************************************************************
michael@0: */
michael@0:
michael@0: #ifndef CANITER_H
michael@0: #define CANITER_H
michael@0:
michael@0: #include "unicode/utypes.h"
michael@0:
michael@0: #if !UCONFIG_NO_NORMALIZATION
michael@0:
michael@0: #include "unicode/uobject.h"
michael@0: #include "unicode/unistr.h"
michael@0:
michael@0: /**
michael@0: * \file
michael@0: * \brief C++ API: Canonical Iterator
michael@0: */
michael@0:
michael@0: /** Should permutation skip characters with combining class zero
michael@0: * Should be either TRUE or FALSE. This is a compile time option
michael@0: * @stable ICU 2.4
michael@0: */
michael@0: #ifndef CANITER_SKIP_ZEROES
michael@0: #define CANITER_SKIP_ZEROES TRUE
michael@0: #endif
michael@0:
michael@0: U_NAMESPACE_BEGIN
michael@0:
michael@0: class Hashtable;
michael@0: class Normalizer2;
michael@0: class Normalizer2Impl;
michael@0:
michael@0: /**
michael@0: * This class allows one to iterate through all the strings that are canonically equivalent to a given
michael@0: * string. For example, here are some sample results:
michael@0: Results for: {LATIN CAPITAL LETTER A WITH RING ABOVE}{LATIN SMALL LETTER D}{COMBINING DOT ABOVE}{COMBINING CEDILLA}
michael@0: 1: \\u0041\\u030A\\u0064\\u0307\\u0327
michael@0: = {LATIN CAPITAL LETTER A}{COMBINING RING ABOVE}{LATIN SMALL LETTER D}{COMBINING DOT ABOVE}{COMBINING CEDILLA}
michael@0: 2: \\u0041\\u030A\\u0064\\u0327\\u0307
michael@0: = {LATIN CAPITAL LETTER A}{COMBINING RING ABOVE}{LATIN SMALL LETTER D}{COMBINING CEDILLA}{COMBINING DOT ABOVE}
michael@0: 3: \\u0041\\u030A\\u1E0B\\u0327
michael@0: = {LATIN CAPITAL LETTER A}{COMBINING RING ABOVE}{LATIN SMALL LETTER D WITH DOT ABOVE}{COMBINING CEDILLA}
michael@0: 4: \\u0041\\u030A\\u1E11\\u0307
michael@0: = {LATIN CAPITAL LETTER A}{COMBINING RING ABOVE}{LATIN SMALL LETTER D WITH CEDILLA}{COMBINING DOT ABOVE}
michael@0: 5: \\u00C5\\u0064\\u0307\\u0327
michael@0: = {LATIN CAPITAL LETTER A WITH RING ABOVE}{LATIN SMALL LETTER D}{COMBINING DOT ABOVE}{COMBINING CEDILLA}
michael@0: 6: \\u00C5\\u0064\\u0327\\u0307
michael@0: = {LATIN CAPITAL LETTER A WITH RING ABOVE}{LATIN SMALL LETTER D}{COMBINING CEDILLA}{COMBINING DOT ABOVE}
michael@0: 7: \\u00C5\\u1E0B\\u0327
michael@0: = {LATIN CAPITAL LETTER A WITH RING ABOVE}{LATIN SMALL LETTER D WITH DOT ABOVE}{COMBINING CEDILLA}
michael@0: 8: \\u00C5\\u1E11\\u0307
michael@0: = {LATIN CAPITAL LETTER A WITH RING ABOVE}{LATIN SMALL LETTER D WITH CEDILLA}{COMBINING DOT ABOVE}
michael@0: 9: \\u212B\\u0064\\u0307\\u0327
michael@0: = {ANGSTROM SIGN}{LATIN SMALL LETTER D}{COMBINING DOT ABOVE}{COMBINING CEDILLA}
michael@0: 10: \\u212B\\u0064\\u0327\\u0307
michael@0: = {ANGSTROM SIGN}{LATIN SMALL LETTER D}{COMBINING CEDILLA}{COMBINING DOT ABOVE}
michael@0: 11: \\u212B\\u1E0B\\u0327
michael@0: = {ANGSTROM SIGN}{LATIN SMALL LETTER D WITH DOT ABOVE}{COMBINING CEDILLA}
michael@0: 12: \\u212B\\u1E11\\u0307
michael@0: = {ANGSTROM SIGN}{LATIN SMALL LETTER D WITH CEDILLA}{COMBINING DOT ABOVE}
michael@0: *
Note: the code is intended for use with small strings, and is not suitable for larger ones,
michael@0: * since it has not been optimized for that situation.
michael@0: * Note, CanonicalIterator is not intended to be subclassed.
michael@0: * @author M. Davis
michael@0: * @author C++ port by V. Weinstein
michael@0: * @stable ICU 2.4
michael@0: */
michael@0: class U_COMMON_API CanonicalIterator : public UObject {
michael@0: public:
michael@0: /**
michael@0: * Construct a CanonicalIterator object
michael@0: * @param source string to get results for
michael@0: * @param status Fill-in parameter which receives the status of this operation.
michael@0: * @stable ICU 2.4
michael@0: */
michael@0: CanonicalIterator(const UnicodeString &source, UErrorCode &status);
michael@0:
michael@0: /** Destructor
michael@0: * Cleans pieces
michael@0: * @stable ICU 2.4
michael@0: */
michael@0: virtual ~CanonicalIterator();
michael@0:
michael@0: /**
michael@0: * Gets the NFD form of the current source we are iterating over.
michael@0: * @return gets the source: NOTE: it is the NFD form of source
michael@0: * @stable ICU 2.4
michael@0: */
michael@0: UnicodeString getSource();
michael@0:
michael@0: /**
michael@0: * Resets the iterator so that one can start again from the beginning.
michael@0: * @stable ICU 2.4
michael@0: */
michael@0: void reset();
michael@0:
michael@0: /**
michael@0: * Get the next canonically equivalent string.
michael@0: *
Warning: The strings are not guaranteed to be in any particular order.
michael@0: * @return the next string that is canonically equivalent. A bogus string is returned when
michael@0: * the iteration is done.
michael@0: * @stable ICU 2.4
michael@0: */
michael@0: UnicodeString next();
michael@0:
michael@0: /**
michael@0: * Set a new source for this iterator. Allows object reuse.
michael@0: * @param newSource the source string to iterate against. This allows the same iterator to be used
michael@0: * while changing the source string, saving object creation.
michael@0: * @param status Fill-in parameter which receives the status of this operation.
michael@0: * @stable ICU 2.4
michael@0: */
michael@0: void setSource(const UnicodeString &newSource, UErrorCode &status);
michael@0:
michael@0: #ifndef U_HIDE_INTERNAL_API
michael@0: /**
michael@0: * Dumb recursive implementation of permutation.
michael@0: * TODO: optimize
michael@0: * @param source the string to find permutations for
michael@0: * @param skipZeros determine if skip zeros
michael@0: * @param result the results in a set.
michael@0: * @param status Fill-in parameter which receives the status of this operation.
michael@0: * @internal
michael@0: */
michael@0: static void U_EXPORT2 permute(UnicodeString &source, UBool skipZeros, Hashtable *result, UErrorCode &status);
michael@0: #endif /* U_HIDE_INTERNAL_API */
michael@0:
michael@0: /**
michael@0: * ICU "poor man's RTTI", returns a UClassID for this class.
michael@0: *
michael@0: * @stable ICU 2.2
michael@0: */
michael@0: static UClassID U_EXPORT2 getStaticClassID();
michael@0:
michael@0: /**
michael@0: * ICU "poor man's RTTI", returns a UClassID for the actual class.
michael@0: *
michael@0: * @stable ICU 2.2
michael@0: */
michael@0: virtual UClassID getDynamicClassID() const;
michael@0:
michael@0: private:
michael@0: // ===================== PRIVATES ==============================
michael@0: // private default constructor
michael@0: CanonicalIterator();
michael@0:
michael@0:
michael@0: /**
michael@0: * Copy constructor. Private for now.
michael@0: * @internal
michael@0: */
michael@0: CanonicalIterator(const CanonicalIterator& other);
michael@0:
michael@0: /**
michael@0: * Assignment operator. Private for now.
michael@0: * @internal
michael@0: */
michael@0: CanonicalIterator& operator=(const CanonicalIterator& other);
michael@0:
michael@0: // fields
michael@0: UnicodeString source;
michael@0: UBool done;
michael@0:
michael@0: // 2 dimensional array holds the pieces of the string with
michael@0: // their different canonically equivalent representations
michael@0: UnicodeString **pieces;
michael@0: int32_t pieces_length;
michael@0: int32_t *pieces_lengths;
michael@0:
michael@0: // current is used in iterating to combine pieces
michael@0: int32_t *current;
michael@0: int32_t current_length;
michael@0:
michael@0: // transient fields
michael@0: UnicodeString buffer;
michael@0:
michael@0: const Normalizer2 &nfd;
michael@0: const Normalizer2Impl &nfcImpl;
michael@0:
michael@0: // we have a segment, in NFD. Find all the strings that are canonically equivalent to it.
michael@0: UnicodeString *getEquivalents(const UnicodeString &segment, int32_t &result_len, UErrorCode &status); //private String[] getEquivalents(String segment)
michael@0:
michael@0: //Set getEquivalents2(String segment);
michael@0: Hashtable *getEquivalents2(Hashtable *fillinResult, const UChar *segment, int32_t segLen, UErrorCode &status);
michael@0: //Hashtable *getEquivalents2(const UnicodeString &segment, int32_t segLen, UErrorCode &status);
michael@0:
michael@0: /**
michael@0: * See if the decomposition of cp2 is at segment starting at segmentPos
michael@0: * (with canonical rearrangment!)
michael@0: * If so, take the remainder, and return the equivalents
michael@0: */
michael@0: //Set extract(int comp, String segment, int segmentPos, StringBuffer buffer);
michael@0: Hashtable *extract(Hashtable *fillinResult, UChar32 comp, const UChar *segment, int32_t segLen, int32_t segmentPos, UErrorCode &status);
michael@0: //Hashtable *extract(UChar32 comp, const UnicodeString &segment, int32_t segLen, int32_t segmentPos, UErrorCode &status);
michael@0:
michael@0: void cleanPieces();
michael@0:
michael@0: };
michael@0:
michael@0: U_NAMESPACE_END
michael@0:
michael@0: #endif /* #if !UCONFIG_NO_NORMALIZATION */
michael@0:
michael@0: #endif