2 ********************************************************************
4 * Copyright (c) 1996-2004, International Business Machines Corporation and
5 * others. All Rights Reserved.
6 ********************************************************************
12 #include "unicode/utypes.h"
14 #if !UCONFIG_NO_NORMALIZATION
16 #include "unicode/uobject.h"
17 #include "unicode/unistr.h"
18 #include "unicode/chariter.h"
19 #include "unicode/unorm.h"
22 typedef struct UCharIterator UCharIterator
; /**< C typedef for struct UCharIterator. @stable ICU 2.1 */
26 * \brief C++ API: Unicode Normalization
28 * The Normalizer class consists of two parts:
29 * - static functions that normalize strings or test if strings are normalized
30 * - a Normalizer object is an iterator that takes any kind of text and
31 * provides iteration over its normalized form
33 * The Normalizer class is not suitable for subclassing.
35 * The static functions are basically wrappers around the C implementation,
36 * using UnicodeString instead of UChar*.
37 * For basic information about normalization forms and details about the C API
38 * please see the documentation in unorm.h.
40 * The iterator API with the Normalizer constructors and the non-static functions
41 * uses a CharacterIterator as input. It is possible to pass a string which
42 * is then internally wrapped in a CharacterIterator.
43 * The input text is not normalized all at once, but incrementally where needed
44 * (providing efficient random access).
45 * This allows to pass in a large text but spend only a small amount of time
46 * normalizing a small part of that text.
47 * However, if the entire text is normalized, then the iterator will be
48 * slower than normalizing the entire text at once and iterating over the result.
49 * A possible use of the Normalizer iterator is also to report an index into the
50 * original text that is close to where the normalized characters come from.
52 * <em>Important:</em> The iterator API was cleaned up significantly for ICU 2.0.
53 * The earlier implementation reported the getIndex() inconsistently,
54 * and previous() could not be used after setIndex(), next(), first(), and current().
56 * Normalizer allows to start normalizing from anywhere in the input text by
57 * calling setIndexOnly(), first(), or last().
58 * Without calling any of these, the iterator will start at the beginning of the text.
60 * At any time, next() returns the next normalized code point (UChar32),
61 * with post-increment semantics (like CharacterIterator::next32PostInc()).
62 * previous() returns the previous normalized code point (UChar32),
63 * with pre-decrement semantics (like CharacterIterator::previous32()).
65 * current() returns the current code point
66 * (respectively the one at the newly set index) without moving
67 * the getIndex(). Note that if the text at the current position
68 * needs to be normalized, then these functions will do that.
69 * (This is why current() is not const.)
70 * It is more efficient to call setIndexOnly() instead, which does not
73 * getIndex() always refers to the position in the input text where the normalized
74 * code points are returned from. It does not always change with each returned
76 * The code point that is returned from any of the functions
77 * corresponds to text at or after getIndex(), according to the
78 * function's iteration semantics (post-increment or pre-decrement).
80 * next() returns a code point from at or after the getIndex()
81 * from before the next() call. After the next() call, the getIndex()
82 * might have moved to where the next code point will be returned from
83 * (from a next() or current() call).
84 * This is semantically equivalent to array access with array[index++]
85 * (post-increment semantics).
87 * previous() returns a code point from at or after the getIndex()
88 * from after the previous() call.
89 * This is semantically equivalent to array access with array[--index]
90 * (pre-decrement semantics).
92 * Internally, the Normalizer iterator normalizes a small piece of text
93 * starting at the getIndex() and ending at a following "safe" index.
94 * The normalized results is stored in an internal string buffer, and
95 * the code points are iterated from there.
96 * With multiple iteration calls, this is repeated until the next piece
97 * of text needs to be normalized, and the getIndex() needs to be moved.
99 * The following "safe" index, the internal buffer, and the secondary
100 * iteration index into that buffer are not exposed on the API.
101 * This also means that it is currently not practical to return to
102 * a particular, arbitrary position in the text because one would need to
103 * know, and be able to set, in addition to the getIndex(), at least also the
104 * current index into the internal buffer.
105 * It is currently only possible to observe when getIndex() changes
106 * (with careful consideration of the iteration semantics),
107 * at which time the internal index will be 0.
108 * For example, if getIndex() is different after next() than before it,
109 * then the internal index is 0 and one can return to this getIndex()
110 * later with setIndexOnly().
112 * @author Laura Werner, Mark Davis, Markus Scherer
115 class U_COMMON_API Normalizer
: public UObject
{
118 * If DONE is returned from an iteration function that returns a code point,
119 * then there are no more normalization results available.
129 * Creates a new <code>Normalizer</code> object for iterating over the
130 * normalized form of a given string.
132 * @param str The string to be normalized. The normalization
133 * will start at the beginning of the string.
135 * @param mode The normalization mode.
138 Normalizer(const UnicodeString
& str
, UNormalizationMode mode
);
141 * Creates a new <code>Normalizer</code> object for iterating over the
142 * normalized form of a given string.
144 * @param str The string to be normalized. The normalization
145 * will start at the beginning of the string.
147 * @param length Length of the string, or -1 if NUL-terminated.
148 * @param mode The normalization mode.
151 Normalizer(const UChar
* str
, int32_t length
, UNormalizationMode mode
);
154 * Creates a new <code>Normalizer</code> object for iterating over the
155 * normalized form of the given text.
157 * @param iter The input text to be normalized. The normalization
158 * will start at the beginning of the string.
160 * @param mode The normalization mode.
163 Normalizer(const CharacterIterator
& iter
, UNormalizationMode mode
);
167 * @param copy The object to be copied.
170 Normalizer(const Normalizer
& copy
);
176 virtual ~Normalizer();
179 //-------------------------------------------------------------------------
180 // Static utility methods
181 //-------------------------------------------------------------------------
184 * Normalizes a <code>UnicodeString</code> according to the specified normalization mode.
185 * This is a wrapper for unorm_normalize(), using UnicodeString's.
187 * The <code>options</code> parameter specifies which optional
188 * <code>Normalizer</code> features are to be enabled for this operation.
190 * @param source the input string to be normalized.
191 * @param mode the normalization mode
192 * @param options the optional features to be enabled (0 for no options)
193 * @param result The normalized string (on output).
194 * @param status The error code.
197 static void U_EXPORT2
normalize(const UnicodeString
& source
,
198 UNormalizationMode mode
, int32_t options
,
199 UnicodeString
& result
,
203 * Compose a <code>UnicodeString</code>.
204 * This is equivalent to normalize() with mode UNORM_NFC or UNORM_NFKC.
205 * This is a wrapper for unorm_normalize(), using UnicodeString's.
207 * The <code>options</code> parameter specifies which optional
208 * <code>Normalizer</code> features are to be enabled for this operation.
210 * @param source the string to be composed.
211 * @param compat Perform compatibility decomposition before composition.
212 * If this argument is <code>FALSE</code>, only canonical
213 * decomposition will be performed.
214 * @param options the optional features to be enabled (0 for no options)
215 * @param result The composed string (on output).
216 * @param status The error code.
219 static void U_EXPORT2
compose(const UnicodeString
& source
,
220 UBool compat
, int32_t options
,
221 UnicodeString
& result
,
225 * Static method to decompose a <code>UnicodeString</code>.
226 * This is equivalent to normalize() with mode UNORM_NFD or UNORM_NFKD.
227 * This is a wrapper for unorm_normalize(), using UnicodeString's.
229 * The <code>options</code> parameter specifies which optional
230 * <code>Normalizer</code> features are to be enabled for this operation.
232 * @param source the string to be decomposed.
233 * @param compat Perform compatibility decomposition.
234 * If this argument is <code>FALSE</code>, only canonical
235 * decomposition will be performed.
236 * @param options the optional features to be enabled (0 for no options)
237 * @param result The decomposed string (on output).
238 * @param status The error code.
241 static void U_EXPORT2
decompose(const UnicodeString
& source
,
242 UBool compat
, int32_t options
,
243 UnicodeString
& result
,
247 * Performing quick check on a string, to quickly determine if the string is
248 * in a particular normalization format.
249 * This is a wrapper for unorm_quickCheck(), using a UnicodeString.
251 * Three types of result can be returned UNORM_YES, UNORM_NO or
252 * UNORM_MAYBE. Result UNORM_YES indicates that the argument
253 * string is in the desired normalized format, UNORM_NO determines that
254 * argument string is not in the desired normalized format. A
255 * UNORM_MAYBE result indicates that a more thorough check is required,
256 * the user may have to put the string in its normalized form and compare the
258 * @param source string for determining if it is in a normalized format
259 * @param mode normalization format
260 * @param status A reference to a UErrorCode to receive any errors
261 * @return UNORM_YES, UNORM_NO or UNORM_MAYBE
266 static inline UNormalizationCheckResult
267 quickCheck(const UnicodeString
&source
, UNormalizationMode mode
, UErrorCode
&status
);
270 * Performing quick check on a string; same as the other version of quickCheck
271 * but takes an extra options parameter like most normalization functions.
273 * @param source string for determining if it is in a normalized format
274 * @param mode normalization format
275 * @param options the optional features to be enabled (0 for no options)
276 * @param status A reference to a UErrorCode to receive any errors
277 * @return UNORM_YES, UNORM_NO or UNORM_MAYBE
282 static inline UNormalizationCheckResult
283 quickCheck(const UnicodeString
&source
, UNormalizationMode mode
, int32_t options
, UErrorCode
&status
);
286 * Test if a string is in a given normalization form.
287 * This is semantically equivalent to source.equals(normalize(source, mode)) .
289 * Unlike unorm_quickCheck(), this function returns a definitive result,
291 * For NFD, NFKD, and FCD, both functions work exactly the same.
292 * For NFC and NFKC where quickCheck may return "maybe", this function will
293 * perform further tests to arrive at a TRUE/FALSE result.
295 * @param src String that is to be tested if it is in a normalization format.
296 * @param mode Which normalization form to test for.
297 * @param errorCode ICU error code in/out parameter.
298 * Must fulfill U_SUCCESS before the function call.
299 * @return Boolean value indicating whether the source string is in the
300 * "mode" normalization form.
306 isNormalized(const UnicodeString
&src
, UNormalizationMode mode
, UErrorCode
&errorCode
);
309 * Test if a string is in a given normalization form; same as the other version of isNormalized
310 * but takes an extra options parameter like most normalization functions.
312 * @param src String that is to be tested if it is in a normalization format.
313 * @param mode Which normalization form to test for.
314 * @param options the optional features to be enabled (0 for no options)
315 * @param errorCode ICU error code in/out parameter.
316 * Must fulfill U_SUCCESS before the function call.
317 * @return Boolean value indicating whether the source string is in the
318 * "mode" normalization form.
324 isNormalized(const UnicodeString
&src
, UNormalizationMode mode
, int32_t options
, UErrorCode
&errorCode
);
327 * Concatenate normalized strings, making sure that the result is normalized as well.
329 * If both the left and the right strings are in
330 * the normalization form according to "mode/options",
331 * then the result will be
334 * dest=normalize(left+right, mode, options)
337 * For details see unorm_concatenate in unorm.h.
339 * @param left Left source string.
340 * @param right Right source string.
341 * @param result The output string.
342 * @param mode The normalization mode.
343 * @param options A bit set of normalization options.
344 * @param errorCode ICU error code in/out parameter.
345 * Must fulfill U_SUCCESS before the function call.
348 * @see unorm_concatenate
351 * @see unorm_previous
355 static UnicodeString
&
356 U_EXPORT2
concatenate(UnicodeString
&left
, UnicodeString
&right
,
357 UnicodeString
&result
,
358 UNormalizationMode mode
, int32_t options
,
359 UErrorCode
&errorCode
);
362 * Compare two strings for canonical equivalence.
363 * Further options include case-insensitive comparison and
364 * code point order (as opposed to code unit order).
366 * Canonical equivalence between two strings is defined as their normalized
367 * forms (NFD or NFC) being identical.
368 * This function compares strings incrementally instead of normalizing
369 * (and optionally case-folding) both strings entirely,
370 * improving performance significantly.
372 * Bulk normalization is only necessary if the strings do not fulfill the FCD
373 * conditions. Only in this case, and only if the strings are relatively long,
374 * is memory allocated temporarily.
375 * For FCD strings and short non-FCD strings there is no memory allocation.
377 * Semantically, this is equivalent to
378 * strcmp[CodePointOrder](NFD(foldCase(s1)), NFD(foldCase(s2)))
379 * where code point order and foldCase are all optional.
381 * UAX 21 2.5 Caseless Matching specifies that for a canonical caseless match
382 * the case folding must be performed first, then the normalization.
384 * @param s1 First source string.
385 * @param s2 Second source string.
387 * @param options A bit set of options:
388 * - U_FOLD_CASE_DEFAULT or 0 is used for default options:
389 * Case-sensitive comparison in code unit order, and the input strings
390 * are quick-checked for FCD.
392 * - UNORM_INPUT_IS_FCD
393 * Set if the caller knows that both s1 and s2 fulfill the FCD conditions.
394 * If not set, the function will quickCheck for FCD
395 * and normalize if necessary.
397 * - U_COMPARE_CODE_POINT_ORDER
398 * Set to choose code point order instead of code unit order
399 * (see u_strCompare for details).
401 * - U_COMPARE_IGNORE_CASE
402 * Set to compare strings case-insensitively using case folding,
403 * instead of case-sensitively.
404 * If set, then the following case folding options are used.
406 * - Options as used with case-insensitive comparisons, currently:
408 * - U_FOLD_CASE_EXCLUDE_SPECIAL_I
409 * (see u_strCaseCompare for details)
411 * - regular normalization options shifted left by UNORM_COMPARE_NORM_OPTIONS_SHIFT
413 * @param errorCode ICU error code in/out parameter.
414 * Must fulfill U_SUCCESS before the function call.
415 * @return <0 or 0 or >0 as usual for string comparisons
421 * @see u_strCaseCompare
425 static inline int32_t
426 compare(const UnicodeString
&s1
, const UnicodeString
&s2
,
428 UErrorCode
&errorCode
);
430 //-------------------------------------------------------------------------
432 //-------------------------------------------------------------------------
435 * Return the current character in the normalized text.
436 * current() may need to normalize some text at getIndex().
437 * The getIndex() is not changed.
439 * @return the current normalized code point
442 UChar32
current(void);
445 * Return the first character in the normalized text.
446 * This is equivalent to setIndexOnly(startIndex()) followed by next().
447 * (Post-increment semantics.)
449 * @return the first normalized code point
455 * Return the last character in the normalized text.
456 * This is equivalent to setIndexOnly(endIndex()) followed by previous().
457 * (Pre-decrement semantics.)
459 * @return the last normalized code point
465 * Return the next character in the normalized text.
466 * (Post-increment semantics.)
467 * If the end of the text has already been reached, DONE is returned.
468 * The DONE value could be confused with a U+FFFF non-character code point
469 * in the text. If this is possible, you can test getIndex()<endIndex()
470 * before calling next(), or (getIndex()<endIndex() || last()!=DONE)
471 * after calling next(). (Calling last() will change the iterator state!)
473 * The C API unorm_next() is more efficient and does not have this ambiguity.
475 * @return the next normalized code point
481 * Return the previous character in the normalized text and decrement.
482 * (Pre-decrement semantics.)
483 * If the beginning of the text has already been reached, DONE is returned.
484 * The DONE value could be confused with a U+FFFF non-character code point
485 * in the text. If this is possible, you can test
486 * (getIndex()>startIndex() || first()!=DONE). (Calling first() will change
487 * the iterator state!)
489 * The C API unorm_previous() is more efficient and does not have this ambiguity.
491 * @return the previous normalized code point
494 UChar32
previous(void);
497 * Set the iteration position in the input text that is being normalized,
498 * without any immediate normalization.
499 * After setIndexOnly(), getIndex() will return the same index that is
502 * @param index the desired index in the input text.
505 void setIndexOnly(int32_t index
);
508 * Reset the index to the beginning of the text.
509 * This is equivalent to setIndexOnly(startIndex)).
515 * Retrieve the current iteration position in the input text that is
518 * A following call to next() will return a normalized code point from
519 * the input text at or after this index.
521 * After a call to previous(), getIndex() will point at or before the
522 * position in the input text where the normalized code point
523 * was returned from with previous().
525 * @return the current index in the input text
528 int32_t getIndex(void) const;
531 * Retrieve the index of the start of the input text. This is the begin index
532 * of the <code>CharacterIterator</code> or the start (i.e. index 0) of the string
533 * over which this <code>Normalizer</code> is iterating.
535 * @return the smallest index in the input text where the Normalizer operates
538 int32_t startIndex(void) const;
541 * Retrieve the index of the end of the input text. This is the end index
542 * of the <code>CharacterIterator</code> or the length of the string
543 * over which this <code>Normalizer</code> is iterating.
544 * This end index is exclusive, i.e., the Normalizer operates only on characters
547 * @return the first index in the input text where the Normalizer does not operate
550 int32_t endIndex(void) const;
553 * Returns TRUE when both iterators refer to the same character in the same
556 * @param that a Normalizer object to compare this one to
557 * @return comparison result
560 UBool
operator==(const Normalizer
& that
) const;
563 * Returns FALSE when both iterators refer to the same character in the same
566 * @param that a Normalizer object to compare this one to
567 * @return comparison result
570 inline UBool
operator!=(const Normalizer
& that
) const;
573 * Returns a pointer to a new Normalizer that is a clone of this one.
574 * The caller is responsible for deleting the new clone.
575 * @return a pointer to a new Normalizer
578 Normalizer
* clone(void) const;
581 * Generates a hash code for this iterator.
583 * @return the hash code
586 int32_t hashCode(void) const;
588 //-------------------------------------------------------------------------
589 // Property access methods
590 //-------------------------------------------------------------------------
593 * Set the normalization mode for this object.
595 * <b>Note:</b>If the normalization mode is changed while iterating
596 * over a string, calls to {@link #next() } and {@link #previous() } may
597 * return previously buffers characters in the old normalization mode
598 * until the iteration is able to re-sync at the next base character.
599 * It is safest to call {@link #setIndexOnly }, {@link #reset() },
600 * {@link #setText }, {@link #first() },
601 * {@link #last() }, etc. after calling <code>setMode</code>.
603 * @param newMode the new mode for this <code>Normalizer</code>.
607 void setMode(UNormalizationMode newMode
);
610 * Return the normalization mode for this object.
612 * This is an unusual name because there used to be a getMode() that
613 * returned a different type.
615 * @return the mode for this <code>Normalizer</code>
619 UNormalizationMode
getUMode(void) const;
622 * Set options that affect this <code>Normalizer</code>'s operation.
623 * Options do not change the basic composition or decomposition operation
624 * that is being performed, but they control whether
625 * certain optional portions of the operation are done.
626 * Currently the only available option is obsolete.
628 * It is possible to specify multiple options that are all turned on or off.
630 * @param option the option(s) whose value is/are to be set.
631 * @param value the new setting for the option. Use <code>TRUE</code> to
632 * turn the option(s) on and <code>FALSE</code> to turn it/them off.
637 void setOption(int32_t option
,
641 * Determine whether an option is turned on or off.
642 * If multiple options are specified, then the result is TRUE if any
645 * @param option the option(s) that are to be checked
646 * @return TRUE if any of the option(s) are set
650 UBool
getOption(int32_t option
) const;
653 * Set the input text over which this <code>Normalizer</code> will iterate.
654 * The iteration position is set to the beginning.
656 * @param newText a string that replaces the current input text
657 * @param status a UErrorCode
660 void setText(const UnicodeString
& newText
,
664 * Set the input text over which this <code>Normalizer</code> will iterate.
665 * The iteration position is set to the beginning.
667 * @param newText a CharacterIterator object that replaces the current input text
668 * @param status a UErrorCode
671 void setText(const CharacterIterator
& newText
,
675 * Set the input text over which this <code>Normalizer</code> will iterate.
676 * The iteration position is set to the beginning.
678 * @param newText a string that replaces the current input text
679 * @param length the length of the string, or -1 if NUL-terminated
680 * @param status a UErrorCode
683 void setText(const UChar
* newText
,
687 * Copies the input text into the UnicodeString argument.
689 * @param result Receives a copy of the text under iteration.
692 void getText(UnicodeString
& result
);
695 * ICU "poor man's RTTI", returns a UClassID for this class.
696 * @returns a UClassID for this class.
699 static UClassID U_EXPORT2
getStaticClassID();
702 * ICU "poor man's RTTI", returns a UClassID for the actual class.
703 * @return a UClassID for the actual class.
706 virtual UClassID
getDynamicClassID() const;
709 //-------------------------------------------------------------------------
711 //-------------------------------------------------------------------------
713 Normalizer(); // default constructor not implemented
714 Normalizer
&operator=(const Normalizer
&that
); // assignment operator not implemented
716 // Private utility methods for iteration
717 // For documentation, see the source code
718 UBool
nextNormalize();
719 UBool
previousNormalize();
721 void init(CharacterIterator
*iter
);
722 void clearBuffer(void);
724 //-------------------------------------------------------------------------
726 //-------------------------------------------------------------------------
728 UNormalizationMode fUMode
;
731 // The input text and our position in it
734 // The normalization buffer is the result of normalization
735 // of the source in [currentIndex..nextIndex[ .
736 int32_t currentIndex
, nextIndex
;
738 // A buffer for holding intermediate results
739 UnicodeString buffer
;
744 //-------------------------------------------------------------------------
745 // Inline implementations
746 //-------------------------------------------------------------------------
749 Normalizer::operator!= (const Normalizer
& other
) const
750 { return ! operator==(other
); }
752 inline UNormalizationCheckResult
753 Normalizer::quickCheck(const UnicodeString
& source
,
754 UNormalizationMode mode
,
755 UErrorCode
&status
) {
756 if(U_FAILURE(status
)) {
760 return unorm_quickCheck(source
.getBuffer(), source
.length(),
764 inline UNormalizationCheckResult
765 Normalizer::quickCheck(const UnicodeString
& source
,
766 UNormalizationMode mode
, int32_t options
,
767 UErrorCode
&status
) {
768 if(U_FAILURE(status
)) {
772 return unorm_quickCheckWithOptions(source
.getBuffer(), source
.length(),
773 mode
, options
, &status
);
777 Normalizer::isNormalized(const UnicodeString
& source
,
778 UNormalizationMode mode
,
779 UErrorCode
&status
) {
780 if(U_FAILURE(status
)) {
784 return unorm_isNormalized(source
.getBuffer(), source
.length(),
789 Normalizer::isNormalized(const UnicodeString
& source
,
790 UNormalizationMode mode
, int32_t options
,
791 UErrorCode
&status
) {
792 if(U_FAILURE(status
)) {
796 return unorm_isNormalizedWithOptions(source
.getBuffer(), source
.length(),
797 mode
, options
, &status
);
801 Normalizer::compare(const UnicodeString
&s1
, const UnicodeString
&s2
,
803 UErrorCode
&errorCode
) {
804 // all argument checking is done in unorm_compare
805 return unorm_compare(s1
.getBuffer(), s1
.length(),
806 s2
.getBuffer(), s2
.length(),
813 #endif /* #if !UCONFIG_NO_NORMALIZATION */