• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2 *******************************************************************************
3 * Copyright (c) 1996-2004, International Business Machines Corporation
4 *               and others. All Rights Reserved.
5 *******************************************************************************
6 * File unorm.h
7 *
8 * Created by: Vladimir Weinstein 12052000
9 *
10 * Modification history :
11 *
12 * Date        Name        Description
13 * 02/01/01    synwee      Added normalization quickcheck enum and method.
14 */
15 #ifndef UNORM_H
16 #define UNORM_H
17 
18 #include "unicode/utypes.h"
19 
20 #if !UCONFIG_NO_NORMALIZATION
21 
22 #include "unicode/uiter.h"
23 
24 /**
25  * \file
26  * \brief C API: Unicode Normalization
27  *
28  * <h2>Unicode normalization API</h2>
29  *
30  * <code>unorm_normalize</code> transforms Unicode text into an equivalent composed or
31  * decomposed form, allowing for easier sorting and searching of text.
32  * <code>unorm_normalize</code> supports the standard normalization forms described in
33  * <a href="http://www.unicode.org/unicode/reports/tr15/" target="unicode">
34  * Unicode Standard Annex #15 &#8212; Unicode Normalization Forms</a>.
35  *
36  * Characters with accents or other adornments can be encoded in
37  * several different ways in Unicode.  For example, take the character A-acute.
38  * In Unicode, this can be encoded as a single character (the
39  * "composed" form):
40  *
41  * \code
42  *      00C1    LATIN CAPITAL LETTER A WITH ACUTE
43  * \endcode
44  *
45  * or as two separate characters (the "decomposed" form):
46  *
47  * \code
48  *      0041    LATIN CAPITAL LETTER A
49  *      0301    COMBINING ACUTE ACCENT
50  * \endcode
51  *
52  * To a user of your program, however, both of these sequences should be
53  * treated as the same "user-level" character "A with acute accent".  When you are searching or
54  * comparing text, you must ensure that these two sequences are treated
55  * equivalently.  In addition, you must handle characters with more than one
56  * accent.  Sometimes the order of a character's combining accents is
57  * significant, while in other cases accent sequences in different orders are
58  * really equivalent.
59  *
60  * Similarly, the string "ffi" can be encoded as three separate letters:
61  *
62  * \code
63  *      0066    LATIN SMALL LETTER F
64  *      0066    LATIN SMALL LETTER F
65  *      0069    LATIN SMALL LETTER I
66  * \endcode
67  *
68  * or as the single character
69  *
70  * \code
71  *      FB03    LATIN SMALL LIGATURE FFI
72  * \endcode
73  *
74  * The ffi ligature is not a distinct semantic character, and strictly speaking
75  * it shouldn't be in Unicode at all, but it was included for compatibility
76  * with existing character sets that already provided it.  The Unicode standard
77  * identifies such characters by giving them "compatibility" decompositions
78  * into the corresponding semantic characters.  When sorting and searching, you
79  * will often want to use these mappings.
80  *
81  * <code>unorm_normalize</code> helps solve these problems by transforming text into the
82  * canonical composed and decomposed forms as shown in the first example above.
83  * In addition, you can have it perform compatibility decompositions so that
84  * you can treat compatibility characters the same as their equivalents.
85  * Finally, <code>unorm_normalize</code> rearranges accents into the proper canonical
86  * order, so that you do not have to worry about accent rearrangement on your
87  * own.
88  *
89  * Form FCD, "Fast C or D", is also designed for collation.
90  * It allows to work on strings that are not necessarily normalized
91  * with an algorithm (like in collation) that works under "canonical closure", i.e., it treats precomposed
92  * characters and their decomposed equivalents the same.
93  *
94  * It is not a normalization form because it does not provide for uniqueness of representation. Multiple strings
95  * may be canonically equivalent (their NFDs are identical) and may all conform to FCD without being identical
96  * themselves.
97  *
98  * The form is defined such that the "raw decomposition", the recursive canonical decomposition of each character,
99  * results in a string that is canonically ordered. This means that precomposed characters are allowed for as long
100  * as their decompositions do not need canonical reordering.
101  *
102  * Its advantage for a process like collation is that all NFD and most NFC texts - and many unnormalized texts -
103  * already conform to FCD and do not need to be normalized (NFD) for such a process. The FCD quick check will
104  * return UNORM_YES for most strings in practice.
105  *
106  * unorm_normalize(UNORM_FCD) may be implemented with UNORM_NFD.
107  *
108  * For more details on FCD see the collation design document:
109  * http://oss.software.ibm.com/cvs/icu/~checkout~/icuhtml/design/collation/ICU_collation_design.htm
110  *
111  * ICU collation performs either NFD or FCD normalization automatically if normalization
112  * is turned on for the collator object.
113  * Beyond collation and string search, normalized strings may be useful for string equivalence comparisons,
114  * transliteration/transcription, unique representations, etc.
115  *
116  * The W3C generally recommends to exchange texts in NFC.
117  * Note also that most legacy character encodings use only precomposed forms and often do not
118  * encode any combining marks by themselves. For conversion to such character encodings the
119  * Unicode text needs to be normalized to NFC.
120  * For more usage examples, see the Unicode Standard Annex.
121  */
122 
123 /**
124  * Constants for normalization modes.
125  * @stable ICU 2.0
126  */
127 typedef enum {
128   /** No decomposition/composition. @stable ICU 2.0 */
129   UNORM_NONE = 1,
130   /** Canonical decomposition. @stable ICU 2.0 */
131   UNORM_NFD = 2,
132   /** Compatibility decomposition. @stable ICU 2.0 */
133   UNORM_NFKD = 3,
134   /** Canonical decomposition followed by canonical composition. @stable ICU 2.0 */
135   UNORM_NFC = 4,
136   /** Default normalization. @stable ICU 2.0 */
137   UNORM_DEFAULT = UNORM_NFC,
138   /** Compatibility decomposition followed by canonical composition. @stable ICU 2.0 */
139   UNORM_NFKC =5,
140   /** "Fast C or D" form. @stable ICU 2.0 */
141   UNORM_FCD = 6,
142 
143   /** One more than the highest normalization mode constant. @stable ICU 2.0 */
144   UNORM_MODE_COUNT
145 } UNormalizationMode;
146 
147 /**
148  * Constants for options flags for normalization.
149  * Use 0 for default options,
150  * including normalization according to the Unicode version
151  * that is currently supported by ICU (see u_getUnicodeVersion).
152  * @stable ICU 2.6
153  */
154 enum {
155     /**
156      * Options bit set value to select Unicode 3.2 normalization
157      * (except NormalizationCorrections).
158      * At most one Unicode version can be selected at a time.
159      * @stable ICU 2.6
160      */
161     UNORM_UNICODE_3_2=0x20
162 };
163 
164 /**
165  * Lowest-order bit number of unorm_compare() options bits corresponding to
166  * normalization options bits.
167  *
168  * The options parameter for unorm_compare() uses most bits for
169  * itself and for various comparison and folding flags.
170  * The most significant bits, however, are shifted down and passed on
171  * to the normalization implementation.
172  * (That is, from unorm_compare(..., options, ...),
173  * options>>UNORM_COMPARE_NORM_OPTIONS_SHIFT will be passed on to the
174  * internal normalization functions.)
175  *
176  * @see unorm_compare
177  * @stable ICU 2.6
178  */
179 #define UNORM_COMPARE_NORM_OPTIONS_SHIFT 20
180 
181 /**
182  * Normalize a string.
183  * The string will be normalized according the specified normalization mode
184  * and options.
185  *
186  * @param source The string to normalize.
187  * @param sourceLength The length of source, or -1 if NUL-terminated.
188  * @param mode The normalization mode; one of UNORM_NONE,
189  *             UNORM_NFD, UNORM_NFC, UNORM_NFKC, UNORM_NFKD, UNORM_DEFAULT.
190  * @param options The normalization options, ORed together (0 for no options).
191  * @param result A pointer to a buffer to receive the result string.
192  *               The result string is NUL-terminated if possible.
193  * @param resultLength The maximum size of result.
194  * @param status A pointer to a UErrorCode to receive any errors.
195  * @return The total buffer size needed; if greater than resultLength,
196  *         the output was truncated, and the error code is set to U_BUFFER_OVERFLOW_ERROR.
197  * @stable ICU 2.0
198  */
199 U_STABLE int32_t U_EXPORT2
200 unorm_normalize(const UChar *source, int32_t sourceLength,
201                 UNormalizationMode mode, int32_t options,
202                 UChar *result, int32_t resultLength,
203                 UErrorCode *status);
204 #endif
205 /**
206  * Result values for unorm_quickCheck().
207  * For details see Unicode Technical Report 15.
208  * @stable ICU 2.0
209  */
210 typedef enum UNormalizationCheckResult {
211   /**
212    * Indicates that string is not in the normalized format
213    */
214   UNORM_NO,
215   /**
216    * Indicates that string is in the normalized format
217    */
218   UNORM_YES,
219   /**
220    * Indicates that string cannot be determined if it is in the normalized
221    * format without further thorough checks.
222    */
223   UNORM_MAYBE
224 } UNormalizationCheckResult;
225 #if !UCONFIG_NO_NORMALIZATION
226 /**
227  * Performing quick check on a string, to quickly determine if the string is
228  * in a particular normalization format.
229  * Three types of result can be returned UNORM_YES, UNORM_NO or
230  * UNORM_MAYBE. Result UNORM_YES indicates that the argument
231  * string is in the desired normalized format, UNORM_NO determines that
232  * argument string is not in the desired normalized format. A
233  * UNORM_MAYBE result indicates that a more thorough check is required,
234  * the user may have to put the string in its normalized form and compare the
235  * results.
236  *
237  * @param source       string for determining if it is in a normalized format
238  * @param sourcelength length of source to test, or -1 if NUL-terminated
239  * @param mode         which normalization form to test for
240  * @param status       a pointer to a UErrorCode to receive any errors
241  * @return UNORM_YES, UNORM_NO or UNORM_MAYBE
242  *
243  * @see unorm_isNormalized
244  * @stable ICU 2.0
245  */
246 U_STABLE UNormalizationCheckResult U_EXPORT2
247 unorm_quickCheck(const UChar *source, int32_t sourcelength,
248                  UNormalizationMode mode,
249                  UErrorCode *status);
250 
251 /**
252  * Performing quick check on a string; same as unorm_quickCheck but
253  * takes an extra options parameter like most normalization functions.
254  *
255  * @param src        String that is to be tested if it is in a normalization format.
256  * @param srcLength  Length of source to test, or -1 if NUL-terminated.
257  * @param mode       Which normalization form to test for.
258  * @param options    The normalization options, ORed together (0 for no options).
259  * @param pErrorCode ICU error code in/out parameter.
260  *                   Must fulfill U_SUCCESS before the function call.
261  * @return UNORM_YES, UNORM_NO or UNORM_MAYBE
262  *
263  * @see unorm_quickCheck
264  * @see unorm_isNormalized
265  * @stable ICU 2.6
266  */
267 U_STABLE UNormalizationCheckResult U_EXPORT2
268 unorm_quickCheckWithOptions(const UChar *src, int32_t srcLength,
269                             UNormalizationMode mode, int32_t options,
270                             UErrorCode *pErrorCode);
271 
272 /**
273  * Test if a string is in a given normalization form.
274  * This is semantically equivalent to source.equals(normalize(source, mode)) .
275  *
276  * Unlike unorm_quickCheck(), this function returns a definitive result,
277  * never a "maybe".
278  * For NFD, NFKD, and FCD, both functions work exactly the same.
279  * For NFC and NFKC where quickCheck may return "maybe", this function will
280  * perform further tests to arrive at a TRUE/FALSE result.
281  *
282  * @param src        String that is to be tested if it is in a normalization format.
283  * @param srcLength  Length of source to test, or -1 if NUL-terminated.
284  * @param mode       Which normalization form to test for.
285  * @param pErrorCode ICU error code in/out parameter.
286  *                   Must fulfill U_SUCCESS before the function call.
287  * @return Boolean value indicating whether the source string is in the
288  *         "mode" normalization form.
289  *
290  * @see unorm_quickCheck
291  * @stable ICU 2.2
292  */
293 U_STABLE UBool U_EXPORT2
294 unorm_isNormalized(const UChar *src, int32_t srcLength,
295                    UNormalizationMode mode,
296                    UErrorCode *pErrorCode);
297 
298 /**
299  * Test if a string is in a given normalization form; same as unorm_isNormalized but
300  * takes an extra options parameter like most normalization functions.
301  *
302  * @param src        String that is to be tested if it is in a normalization format.
303  * @param srcLength  Length of source to test, or -1 if NUL-terminated.
304  * @param mode       Which normalization form to test for.
305  * @param options    The normalization options, ORed together (0 for no options).
306  * @param pErrorCode ICU error code in/out parameter.
307  *                   Must fulfill U_SUCCESS before the function call.
308  * @return Boolean value indicating whether the source string is in the
309  *         "mode/options" normalization form.
310  *
311  * @see unorm_quickCheck
312  * @see unorm_isNormalized
313  * @stable ICU 2.6
314  */
315 U_STABLE UBool U_EXPORT2
316 unorm_isNormalizedWithOptions(const UChar *src, int32_t srcLength,
317                               UNormalizationMode mode, int32_t options,
318                               UErrorCode *pErrorCode);
319 
320 /**
321  * Iterative normalization forward.
322  * This function (together with unorm_previous) is somewhat
323  * similar to the C++ Normalizer class (see its non-static functions).
324  *
325  * Iterative normalization is useful when only a small portion of a longer
326  * string/text needs to be processed.
327  *
328  * For example, the likelihood may be high that processing the first 10% of some
329  * text will be sufficient to find certain data.
330  * Another example: When one wants to concatenate two normalized strings and get a
331  * normalized result, it is much more efficient to normalize just a small part of
332  * the result around the concatenation place instead of re-normalizing everything.
333  *
334  * The input text is an instance of the C character iteration API UCharIterator.
335  * It may wrap around a simple string, a CharacterIterator, a Replaceable, or any
336  * other kind of text object.
337  *
338  * If a buffer overflow occurs, then the caller needs to reset the iterator to the
339  * old index and call the function again with a larger buffer - if the caller cares
340  * for the actual output.
341  * Regardless of the output buffer, the iterator will always be moved to the next
342  * normalization boundary.
343  *
344  * This function (like unorm_previous) serves two purposes:
345  *
346  * 1) To find the next boundary so that the normalization of the part of the text
347  * from the current position to that boundary does not affect and is not affected
348  * by the part of the text beyond that boundary.
349  *
350  * 2) To normalize the text up to the boundary.
351  *
352  * The second step is optional, per the doNormalize parameter.
353  * It is omitted for operations like string concatenation, where the two adjacent
354  * string ends need to be normalized together.
355  * In such a case, the output buffer will just contain a copy of the text up to the
356  * boundary.
357  *
358  * pNeededToNormalize is an output-only parameter. Its output value is only defined
359  * if normalization was requested (doNormalize) and successful (especially, no
360  * buffer overflow).
361  * It is useful for operations like a normalizing transliterator, where one would
362  * not want to replace a piece of text if it is not modified.
363  *
364  * If doNormalize==TRUE and pNeededToNormalize!=NULL then *pNeeded... is set TRUE
365  * if the normalization was necessary.
366  *
367  * If doNormalize==FALSE then *pNeededToNormalize will be set to FALSE.
368  *
369  * If the buffer overflows, then *pNeededToNormalize will be undefined;
370  * essentially, whenever U_FAILURE is true (like in buffer overflows), this result
371  * will be undefined.
372  *
373  * @param src The input text in the form of a C character iterator.
374  * @param dest The output buffer; can be NULL if destCapacity==0 for pure preflighting.
375  * @param destCapacity The number of UChars that fit into dest.
376  * @param mode The normalization mode.
377  * @param options The normalization options, ORed together (0 for no options).
378  * @param doNormalize Indicates if the source text up to the next boundary
379  *                    is to be normalized (TRUE) or just copied (FALSE).
380  * @param pNeededToNormalize Output flag indicating if the normalization resulted in
381  *                           different text from the input.
382  *                           Not defined if an error occurs including buffer overflow.
383  *                           Always FALSE if !doNormalize.
384  * @param pErrorCode ICU error code in/out parameter.
385  *                   Must fulfill U_SUCCESS before the function call.
386  * @return Length of output (number of UChars) when successful or buffer overflow.
387  *
388  * @see unorm_previous
389  * @see unorm_normalize
390  *
391  * @stable ICU 2.1
392  */
393 U_STABLE int32_t U_EXPORT2
394 unorm_next(UCharIterator *src,
395            UChar *dest, int32_t destCapacity,
396            UNormalizationMode mode, int32_t options,
397            UBool doNormalize, UBool *pNeededToNormalize,
398            UErrorCode *pErrorCode);
399 
400 /**
401  * Iterative normalization backward.
402  * This function (together with unorm_next) is somewhat
403  * similar to the C++ Normalizer class (see its non-static functions).
404  * For all details see unorm_next.
405  *
406  * @param src The input text in the form of a C character iterator.
407  * @param dest The output buffer; can be NULL if destCapacity==0 for pure preflighting.
408  * @param destCapacity The number of UChars that fit into dest.
409  * @param mode The normalization mode.
410  * @param options The normalization options, ORed together (0 for no options).
411  * @param doNormalize Indicates if the source text up to the next boundary
412  *                    is to be normalized (TRUE) or just copied (FALSE).
413  * @param pNeededToNormalize Output flag indicating if the normalization resulted in
414  *                           different text from the input.
415  *                           Not defined if an error occurs including buffer overflow.
416  *                           Always FALSE if !doNormalize.
417  * @param pErrorCode ICU error code in/out parameter.
418  *                   Must fulfill U_SUCCESS before the function call.
419  * @return Length of output (number of UChars) when successful or buffer overflow.
420  *
421  * @see unorm_next
422  * @see unorm_normalize
423  *
424  * @stable ICU 2.1
425  */
426 U_STABLE int32_t U_EXPORT2
427 unorm_previous(UCharIterator *src,
428                UChar *dest, int32_t destCapacity,
429                UNormalizationMode mode, int32_t options,
430                UBool doNormalize, UBool *pNeededToNormalize,
431                UErrorCode *pErrorCode);
432 
433 /**
434  * Concatenate normalized strings, making sure that the result is normalized as well.
435  *
436  * If both the left and the right strings are in
437  * the normalization form according to "mode/options",
438  * then the result will be
439  *
440  * \code
441  *     dest=normalize(left+right, mode, options)
442  * \endcode
443  *
444  * With the input strings already being normalized,
445  * this function will use unorm_next() and unorm_previous()
446  * to find the adjacent end pieces of the input strings.
447  * Only the concatenation of these end pieces will be normalized and
448  * then concatenated with the remaining parts of the input strings.
449  *
450  * It is allowed to have dest==left to avoid copying the entire left string.
451  *
452  * @param left Left source string, may be same as dest.
453  * @param leftLength Length of left source string, or -1 if NUL-terminated.
454  * @param right Right source string.
455  * @param rightLength Length of right source string, or -1 if NUL-terminated.
456  * @param dest The output buffer; can be NULL if destCapacity==0 for pure preflighting.
457  * @param destCapacity The number of UChars that fit into dest.
458  * @param mode The normalization mode.
459  * @param options The normalization options, ORed together (0 for no options).
460  * @param pErrorCode ICU error code in/out parameter.
461  *                   Must fulfill U_SUCCESS before the function call.
462  * @return Length of output (number of UChars) when successful or buffer overflow.
463  *
464  * @see unorm_normalize
465  * @see unorm_next
466  * @see unorm_previous
467  *
468  * @stable ICU 2.1
469  */
470 U_STABLE int32_t U_EXPORT2
471 unorm_concatenate(const UChar *left, int32_t leftLength,
472                   const UChar *right, int32_t rightLength,
473                   UChar *dest, int32_t destCapacity,
474                   UNormalizationMode mode, int32_t options,
475                   UErrorCode *pErrorCode);
476 
477 /**
478  * Option bit for unorm_compare:
479  * Both input strings are assumed to fulfill FCD conditions.
480  * @stable ICU 2.2
481  */
482 #define UNORM_INPUT_IS_FCD          0x20000
483 
484 /**
485  * Option bit for unorm_compare:
486  * Perform case-insensitive comparison.
487  * @stable ICU 2.2
488  */
489 #define U_COMPARE_IGNORE_CASE       0x10000
490 
491 #ifndef U_COMPARE_CODE_POINT_ORDER
492 /* see also unistr.h and ustring.h */
493 /**
494  * Option bit for u_strCaseCompare, u_strcasecmp, unorm_compare, etc:
495  * Compare strings in code point order instead of code unit order.
496  * @stable ICU 2.2
497  */
498 #define U_COMPARE_CODE_POINT_ORDER  0x8000
499 #endif
500 
501 /**
502  * Compare two strings for canonical equivalence.
503  * Further options include case-insensitive comparison and
504  * code point order (as opposed to code unit order).
505  *
506  * Canonical equivalence between two strings is defined as their normalized
507  * forms (NFD or NFC) being identical.
508  * This function compares strings incrementally instead of normalizing
509  * (and optionally case-folding) both strings entirely,
510  * improving performance significantly.
511  *
512  * Bulk normalization is only necessary if the strings do not fulfill the FCD
513  * conditions. Only in this case, and only if the strings are relatively long,
514  * is memory allocated temporarily.
515  * For FCD strings and short non-FCD strings there is no memory allocation.
516  *
517  * Semantically, this is equivalent to
518  *   strcmp[CodePointOrder](NFD(foldCase(NFD(s1))), NFD(foldCase(NFD(s2))))
519  * where code point order and foldCase are all optional.
520  *
521  * UAX 21 2.5 Caseless Matching specifies that for a canonical caseless match
522  * the case folding must be performed first, then the normalization.
523  *
524  * @param s1 First source string.
525  * @param length1 Length of first source string, or -1 if NUL-terminated.
526  *
527  * @param s2 Second source string.
528  * @param length2 Length of second source string, or -1 if NUL-terminated.
529  *
530  * @param options A bit set of options:
531  *   - U_FOLD_CASE_DEFAULT or 0 is used for default options:
532  *     Case-sensitive comparison in code unit order, and the input strings
533  *     are quick-checked for FCD.
534  *
535  *   - UNORM_INPUT_IS_FCD
536  *     Set if the caller knows that both s1 and s2 fulfill the FCD conditions.
537  *     If not set, the function will quickCheck for FCD
538  *     and normalize if necessary.
539  *
540  *   - U_COMPARE_CODE_POINT_ORDER
541  *     Set to choose code point order instead of code unit order
542  *     (see u_strCompare for details).
543  *
544  *   - U_COMPARE_IGNORE_CASE
545  *     Set to compare strings case-insensitively using case folding,
546  *     instead of case-sensitively.
547  *     If set, then the following case folding options are used.
548  *
549  *   - Options as used with case-insensitive comparisons, currently:
550  *
551  *   - U_FOLD_CASE_EXCLUDE_SPECIAL_I
552  *    (see u_strCaseCompare for details)
553  *
554  *   - regular normalization options shifted left by UNORM_COMPARE_NORM_OPTIONS_SHIFT
555  *
556  * @param pErrorCode ICU error code in/out parameter.
557  *                   Must fulfill U_SUCCESS before the function call.
558  * @return <0 or 0 or >0 as usual for string comparisons
559  *
560  * @see unorm_normalize
561  * @see UNORM_FCD
562  * @see u_strCompare
563  * @see u_strCaseCompare
564  *
565  * @stable ICU 2.2
566  */
567 U_STABLE int32_t U_EXPORT2
568 unorm_compare(const UChar *s1, int32_t length1,
569               const UChar *s2, int32_t length2,
570               uint32_t options,
571               UErrorCode *pErrorCode);
572 
573 #endif /* #if !UCONFIG_NO_NORMALIZATION */
574 
575 #endif
576