jpayne@69: // © 2016 and later: Unicode, Inc. and others.
jpayne@69: // License & terms of use: http://www.unicode.org/copyright.html
jpayne@69: /*
jpayne@69: *******************************************************************************
jpayne@69: *
jpayne@69: * Copyright (C) 2009-2013, International Business Machines
jpayne@69: * Corporation and others. All Rights Reserved.
jpayne@69: *
jpayne@69: *******************************************************************************
jpayne@69: * file name: normalizer2.h
jpayne@69: * encoding: UTF-8
jpayne@69: * tab size: 8 (not used)
jpayne@69: * indentation:4
jpayne@69: *
jpayne@69: * created on: 2009nov22
jpayne@69: * created by: Markus W. Scherer
jpayne@69: */
jpayne@69:
jpayne@69: #ifndef __NORMALIZER2_H__
jpayne@69: #define __NORMALIZER2_H__
jpayne@69:
jpayne@69: /**
jpayne@69: * \file
jpayne@69: * \brief C++ API: New API for Unicode Normalization.
jpayne@69: */
jpayne@69:
jpayne@69: #include "unicode/utypes.h"
jpayne@69:
jpayne@69: #if U_SHOW_CPLUSPLUS_API
jpayne@69:
jpayne@69: #if !UCONFIG_NO_NORMALIZATION
jpayne@69:
jpayne@69: #include "unicode/stringpiece.h"
jpayne@69: #include "unicode/uniset.h"
jpayne@69: #include "unicode/unistr.h"
jpayne@69: #include "unicode/unorm2.h"
jpayne@69:
jpayne@69: U_NAMESPACE_BEGIN
jpayne@69:
jpayne@69: class ByteSink;
jpayne@69:
jpayne@69: /**
jpayne@69: * Unicode normalization functionality for standard Unicode normalization or
jpayne@69: * for using custom mapping tables.
jpayne@69: * All instances of this class are unmodifiable/immutable.
jpayne@69: * Instances returned by getInstance() are singletons that must not be deleted by the caller.
jpayne@69: * The Normalizer2 class is not intended for public subclassing.
jpayne@69: *
jpayne@69: * The primary functions are to produce a normalized string and to detect whether
jpayne@69: * a string is already normalized.
jpayne@69: * The most commonly used normalization forms are those defined in
jpayne@69: * http://www.unicode.org/unicode/reports/tr15/
jpayne@69: * However, this API supports additional normalization forms for specialized purposes.
jpayne@69: * For example, NFKC_Casefold is provided via getInstance("nfkc_cf", COMPOSE)
jpayne@69: * and can be used in implementations of UTS #46.
jpayne@69: *
jpayne@69: * Not only are the standard compose and decompose modes supplied,
jpayne@69: * but additional modes are provided as documented in the Mode enum.
jpayne@69: *
jpayne@69: * Some of the functions in this class identify normalization boundaries.
jpayne@69: * At a normalization boundary, the portions of the string
jpayne@69: * before it and starting from it do not interact and can be handled independently.
jpayne@69: *
jpayne@69: * The spanQuickCheckYes() stops at a normalization boundary.
jpayne@69: * When the goal is a normalized string, then the text before the boundary
jpayne@69: * can be copied, and the remainder can be processed with normalizeSecondAndAppend().
jpayne@69: *
jpayne@69: * The hasBoundaryBefore(), hasBoundaryAfter() and isInert() functions test whether
jpayne@69: * a character is guaranteed to be at a normalization boundary,
jpayne@69: * regardless of context.
jpayne@69: * This is used for moving from one normalization boundary to the next
jpayne@69: * or preceding boundary, and for performing iterative normalization.
jpayne@69: *
jpayne@69: * Iterative normalization is useful when only a small portion of a
jpayne@69: * longer string needs to be processed.
jpayne@69: * For example, in ICU, iterative normalization is used by the NormalizationTransliterator
jpayne@69: * (to avoid replacing already-normalized text) and ucol_nextSortKeyPart()
jpayne@69: * (to process only the substring for which sort key bytes are computed).
jpayne@69: *
jpayne@69: * The set of normalization boundaries returned by these functions may not be
jpayne@69: * complete: There may be more boundaries that could be returned.
jpayne@69: * Different functions may return different boundaries.
jpayne@69: * @stable ICU 4.4
jpayne@69: */
jpayne@69: class U_COMMON_API Normalizer2 : public UObject {
jpayne@69: public:
jpayne@69: /**
jpayne@69: * Destructor.
jpayne@69: * @stable ICU 4.4
jpayne@69: */
jpayne@69: ~Normalizer2();
jpayne@69:
jpayne@69: /**
jpayne@69: * Returns a Normalizer2 instance for Unicode NFC normalization.
jpayne@69: * Same as getInstance(NULL, "nfc", UNORM2_COMPOSE, errorCode).
jpayne@69: * Returns an unmodifiable singleton instance. Do not delete it.
jpayne@69: * @param errorCode Standard ICU error code. Its input value must
jpayne@69: * pass the U_SUCCESS() test, or else the function returns
jpayne@69: * immediately. Check for U_FAILURE() on output or use with
jpayne@69: * function chaining. (See User Guide for details.)
jpayne@69: * @return the requested Normalizer2, if successful
jpayne@69: * @stable ICU 49
jpayne@69: */
jpayne@69: static const Normalizer2 *
jpayne@69: getNFCInstance(UErrorCode &errorCode);
jpayne@69:
jpayne@69: /**
jpayne@69: * Returns a Normalizer2 instance for Unicode NFD normalization.
jpayne@69: * Same as getInstance(NULL, "nfc", UNORM2_DECOMPOSE, errorCode).
jpayne@69: * Returns an unmodifiable singleton instance. Do not delete it.
jpayne@69: * @param errorCode Standard ICU error code. Its input value must
jpayne@69: * pass the U_SUCCESS() test, or else the function returns
jpayne@69: * immediately. Check for U_FAILURE() on output or use with
jpayne@69: * function chaining. (See User Guide for details.)
jpayne@69: * @return the requested Normalizer2, if successful
jpayne@69: * @stable ICU 49
jpayne@69: */
jpayne@69: static const Normalizer2 *
jpayne@69: getNFDInstance(UErrorCode &errorCode);
jpayne@69:
jpayne@69: /**
jpayne@69: * Returns a Normalizer2 instance for Unicode NFKC normalization.
jpayne@69: * Same as getInstance(NULL, "nfkc", UNORM2_COMPOSE, errorCode).
jpayne@69: * Returns an unmodifiable singleton instance. Do not delete it.
jpayne@69: * @param errorCode Standard ICU error code. Its input value must
jpayne@69: * pass the U_SUCCESS() test, or else the function returns
jpayne@69: * immediately. Check for U_FAILURE() on output or use with
jpayne@69: * function chaining. (See User Guide for details.)
jpayne@69: * @return the requested Normalizer2, if successful
jpayne@69: * @stable ICU 49
jpayne@69: */
jpayne@69: static const Normalizer2 *
jpayne@69: getNFKCInstance(UErrorCode &errorCode);
jpayne@69:
jpayne@69: /**
jpayne@69: * Returns a Normalizer2 instance for Unicode NFKD normalization.
jpayne@69: * Same as getInstance(NULL, "nfkc", UNORM2_DECOMPOSE, errorCode).
jpayne@69: * Returns an unmodifiable singleton instance. Do not delete it.
jpayne@69: * @param errorCode Standard ICU error code. Its input value must
jpayne@69: * pass the U_SUCCESS() test, or else the function returns
jpayne@69: * immediately. Check for U_FAILURE() on output or use with
jpayne@69: * function chaining. (See User Guide for details.)
jpayne@69: * @return the requested Normalizer2, if successful
jpayne@69: * @stable ICU 49
jpayne@69: */
jpayne@69: static const Normalizer2 *
jpayne@69: getNFKDInstance(UErrorCode &errorCode);
jpayne@69:
jpayne@69: /**
jpayne@69: * Returns a Normalizer2 instance for Unicode NFKC_Casefold normalization.
jpayne@69: * Same as getInstance(NULL, "nfkc_cf", UNORM2_COMPOSE, errorCode).
jpayne@69: * Returns an unmodifiable singleton instance. Do not delete it.
jpayne@69: * @param errorCode Standard ICU error code. Its input value must
jpayne@69: * pass the U_SUCCESS() test, or else the function returns
jpayne@69: * immediately. Check for U_FAILURE() on output or use with
jpayne@69: * function chaining. (See User Guide for details.)
jpayne@69: * @return the requested Normalizer2, if successful
jpayne@69: * @stable ICU 49
jpayne@69: */
jpayne@69: static const Normalizer2 *
jpayne@69: getNFKCCasefoldInstance(UErrorCode &errorCode);
jpayne@69:
jpayne@69: /**
jpayne@69: * Returns a Normalizer2 instance which uses the specified data file
jpayne@69: * (packageName/name similar to ucnv_openPackage() and ures_open()/ResourceBundle)
jpayne@69: * and which composes or decomposes text according to the specified mode.
jpayne@69: * Returns an unmodifiable singleton instance. Do not delete it.
jpayne@69: *
jpayne@69: * Use packageName=NULL for data files that are part of ICU's own data.
jpayne@69: * Use name="nfc" and UNORM2_COMPOSE/UNORM2_DECOMPOSE for Unicode standard NFC/NFD.
jpayne@69: * Use name="nfkc" and UNORM2_COMPOSE/UNORM2_DECOMPOSE for Unicode standard NFKC/NFKD.
jpayne@69: * Use name="nfkc_cf" and UNORM2_COMPOSE for Unicode standard NFKC_CF=NFKC_Casefold.
jpayne@69: *
jpayne@69: * @param packageName NULL for ICU built-in data, otherwise application data package name
jpayne@69: * @param name "nfc" or "nfkc" or "nfkc_cf" or name of custom data file
jpayne@69: * @param mode normalization mode (compose or decompose etc.)
jpayne@69: * @param errorCode Standard ICU error code. Its input value must
jpayne@69: * pass the U_SUCCESS() test, or else the function returns
jpayne@69: * immediately. Check for U_FAILURE() on output or use with
jpayne@69: * function chaining. (See User Guide for details.)
jpayne@69: * @return the requested Normalizer2, if successful
jpayne@69: * @stable ICU 4.4
jpayne@69: */
jpayne@69: static const Normalizer2 *
jpayne@69: getInstance(const char *packageName,
jpayne@69: const char *name,
jpayne@69: UNormalization2Mode mode,
jpayne@69: UErrorCode &errorCode);
jpayne@69:
jpayne@69: /**
jpayne@69: * Returns the normalized form of the source string.
jpayne@69: * @param src source string
jpayne@69: * @param errorCode Standard ICU error code. Its input value must
jpayne@69: * pass the U_SUCCESS() test, or else the function returns
jpayne@69: * immediately. Check for U_FAILURE() on output or use with
jpayne@69: * function chaining. (See User Guide for details.)
jpayne@69: * @return normalized src
jpayne@69: * @stable ICU 4.4
jpayne@69: */
jpayne@69: UnicodeString
jpayne@69: normalize(const UnicodeString &src, UErrorCode &errorCode) const {
jpayne@69: UnicodeString result;
jpayne@69: normalize(src, result, errorCode);
jpayne@69: return result;
jpayne@69: }
jpayne@69: /**
jpayne@69: * Writes the normalized form of the source string to the destination string
jpayne@69: * (replacing its contents) and returns the destination string.
jpayne@69: * The source and destination strings must be different objects.
jpayne@69: * @param src source string
jpayne@69: * @param dest destination string; its contents is replaced with normalized src
jpayne@69: * @param errorCode Standard ICU error code. Its input value must
jpayne@69: * pass the U_SUCCESS() test, or else the function returns
jpayne@69: * immediately. Check for U_FAILURE() on output or use with
jpayne@69: * function chaining. (See User Guide for details.)
jpayne@69: * @return dest
jpayne@69: * @stable ICU 4.4
jpayne@69: */
jpayne@69: virtual UnicodeString &
jpayne@69: normalize(const UnicodeString &src,
jpayne@69: UnicodeString &dest,
jpayne@69: UErrorCode &errorCode) const = 0;
jpayne@69:
jpayne@69: /**
jpayne@69: * Normalizes a UTF-8 string and optionally records how source substrings
jpayne@69: * relate to changed and unchanged result substrings.
jpayne@69: *
jpayne@69: * Currently implemented completely only for "compose" modes,
jpayne@69: * such as for NFC, NFKC, and NFKC_Casefold
jpayne@69: * (UNORM2_COMPOSE and UNORM2_COMPOSE_CONTIGUOUS).
jpayne@69: * Otherwise currently converts to & from UTF-16 and does not support edits.
jpayne@69: *
jpayne@69: * @param options Options bit set, usually 0. See U_OMIT_UNCHANGED_TEXT and U_EDITS_NO_RESET.
jpayne@69: * @param src Source UTF-8 string.
jpayne@69: * @param sink A ByteSink to which the normalized UTF-8 result string is written.
jpayne@69: * sink.Flush() is called at the end.
jpayne@69: * @param edits Records edits for index mapping, working with styled text,
jpayne@69: * and getting only changes (if any).
jpayne@69: * The Edits contents is undefined if any error occurs.
jpayne@69: * This function calls edits->reset() first unless
jpayne@69: * options includes U_EDITS_NO_RESET. edits can be nullptr.
jpayne@69: * @param errorCode Standard ICU error code. Its input value must
jpayne@69: * pass the U_SUCCESS() test, or else the function returns
jpayne@69: * immediately. Check for U_FAILURE() on output or use with
jpayne@69: * function chaining. (See User Guide for details.)
jpayne@69: * @stable ICU 60
jpayne@69: */
jpayne@69: virtual void
jpayne@69: normalizeUTF8(uint32_t options, StringPiece src, ByteSink &sink,
jpayne@69: Edits *edits, UErrorCode &errorCode) const;
jpayne@69:
jpayne@69: /**
jpayne@69: * Appends the normalized form of the second string to the first string
jpayne@69: * (merging them at the boundary) and returns the first string.
jpayne@69: * The result is normalized if the first string was normalized.
jpayne@69: * The first and second strings must be different objects.
jpayne@69: * @param first string, should be normalized
jpayne@69: * @param second string, will be normalized
jpayne@69: * @param errorCode Standard ICU error code. Its input value must
jpayne@69: * pass the U_SUCCESS() test, or else the function returns
jpayne@69: * immediately. Check for U_FAILURE() on output or use with
jpayne@69: * function chaining. (See User Guide for details.)
jpayne@69: * @return first
jpayne@69: * @stable ICU 4.4
jpayne@69: */
jpayne@69: virtual UnicodeString &
jpayne@69: normalizeSecondAndAppend(UnicodeString &first,
jpayne@69: const UnicodeString &second,
jpayne@69: UErrorCode &errorCode) const = 0;
jpayne@69: /**
jpayne@69: * Appends the second string to the first string
jpayne@69: * (merging them at the boundary) and returns the first string.
jpayne@69: * The result is normalized if both the strings were normalized.
jpayne@69: * The first and second strings must be different objects.
jpayne@69: * @param first string, should be normalized
jpayne@69: * @param second string, should be normalized
jpayne@69: * @param errorCode Standard ICU error code. Its input value must
jpayne@69: * pass the U_SUCCESS() test, or else the function returns
jpayne@69: * immediately. Check for U_FAILURE() on output or use with
jpayne@69: * function chaining. (See User Guide for details.)
jpayne@69: * @return first
jpayne@69: * @stable ICU 4.4
jpayne@69: */
jpayne@69: virtual UnicodeString &
jpayne@69: append(UnicodeString &first,
jpayne@69: const UnicodeString &second,
jpayne@69: UErrorCode &errorCode) const = 0;
jpayne@69:
jpayne@69: /**
jpayne@69: * Gets the decomposition mapping of c.
jpayne@69: * Roughly equivalent to normalizing the String form of c
jpayne@69: * on a UNORM2_DECOMPOSE Normalizer2 instance, but much faster, and except that this function
jpayne@69: * returns FALSE and does not write a string
jpayne@69: * if c does not have a decomposition mapping in this instance's data.
jpayne@69: * This function is independent of the mode of the Normalizer2.
jpayne@69: * @param c code point
jpayne@69: * @param decomposition String object which will be set to c's
jpayne@69: * decomposition mapping, if there is one.
jpayne@69: * @return TRUE if c has a decomposition, otherwise FALSE
jpayne@69: * @stable ICU 4.6
jpayne@69: */
jpayne@69: virtual UBool
jpayne@69: getDecomposition(UChar32 c, UnicodeString &decomposition) const = 0;
jpayne@69:
jpayne@69: /**
jpayne@69: * Gets the raw decomposition mapping of c.
jpayne@69: *
jpayne@69: * This is similar to the getDecomposition() method but returns the
jpayne@69: * raw decomposition mapping as specified in UnicodeData.txt or
jpayne@69: * (for custom data) in the mapping files processed by the gennorm2 tool.
jpayne@69: * By contrast, getDecomposition() returns the processed,
jpayne@69: * recursively-decomposed version of this mapping.
jpayne@69: *
jpayne@69: * When used on a standard NFKC Normalizer2 instance,
jpayne@69: * getRawDecomposition() returns the Unicode Decomposition_Mapping (dm) property.
jpayne@69: *
jpayne@69: * When used on a standard NFC Normalizer2 instance,
jpayne@69: * it returns the Decomposition_Mapping only if the Decomposition_Type (dt) is Canonical (Can);
jpayne@69: * in this case, the result contains either one or two code points (=1..4 char16_ts).
jpayne@69: *
jpayne@69: * This function is independent of the mode of the Normalizer2.
jpayne@69: * The default implementation returns FALSE.
jpayne@69: * @param c code point
jpayne@69: * @param decomposition String object which will be set to c's
jpayne@69: * raw decomposition mapping, if there is one.
jpayne@69: * @return TRUE if c has a decomposition, otherwise FALSE
jpayne@69: * @stable ICU 49
jpayne@69: */
jpayne@69: virtual UBool
jpayne@69: getRawDecomposition(UChar32 c, UnicodeString &decomposition) const;
jpayne@69:
jpayne@69: /**
jpayne@69: * Performs pairwise composition of a & b and returns the composite if there is one.
jpayne@69: *
jpayne@69: * Returns a composite code point c only if c has a two-way mapping to a+b.
jpayne@69: * In standard Unicode normalization, this means that
jpayne@69: * c has a canonical decomposition to a+b
jpayne@69: * and c does not have the Full_Composition_Exclusion property.
jpayne@69: *
jpayne@69: * This function is independent of the mode of the Normalizer2.
jpayne@69: * The default implementation returns a negative value.
jpayne@69: * @param a A (normalization starter) code point.
jpayne@69: * @param b Another code point.
jpayne@69: * @return The non-negative composite code point if there is one; otherwise a negative value.
jpayne@69: * @stable ICU 49
jpayne@69: */
jpayne@69: virtual UChar32
jpayne@69: composePair(UChar32 a, UChar32 b) const;
jpayne@69:
jpayne@69: /**
jpayne@69: * Gets the combining class of c.
jpayne@69: * The default implementation returns 0
jpayne@69: * but all standard implementations return the Unicode Canonical_Combining_Class value.
jpayne@69: * @param c code point
jpayne@69: * @return c's combining class
jpayne@69: * @stable ICU 49
jpayne@69: */
jpayne@69: virtual uint8_t
jpayne@69: getCombiningClass(UChar32 c) const;
jpayne@69:
jpayne@69: /**
jpayne@69: * Tests if the string is normalized.
jpayne@69: * Internally, in cases where the quickCheck() method would return "maybe"
jpayne@69: * (which is only possible for the two COMPOSE modes) this method
jpayne@69: * resolves to "yes" or "no" to provide a definitive result,
jpayne@69: * at the cost of doing more work in those cases.
jpayne@69: * @param s input string
jpayne@69: * @param errorCode Standard ICU error code. Its input value must
jpayne@69: * pass the U_SUCCESS() test, or else the function returns
jpayne@69: * immediately. Check for U_FAILURE() on output or use with
jpayne@69: * function chaining. (See User Guide for details.)
jpayne@69: * @return TRUE if s is normalized
jpayne@69: * @stable ICU 4.4
jpayne@69: */
jpayne@69: virtual UBool
jpayne@69: isNormalized(const UnicodeString &s, UErrorCode &errorCode) const = 0;
jpayne@69: /**
jpayne@69: * Tests if the UTF-8 string is normalized.
jpayne@69: * Internally, in cases where the quickCheck() method would return "maybe"
jpayne@69: * (which is only possible for the two COMPOSE modes) this method
jpayne@69: * resolves to "yes" or "no" to provide a definitive result,
jpayne@69: * at the cost of doing more work in those cases.
jpayne@69: *
jpayne@69: * This works for all normalization modes,
jpayne@69: * but it is currently optimized for UTF-8 only for "compose" modes,
jpayne@69: * such as for NFC, NFKC, and NFKC_Casefold
jpayne@69: * (UNORM2_COMPOSE and UNORM2_COMPOSE_CONTIGUOUS).
jpayne@69: * For other modes it currently converts to UTF-16 and calls isNormalized().
jpayne@69: *
jpayne@69: * @param s UTF-8 input string
jpayne@69: * @param errorCode Standard ICU error code. Its input value must
jpayne@69: * pass the U_SUCCESS() test, or else the function returns
jpayne@69: * immediately. Check for U_FAILURE() on output or use with
jpayne@69: * function chaining. (See User Guide for details.)
jpayne@69: * @return TRUE if s is normalized
jpayne@69: * @stable ICU 60
jpayne@69: */
jpayne@69: virtual UBool
jpayne@69: isNormalizedUTF8(StringPiece s, UErrorCode &errorCode) const;
jpayne@69:
jpayne@69:
jpayne@69: /**
jpayne@69: * Tests if the string is normalized.
jpayne@69: * For the two COMPOSE modes, the result could be "maybe" in cases that
jpayne@69: * would take a little more work to resolve definitively.
jpayne@69: * Use spanQuickCheckYes() and normalizeSecondAndAppend() for a faster
jpayne@69: * combination of quick check + normalization, to avoid
jpayne@69: * re-checking the "yes" prefix.
jpayne@69: * @param s input string
jpayne@69: * @param errorCode Standard ICU error code. Its input value must
jpayne@69: * pass the U_SUCCESS() test, or else the function returns
jpayne@69: * immediately. Check for U_FAILURE() on output or use with
jpayne@69: * function chaining. (See User Guide for details.)
jpayne@69: * @return UNormalizationCheckResult
jpayne@69: * @stable ICU 4.4
jpayne@69: */
jpayne@69: virtual UNormalizationCheckResult
jpayne@69: quickCheck(const UnicodeString &s, UErrorCode &errorCode) const = 0;
jpayne@69:
jpayne@69: /**
jpayne@69: * Returns the end of the normalized substring of the input string.
jpayne@69: * In other words, with end=spanQuickCheckYes(s, ec);
jpayne@69: * the substring UnicodeString(s, 0, end)
jpayne@69: * will pass the quick check with a "yes" result.
jpayne@69: *
jpayne@69: * The returned end index is usually one or more characters before the
jpayne@69: * "no" or "maybe" character: The end index is at a normalization boundary.
jpayne@69: * (See the class documentation for more about normalization boundaries.)
jpayne@69: *
jpayne@69: * When the goal is a normalized string and most input strings are expected
jpayne@69: * to be normalized already, then call this method,
jpayne@69: * and if it returns a prefix shorter than the input string,
jpayne@69: * copy that prefix and use normalizeSecondAndAppend() for the remainder.
jpayne@69: * @param s input string
jpayne@69: * @param errorCode Standard ICU error code. Its input value must
jpayne@69: * pass the U_SUCCESS() test, or else the function returns
jpayne@69: * immediately. Check for U_FAILURE() on output or use with
jpayne@69: * function chaining. (See User Guide for details.)
jpayne@69: * @return "yes" span end index
jpayne@69: * @stable ICU 4.4
jpayne@69: */
jpayne@69: virtual int32_t
jpayne@69: spanQuickCheckYes(const UnicodeString &s, UErrorCode &errorCode) const = 0;
jpayne@69:
jpayne@69: /**
jpayne@69: * Tests if the character always has a normalization boundary before it,
jpayne@69: * regardless of context.
jpayne@69: * If true, then the character does not normalization-interact with
jpayne@69: * preceding characters.
jpayne@69: * In other words, a string containing this character can be normalized
jpayne@69: * by processing portions before this character and starting from this
jpayne@69: * character independently.
jpayne@69: * This is used for iterative normalization. See the class documentation for details.
jpayne@69: * @param c character to test
jpayne@69: * @return TRUE if c has a normalization boundary before it
jpayne@69: * @stable ICU 4.4
jpayne@69: */
jpayne@69: virtual UBool hasBoundaryBefore(UChar32 c) const = 0;
jpayne@69:
jpayne@69: /**
jpayne@69: * Tests if the character always has a normalization boundary after it,
jpayne@69: * regardless of context.
jpayne@69: * If true, then the character does not normalization-interact with
jpayne@69: * following characters.
jpayne@69: * In other words, a string containing this character can be normalized
jpayne@69: * by processing portions up to this character and after this
jpayne@69: * character independently.
jpayne@69: * This is used for iterative normalization. See the class documentation for details.
jpayne@69: * Note that this operation may be significantly slower than hasBoundaryBefore().
jpayne@69: * @param c character to test
jpayne@69: * @return TRUE if c has a normalization boundary after it
jpayne@69: * @stable ICU 4.4
jpayne@69: */
jpayne@69: virtual UBool hasBoundaryAfter(UChar32 c) const = 0;
jpayne@69:
jpayne@69: /**
jpayne@69: * Tests if the character is normalization-inert.
jpayne@69: * If true, then the character does not change, nor normalization-interact with
jpayne@69: * preceding or following characters.
jpayne@69: * In other words, a string containing this character can be normalized
jpayne@69: * by processing portions before this character and after this
jpayne@69: * character independently.
jpayne@69: * This is used for iterative normalization. See the class documentation for details.
jpayne@69: * Note that this operation may be significantly slower than hasBoundaryBefore().
jpayne@69: * @param c character to test
jpayne@69: * @return TRUE if c is normalization-inert
jpayne@69: * @stable ICU 4.4
jpayne@69: */
jpayne@69: virtual UBool isInert(UChar32 c) const = 0;
jpayne@69: };
jpayne@69:
jpayne@69: /**
jpayne@69: * Normalization filtered by a UnicodeSet.
jpayne@69: * Normalizes portions of the text contained in the filter set and leaves
jpayne@69: * portions not contained in the filter set unchanged.
jpayne@69: * Filtering is done via UnicodeSet::span(..., USET_SPAN_SIMPLE).
jpayne@69: * Not-in-the-filter text is treated as "is normalized" and "quick check yes".
jpayne@69: * This class implements all of (and only) the Normalizer2 API.
jpayne@69: * An instance of this class is unmodifiable/immutable but is constructed and
jpayne@69: * must be destructed by the owner.
jpayne@69: * @stable ICU 4.4
jpayne@69: */
jpayne@69: class U_COMMON_API FilteredNormalizer2 : public Normalizer2 {
jpayne@69: public:
jpayne@69: /**
jpayne@69: * Constructs a filtered normalizer wrapping any Normalizer2 instance
jpayne@69: * and a filter set.
jpayne@69: * Both are aliased and must not be modified or deleted while this object
jpayne@69: * is used.
jpayne@69: * The filter set should be frozen; otherwise the performance will suffer greatly.
jpayne@69: * @param n2 wrapped Normalizer2 instance
jpayne@69: * @param filterSet UnicodeSet which determines the characters to be normalized
jpayne@69: * @stable ICU 4.4
jpayne@69: */
jpayne@69: FilteredNormalizer2(const Normalizer2 &n2, const UnicodeSet &filterSet) :
jpayne@69: norm2(n2), set(filterSet) {}
jpayne@69:
jpayne@69: /**
jpayne@69: * Destructor.
jpayne@69: * @stable ICU 4.4
jpayne@69: */
jpayne@69: ~FilteredNormalizer2();
jpayne@69:
jpayne@69: /**
jpayne@69: * Writes the normalized form of the source string to the destination string
jpayne@69: * (replacing its contents) and returns the destination string.
jpayne@69: * The source and destination strings must be different objects.
jpayne@69: * @param src source string
jpayne@69: * @param dest destination string; its contents is replaced with normalized src
jpayne@69: * @param errorCode Standard ICU error code. Its input value must
jpayne@69: * pass the U_SUCCESS() test, or else the function returns
jpayne@69: * immediately. Check for U_FAILURE() on output or use with
jpayne@69: * function chaining. (See User Guide for details.)
jpayne@69: * @return dest
jpayne@69: * @stable ICU 4.4
jpayne@69: */
jpayne@69: virtual UnicodeString &
jpayne@69: normalize(const UnicodeString &src,
jpayne@69: UnicodeString &dest,
jpayne@69: UErrorCode &errorCode) const U_OVERRIDE;
jpayne@69:
jpayne@69: /**
jpayne@69: * Normalizes a UTF-8 string and optionally records how source substrings
jpayne@69: * relate to changed and unchanged result substrings.
jpayne@69: *
jpayne@69: * Currently implemented completely only for "compose" modes,
jpayne@69: * such as for NFC, NFKC, and NFKC_Casefold
jpayne@69: * (UNORM2_COMPOSE and UNORM2_COMPOSE_CONTIGUOUS).
jpayne@69: * Otherwise currently converts to & from UTF-16 and does not support edits.
jpayne@69: *
jpayne@69: * @param options Options bit set, usually 0. See U_OMIT_UNCHANGED_TEXT and U_EDITS_NO_RESET.
jpayne@69: * @param src Source UTF-8 string.
jpayne@69: * @param sink A ByteSink to which the normalized UTF-8 result string is written.
jpayne@69: * sink.Flush() is called at the end.
jpayne@69: * @param edits Records edits for index mapping, working with styled text,
jpayne@69: * and getting only changes (if any).
jpayne@69: * The Edits contents is undefined if any error occurs.
jpayne@69: * This function calls edits->reset() first unless
jpayne@69: * options includes U_EDITS_NO_RESET. edits can be nullptr.
jpayne@69: * @param errorCode Standard ICU error code. Its input value must
jpayne@69: * pass the U_SUCCESS() test, or else the function returns
jpayne@69: * immediately. Check for U_FAILURE() on output or use with
jpayne@69: * function chaining. (See User Guide for details.)
jpayne@69: * @stable ICU 60
jpayne@69: */
jpayne@69: virtual void
jpayne@69: normalizeUTF8(uint32_t options, StringPiece src, ByteSink &sink,
jpayne@69: Edits *edits, UErrorCode &errorCode) const U_OVERRIDE;
jpayne@69:
jpayne@69: /**
jpayne@69: * Appends the normalized form of the second string to the first string
jpayne@69: * (merging them at the boundary) and returns the first string.
jpayne@69: * The result is normalized if the first string was normalized.
jpayne@69: * The first and second strings must be different objects.
jpayne@69: * @param first string, should be normalized
jpayne@69: * @param second string, will be normalized
jpayne@69: * @param errorCode Standard ICU error code. Its input value must
jpayne@69: * pass the U_SUCCESS() test, or else the function returns
jpayne@69: * immediately. Check for U_FAILURE() on output or use with
jpayne@69: * function chaining. (See User Guide for details.)
jpayne@69: * @return first
jpayne@69: * @stable ICU 4.4
jpayne@69: */
jpayne@69: virtual UnicodeString &
jpayne@69: normalizeSecondAndAppend(UnicodeString &first,
jpayne@69: const UnicodeString &second,
jpayne@69: UErrorCode &errorCode) const U_OVERRIDE;
jpayne@69: /**
jpayne@69: * Appends the second string to the first string
jpayne@69: * (merging them at the boundary) and returns the first string.
jpayne@69: * The result is normalized if both the strings were normalized.
jpayne@69: * The first and second strings must be different objects.
jpayne@69: * @param first string, should be normalized
jpayne@69: * @param second string, should be normalized
jpayne@69: * @param errorCode Standard ICU error code. Its input value must
jpayne@69: * pass the U_SUCCESS() test, or else the function returns
jpayne@69: * immediately. Check for U_FAILURE() on output or use with
jpayne@69: * function chaining. (See User Guide for details.)
jpayne@69: * @return first
jpayne@69: * @stable ICU 4.4
jpayne@69: */
jpayne@69: virtual UnicodeString &
jpayne@69: append(UnicodeString &first,
jpayne@69: const UnicodeString &second,
jpayne@69: UErrorCode &errorCode) const U_OVERRIDE;
jpayne@69:
jpayne@69: /**
jpayne@69: * Gets the decomposition mapping of c.
jpayne@69: * For details see the base class documentation.
jpayne@69: *
jpayne@69: * This function is independent of the mode of the Normalizer2.
jpayne@69: * @param c code point
jpayne@69: * @param decomposition String object which will be set to c's
jpayne@69: * decomposition mapping, if there is one.
jpayne@69: * @return TRUE if c has a decomposition, otherwise FALSE
jpayne@69: * @stable ICU 4.6
jpayne@69: */
jpayne@69: virtual UBool
jpayne@69: getDecomposition(UChar32 c, UnicodeString &decomposition) const U_OVERRIDE;
jpayne@69:
jpayne@69: /**
jpayne@69: * Gets the raw decomposition mapping of c.
jpayne@69: * For details see the base class documentation.
jpayne@69: *
jpayne@69: * This function is independent of the mode of the Normalizer2.
jpayne@69: * @param c code point
jpayne@69: * @param decomposition String object which will be set to c's
jpayne@69: * raw decomposition mapping, if there is one.
jpayne@69: * @return TRUE if c has a decomposition, otherwise FALSE
jpayne@69: * @stable ICU 49
jpayne@69: */
jpayne@69: virtual UBool
jpayne@69: getRawDecomposition(UChar32 c, UnicodeString &decomposition) const U_OVERRIDE;
jpayne@69:
jpayne@69: /**
jpayne@69: * Performs pairwise composition of a & b and returns the composite if there is one.
jpayne@69: * For details see the base class documentation.
jpayne@69: *
jpayne@69: * This function is independent of the mode of the Normalizer2.
jpayne@69: * @param a A (normalization starter) code point.
jpayne@69: * @param b Another code point.
jpayne@69: * @return The non-negative composite code point if there is one; otherwise a negative value.
jpayne@69: * @stable ICU 49
jpayne@69: */
jpayne@69: virtual UChar32
jpayne@69: composePair(UChar32 a, UChar32 b) const U_OVERRIDE;
jpayne@69:
jpayne@69: /**
jpayne@69: * Gets the combining class of c.
jpayne@69: * The default implementation returns 0
jpayne@69: * but all standard implementations return the Unicode Canonical_Combining_Class value.
jpayne@69: * @param c code point
jpayne@69: * @return c's combining class
jpayne@69: * @stable ICU 49
jpayne@69: */
jpayne@69: virtual uint8_t
jpayne@69: getCombiningClass(UChar32 c) const U_OVERRIDE;
jpayne@69:
jpayne@69: /**
jpayne@69: * Tests if the string is normalized.
jpayne@69: * For details see the Normalizer2 base class documentation.
jpayne@69: * @param s input string
jpayne@69: * @param errorCode Standard ICU error code. Its input value must
jpayne@69: * pass the U_SUCCESS() test, or else the function returns
jpayne@69: * immediately. Check for U_FAILURE() on output or use with
jpayne@69: * function chaining. (See User Guide for details.)
jpayne@69: * @return TRUE if s is normalized
jpayne@69: * @stable ICU 4.4
jpayne@69: */
jpayne@69: virtual UBool
jpayne@69: isNormalized(const UnicodeString &s, UErrorCode &errorCode) const U_OVERRIDE;
jpayne@69: /**
jpayne@69: * Tests if the UTF-8 string is normalized.
jpayne@69: * Internally, in cases where the quickCheck() method would return "maybe"
jpayne@69: * (which is only possible for the two COMPOSE modes) this method
jpayne@69: * resolves to "yes" or "no" to provide a definitive result,
jpayne@69: * at the cost of doing more work in those cases.
jpayne@69: *
jpayne@69: * This works for all normalization modes,
jpayne@69: * but it is currently optimized for UTF-8 only for "compose" modes,
jpayne@69: * such as for NFC, NFKC, and NFKC_Casefold
jpayne@69: * (UNORM2_COMPOSE and UNORM2_COMPOSE_CONTIGUOUS).
jpayne@69: * For other modes it currently converts to UTF-16 and calls isNormalized().
jpayne@69: *
jpayne@69: * @param s UTF-8 input string
jpayne@69: * @param errorCode Standard ICU error code. Its input value must
jpayne@69: * pass the U_SUCCESS() test, or else the function returns
jpayne@69: * immediately. Check for U_FAILURE() on output or use with
jpayne@69: * function chaining. (See User Guide for details.)
jpayne@69: * @return TRUE if s is normalized
jpayne@69: * @stable ICU 60
jpayne@69: */
jpayne@69: virtual UBool
jpayne@69: isNormalizedUTF8(StringPiece s, UErrorCode &errorCode) const U_OVERRIDE;
jpayne@69: /**
jpayne@69: * Tests if the string is normalized.
jpayne@69: * For details see the Normalizer2 base class documentation.
jpayne@69: * @param s input string
jpayne@69: * @param errorCode Standard ICU error code. Its input value must
jpayne@69: * pass the U_SUCCESS() test, or else the function returns
jpayne@69: * immediately. Check for U_FAILURE() on output or use with
jpayne@69: * function chaining. (See User Guide for details.)
jpayne@69: * @return UNormalizationCheckResult
jpayne@69: * @stable ICU 4.4
jpayne@69: */
jpayne@69: virtual UNormalizationCheckResult
jpayne@69: quickCheck(const UnicodeString &s, UErrorCode &errorCode) const U_OVERRIDE;
jpayne@69: /**
jpayne@69: * Returns the end of the normalized substring of the input string.
jpayne@69: * For details see the Normalizer2 base class documentation.
jpayne@69: * @param s input string
jpayne@69: * @param errorCode Standard ICU error code. Its input value must
jpayne@69: * pass the U_SUCCESS() test, or else the function returns
jpayne@69: * immediately. Check for U_FAILURE() on output or use with
jpayne@69: * function chaining. (See User Guide for details.)
jpayne@69: * @return "yes" span end index
jpayne@69: * @stable ICU 4.4
jpayne@69: */
jpayne@69: virtual int32_t
jpayne@69: spanQuickCheckYes(const UnicodeString &s, UErrorCode &errorCode) const U_OVERRIDE;
jpayne@69:
jpayne@69: /**
jpayne@69: * Tests if the character always has a normalization boundary before it,
jpayne@69: * regardless of context.
jpayne@69: * For details see the Normalizer2 base class documentation.
jpayne@69: * @param c character to test
jpayne@69: * @return TRUE if c has a normalization boundary before it
jpayne@69: * @stable ICU 4.4
jpayne@69: */
jpayne@69: virtual UBool hasBoundaryBefore(UChar32 c) const U_OVERRIDE;
jpayne@69:
jpayne@69: /**
jpayne@69: * Tests if the character always has a normalization boundary after it,
jpayne@69: * regardless of context.
jpayne@69: * For details see the Normalizer2 base class documentation.
jpayne@69: * @param c character to test
jpayne@69: * @return TRUE if c has a normalization boundary after it
jpayne@69: * @stable ICU 4.4
jpayne@69: */
jpayne@69: virtual UBool hasBoundaryAfter(UChar32 c) const U_OVERRIDE;
jpayne@69:
jpayne@69: /**
jpayne@69: * Tests if the character is normalization-inert.
jpayne@69: * For details see the Normalizer2 base class documentation.
jpayne@69: * @param c character to test
jpayne@69: * @return TRUE if c is normalization-inert
jpayne@69: * @stable ICU 4.4
jpayne@69: */
jpayne@69: virtual UBool isInert(UChar32 c) const U_OVERRIDE;
jpayne@69: private:
jpayne@69: UnicodeString &
jpayne@69: normalize(const UnicodeString &src,
jpayne@69: UnicodeString &dest,
jpayne@69: USetSpanCondition spanCondition,
jpayne@69: UErrorCode &errorCode) const;
jpayne@69:
jpayne@69: void
jpayne@69: normalizeUTF8(uint32_t options, const char *src, int32_t length,
jpayne@69: ByteSink &sink, Edits *edits,
jpayne@69: USetSpanCondition spanCondition,
jpayne@69: UErrorCode &errorCode) const;
jpayne@69:
jpayne@69: UnicodeString &
jpayne@69: normalizeSecondAndAppend(UnicodeString &first,
jpayne@69: const UnicodeString &second,
jpayne@69: UBool doNormalize,
jpayne@69: UErrorCode &errorCode) const;
jpayne@69:
jpayne@69: const Normalizer2 &norm2;
jpayne@69: const UnicodeSet &set;
jpayne@69: };
jpayne@69:
jpayne@69: U_NAMESPACE_END
jpayne@69:
jpayne@69: #endif // !UCONFIG_NO_NORMALIZATION
jpayne@69:
jpayne@69: #endif /* U_SHOW_CPLUSPLUS_API */
jpayne@69:
jpayne@69: #endif // __NORMALIZER2_H__