1 /* 2 ******************************************************************************* 3 * Copyright (c) 1996-2007, 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: 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://source.icu-project.org/repos/icu/icuhtml/trunk/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 * The source and result buffers must not be the same, nor overlap. 186 * 187 * @param source The string to normalize. 188 * @param sourceLength The length of source, or -1 if NUL-terminated. 189 * @param mode The normalization mode; one of UNORM_NONE, 190 * UNORM_NFD, UNORM_NFC, UNORM_NFKC, UNORM_NFKD, UNORM_DEFAULT. 191 * @param options The normalization options, ORed together (0 for no options). 192 * @param result A pointer to a buffer to receive the result string. 193 * The result string is NUL-terminated if possible. 194 * @param resultLength The maximum size of result. 195 * @param status A pointer to a UErrorCode to receive any errors. 196 * @return The total buffer size needed; if greater than resultLength, 197 * the output was truncated, and the error code is set to U_BUFFER_OVERFLOW_ERROR. 198 * @stable ICU 2.0 199 */ 200 U_STABLE int32_t U_EXPORT2 201 unorm_normalize(const UChar *source, int32_t sourceLength, 202 UNormalizationMode mode, int32_t options, 203 UChar *result, int32_t resultLength, 204 UErrorCode *status); 205 #endif 206 /** 207 * Result values for unorm_quickCheck(). 208 * For details see Unicode Technical Report 15. 209 * @stable ICU 2.0 210 */ 211 typedef enum UNormalizationCheckResult { 212 /** 213 * Indicates that string is not in the normalized format 214 */ 215 UNORM_NO, 216 /** 217 * Indicates that string is in the normalized format 218 */ 219 UNORM_YES, 220 /** 221 * Indicates that string cannot be determined if it is in the normalized 222 * format without further thorough checks. 223 */ 224 UNORM_MAYBE 225 } UNormalizationCheckResult; 226 #if !UCONFIG_NO_NORMALIZATION 227 /** 228 * Performing quick check on a string, to quickly determine if the string is 229 * in a particular normalization format. 230 * Three types of result can be returned UNORM_YES, UNORM_NO or 231 * UNORM_MAYBE. Result UNORM_YES indicates that the argument 232 * string is in the desired normalized format, UNORM_NO determines that 233 * argument string is not in the desired normalized format. A 234 * UNORM_MAYBE result indicates that a more thorough check is required, 235 * the user may have to put the string in its normalized form and compare the 236 * results. 237 * 238 * @param source string for determining if it is in a normalized format 239 * @param sourcelength length of source to test, or -1 if NUL-terminated 240 * @param mode which normalization form to test for 241 * @param status a pointer to a UErrorCode to receive any errors 242 * @return UNORM_YES, UNORM_NO or UNORM_MAYBE 243 * 244 * @see unorm_isNormalized 245 * @stable ICU 2.0 246 */ 247 U_STABLE UNormalizationCheckResult U_EXPORT2 248 unorm_quickCheck(const UChar *source, int32_t sourcelength, 249 UNormalizationMode mode, 250 UErrorCode *status); 251 252 /** 253 * Performing quick check on a string; same as unorm_quickCheck but 254 * takes an extra options parameter like most normalization functions. 255 * 256 * @param src String that is to be tested if it is in a normalization format. 257 * @param srcLength Length of source to test, or -1 if NUL-terminated. 258 * @param mode Which normalization form to test for. 259 * @param options The normalization options, ORed together (0 for no options). 260 * @param pErrorCode ICU error code in/out parameter. 261 * Must fulfill U_SUCCESS before the function call. 262 * @return UNORM_YES, UNORM_NO or UNORM_MAYBE 263 * 264 * @see unorm_quickCheck 265 * @see unorm_isNormalized 266 * @stable ICU 2.6 267 */ 268 U_STABLE UNormalizationCheckResult U_EXPORT2 269 unorm_quickCheckWithOptions(const UChar *src, int32_t srcLength, 270 UNormalizationMode mode, int32_t options, 271 UErrorCode *pErrorCode); 272 273 /** 274 * Test if a string is in a given normalization form. 275 * This is semantically equivalent to source.equals(normalize(source, mode)) . 276 * 277 * Unlike unorm_quickCheck(), this function returns a definitive result, 278 * never a "maybe". 279 * For NFD, NFKD, and FCD, both functions work exactly the same. 280 * For NFC and NFKC where quickCheck may return "maybe", this function will 281 * perform further tests to arrive at a TRUE/FALSE result. 282 * 283 * @param src String that is to be tested if it is in a normalization format. 284 * @param srcLength Length of source to test, or -1 if NUL-terminated. 285 * @param mode Which normalization form to test for. 286 * @param pErrorCode ICU error code in/out parameter. 287 * Must fulfill U_SUCCESS before the function call. 288 * @return Boolean value indicating whether the source string is in the 289 * "mode" normalization form. 290 * 291 * @see unorm_quickCheck 292 * @stable ICU 2.2 293 */ 294 U_STABLE UBool U_EXPORT2 295 unorm_isNormalized(const UChar *src, int32_t srcLength, 296 UNormalizationMode mode, 297 UErrorCode *pErrorCode); 298 299 /** 300 * Test if a string is in a given normalization form; same as unorm_isNormalized but 301 * takes an extra options parameter like most normalization functions. 302 * 303 * @param src String that is to be tested if it is in a normalization format. 304 * @param srcLength Length of source to test, or -1 if NUL-terminated. 305 * @param mode Which normalization form to test for. 306 * @param options The normalization options, ORed together (0 for no options). 307 * @param pErrorCode ICU error code in/out parameter. 308 * Must fulfill U_SUCCESS before the function call. 309 * @return Boolean value indicating whether the source string is in the 310 * "mode/options" normalization form. 311 * 312 * @see unorm_quickCheck 313 * @see unorm_isNormalized 314 * @stable ICU 2.6 315 */ 316 U_STABLE UBool U_EXPORT2 317 unorm_isNormalizedWithOptions(const UChar *src, int32_t srcLength, 318 UNormalizationMode mode, int32_t options, 319 UErrorCode *pErrorCode); 320 321 /** 322 * Iterative normalization forward. 323 * This function (together with unorm_previous) is somewhat 324 * similar to the C++ Normalizer class (see its non-static functions). 325 * 326 * Iterative normalization is useful when only a small portion of a longer 327 * string/text needs to be processed. 328 * 329 * For example, the likelihood may be high that processing the first 10% of some 330 * text will be sufficient to find certain data. 331 * Another example: When one wants to concatenate two normalized strings and get a 332 * normalized result, it is much more efficient to normalize just a small part of 333 * the result around the concatenation place instead of re-normalizing everything. 334 * 335 * The input text is an instance of the C character iteration API UCharIterator. 336 * It may wrap around a simple string, a CharacterIterator, a Replaceable, or any 337 * other kind of text object. 338 * 339 * If a buffer overflow occurs, then the caller needs to reset the iterator to the 340 * old index and call the function again with a larger buffer - if the caller cares 341 * for the actual output. 342 * Regardless of the output buffer, the iterator will always be moved to the next 343 * normalization boundary. 344 * 345 * This function (like unorm_previous) serves two purposes: 346 * 347 * 1) To find the next boundary so that the normalization of the part of the text 348 * from the current position to that boundary does not affect and is not affected 349 * by the part of the text beyond that boundary. 350 * 351 * 2) To normalize the text up to the boundary. 352 * 353 * The second step is optional, per the doNormalize parameter. 354 * It is omitted for operations like string concatenation, where the two adjacent 355 * string ends need to be normalized together. 356 * In such a case, the output buffer will just contain a copy of the text up to the 357 * boundary. 358 * 359 * pNeededToNormalize is an output-only parameter. Its output value is only defined 360 * if normalization was requested (doNormalize) and successful (especially, no 361 * buffer overflow). 362 * It is useful for operations like a normalizing transliterator, where one would 363 * not want to replace a piece of text if it is not modified. 364 * 365 * If doNormalize==TRUE and pNeededToNormalize!=NULL then *pNeeded... is set TRUE 366 * if the normalization was necessary. 367 * 368 * If doNormalize==FALSE then *pNeededToNormalize will be set to FALSE. 369 * 370 * If the buffer overflows, then *pNeededToNormalize will be undefined; 371 * essentially, whenever U_FAILURE is true (like in buffer overflows), this result 372 * will be undefined. 373 * 374 * @param src The input text in the form of a C character iterator. 375 * @param dest The output buffer; can be NULL if destCapacity==0 for pure preflighting. 376 * @param destCapacity The number of UChars that fit into dest. 377 * @param mode The normalization mode. 378 * @param options The normalization options, ORed together (0 for no options). 379 * @param doNormalize Indicates if the source text up to the next boundary 380 * is to be normalized (TRUE) or just copied (FALSE). 381 * @param pNeededToNormalize Output flag indicating if the normalization resulted in 382 * different text from the input. 383 * Not defined if an error occurs including buffer overflow. 384 * Always FALSE if !doNormalize. 385 * @param pErrorCode ICU error code in/out parameter. 386 * Must fulfill U_SUCCESS before the function call. 387 * @return Length of output (number of UChars) when successful or buffer overflow. 388 * 389 * @see unorm_previous 390 * @see unorm_normalize 391 * 392 * @stable ICU 2.1 393 */ 394 U_STABLE int32_t U_EXPORT2 395 unorm_next(UCharIterator *src, 396 UChar *dest, int32_t destCapacity, 397 UNormalizationMode mode, int32_t options, 398 UBool doNormalize, UBool *pNeededToNormalize, 399 UErrorCode *pErrorCode); 400 401 /** 402 * Iterative normalization backward. 403 * This function (together with unorm_next) is somewhat 404 * similar to the C++ Normalizer class (see its non-static functions). 405 * For all details see unorm_next. 406 * 407 * @param src The input text in the form of a C character iterator. 408 * @param dest The output buffer; can be NULL if destCapacity==0 for pure preflighting. 409 * @param destCapacity The number of UChars that fit into dest. 410 * @param mode The normalization mode. 411 * @param options The normalization options, ORed together (0 for no options). 412 * @param doNormalize Indicates if the source text up to the next boundary 413 * is to be normalized (TRUE) or just copied (FALSE). 414 * @param pNeededToNormalize Output flag indicating if the normalization resulted in 415 * different text from the input. 416 * Not defined if an error occurs including buffer overflow. 417 * Always FALSE if !doNormalize. 418 * @param pErrorCode ICU error code in/out parameter. 419 * Must fulfill U_SUCCESS before the function call. 420 * @return Length of output (number of UChars) when successful or buffer overflow. 421 * 422 * @see unorm_next 423 * @see unorm_normalize 424 * 425 * @stable ICU 2.1 426 */ 427 U_STABLE int32_t U_EXPORT2 428 unorm_previous(UCharIterator *src, 429 UChar *dest, int32_t destCapacity, 430 UNormalizationMode mode, int32_t options, 431 UBool doNormalize, UBool *pNeededToNormalize, 432 UErrorCode *pErrorCode); 433 434 /** 435 * Concatenate normalized strings, making sure that the result is normalized as well. 436 * 437 * If both the left and the right strings are in 438 * the normalization form according to "mode/options", 439 * then the result will be 440 * 441 * \code 442 * dest=normalize(left+right, mode, options) 443 * \endcode 444 * 445 * With the input strings already being normalized, 446 * this function will use unorm_next() and unorm_previous() 447 * to find the adjacent end pieces of the input strings. 448 * Only the concatenation of these end pieces will be normalized and 449 * then concatenated with the remaining parts of the input strings. 450 * 451 * It is allowed to have dest==left to avoid copying the entire left string. 452 * 453 * @param left Left source string, may be same as dest. 454 * @param leftLength Length of left source string, or -1 if NUL-terminated. 455 * @param right Right source string. Must not be the same as dest, nor overlap. 456 * @param rightLength Length of right source string, or -1 if NUL-terminated. 457 * @param dest The output buffer; can be NULL if destCapacity==0 for pure preflighting. 458 * @param destCapacity The number of UChars that fit into dest. 459 * @param mode The normalization mode. 460 * @param options The normalization options, ORed together (0 for no options). 461 * @param pErrorCode ICU error code in/out parameter. 462 * Must fulfill U_SUCCESS before the function call. 463 * @return Length of output (number of UChars) when successful or buffer overflow. 464 * 465 * @see unorm_normalize 466 * @see unorm_next 467 * @see unorm_previous 468 * 469 * @stable ICU 2.1 470 */ 471 U_STABLE int32_t U_EXPORT2 472 unorm_concatenate(const UChar *left, int32_t leftLength, 473 const UChar *right, int32_t rightLength, 474 UChar *dest, int32_t destCapacity, 475 UNormalizationMode mode, int32_t options, 476 UErrorCode *pErrorCode); 477 478 /** 479 * Option bit for unorm_compare: 480 * Both input strings are assumed to fulfill FCD conditions. 481 * @stable ICU 2.2 482 */ 483 #define UNORM_INPUT_IS_FCD 0x20000 484 485 /** 486 * Option bit for unorm_compare: 487 * Perform case-insensitive comparison. 488 * @stable ICU 2.2 489 */ 490 #define U_COMPARE_IGNORE_CASE 0x10000 491 492 #ifndef U_COMPARE_CODE_POINT_ORDER 493 /* see also unistr.h and ustring.h */ 494 /** 495 * Option bit for u_strCaseCompare, u_strcasecmp, unorm_compare, etc: 496 * Compare strings in code point order instead of code unit order. 497 * @stable ICU 2.2 498 */ 499 #define U_COMPARE_CODE_POINT_ORDER 0x8000 500 #endif 501 502 /** 503 * Compare two strings for canonical equivalence. 504 * Further options include case-insensitive comparison and 505 * code point order (as opposed to code unit order). 506 * 507 * Canonical equivalence between two strings is defined as their normalized 508 * forms (NFD or NFC) being identical. 509 * This function compares strings incrementally instead of normalizing 510 * (and optionally case-folding) both strings entirely, 511 * improving performance significantly. 512 * 513 * Bulk normalization is only necessary if the strings do not fulfill the FCD 514 * conditions. Only in this case, and only if the strings are relatively long, 515 * is memory allocated temporarily. 516 * For FCD strings and short non-FCD strings there is no memory allocation. 517 * 518 * Semantically, this is equivalent to 519 * strcmp[CodePointOrder](NFD(foldCase(NFD(s1))), NFD(foldCase(NFD(s2)))) 520 * where code point order and foldCase are all optional. 521 * 522 * UAX 21 2.5 Caseless Matching specifies that for a canonical caseless match 523 * the case folding must be performed first, then the normalization. 524 * 525 * @param s1 First source string. 526 * @param length1 Length of first source string, or -1 if NUL-terminated. 527 * 528 * @param s2 Second source string. 529 * @param length2 Length of second source string, or -1 if NUL-terminated. 530 * 531 * @param options A bit set of options: 532 * - U_FOLD_CASE_DEFAULT or 0 is used for default options: 533 * Case-sensitive comparison in code unit order, and the input strings 534 * are quick-checked for FCD. 535 * 536 * - UNORM_INPUT_IS_FCD 537 * Set if the caller knows that both s1 and s2 fulfill the FCD conditions. 538 * If not set, the function will quickCheck for FCD 539 * and normalize if necessary. 540 * 541 * - U_COMPARE_CODE_POINT_ORDER 542 * Set to choose code point order instead of code unit order 543 * (see u_strCompare for details). 544 * 545 * - U_COMPARE_IGNORE_CASE 546 * Set to compare strings case-insensitively using case folding, 547 * instead of case-sensitively. 548 * If set, then the following case folding options are used. 549 * 550 * - Options as used with case-insensitive comparisons, currently: 551 * 552 * - U_FOLD_CASE_EXCLUDE_SPECIAL_I 553 * (see u_strCaseCompare for details) 554 * 555 * - regular normalization options shifted left by UNORM_COMPARE_NORM_OPTIONS_SHIFT 556 * 557 * @param pErrorCode ICU error code in/out parameter. 558 * Must fulfill U_SUCCESS before the function call. 559 * @return <0 or 0 or >0 as usual for string comparisons 560 * 561 * @see unorm_normalize 562 * @see UNORM_FCD 563 * @see u_strCompare 564 * @see u_strCaseCompare 565 * 566 * @stable ICU 2.2 567 */ 568 U_STABLE int32_t U_EXPORT2 569 unorm_compare(const UChar *s1, int32_t length1, 570 const UChar *s2, int32_t length2, 571 uint32_t options, 572 UErrorCode *pErrorCode); 573 574 #endif /* #if !UCONFIG_NO_NORMALIZATION */ 575 576 #endif 577