Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 10:13:13

0001 // © 2016 and later: Unicode, Inc. and others.
0002 // License & terms of use: http://www.unicode.org/copyright.html
0003 /*
0004 *******************************************************************************
0005 * Copyright (c) 1996-2016, International Business Machines Corporation
0006 *               and others. All Rights Reserved.
0007 *******************************************************************************
0008 * File unorm.h
0009 *
0010 * Created by: Vladimir Weinstein 12052000
0011 *
0012 * Modification history :
0013 *
0014 * Date        Name        Description
0015 * 02/01/01    synwee      Added normalization quickcheck enum and method.
0016 */
0017 #ifndef UNORM_H
0018 #define UNORM_H
0019 
0020 #include "unicode/utypes.h"
0021 
0022 #if !UCONFIG_NO_NORMALIZATION
0023 
0024 #include "unicode/uiter.h"
0025 #include "unicode/unorm2.h"
0026 
0027 /**
0028  * \file
0029  * \brief C API: Unicode Normalization
0030  *
0031  * Old Unicode normalization API.
0032  *
0033  * This API has been replaced by the unorm2.h API and is only available
0034  * for backward compatibility. The functions here simply delegate to the
0035  * unorm2.h functions, for example unorm2_getInstance() and unorm2_normalize().
0036  * There is one exception: The new API does not provide a replacement for unorm_compare().
0037  * Its declaration has been moved to unorm2.h.
0038  *
0039  * <code>unorm_normalize</code> transforms Unicode text into an equivalent composed or
0040  * decomposed form, allowing for easier sorting and searching of text.
0041  * <code>unorm_normalize</code> supports the standard normalization forms described in
0042  * <a href="http://www.unicode.org/unicode/reports/tr15/" target="unicode">
0043  * Unicode Standard Annex #15: Unicode Normalization Forms</a>.
0044  *
0045  * Characters with accents or other adornments can be encoded in
0046  * several different ways in Unicode.  For example, take the character A-acute.
0047  * In Unicode, this can be encoded as a single character (the
0048  * "composed" form):
0049  *
0050  * \code
0051  *      00C1    LATIN CAPITAL LETTER A WITH ACUTE
0052  * \endcode
0053  *
0054  * or as two separate characters (the "decomposed" form):
0055  *
0056  * \code
0057  *      0041    LATIN CAPITAL LETTER A
0058  *      0301    COMBINING ACUTE ACCENT
0059  * \endcode
0060  *
0061  * To a user of your program, however, both of these sequences should be
0062  * treated as the same "user-level" character "A with acute accent".  When you are searching or
0063  * comparing text, you must ensure that these two sequences are treated
0064  * equivalently.  In addition, you must handle characters with more than one
0065  * accent.  Sometimes the order of a character's combining accents is
0066  * significant, while in other cases accent sequences in different orders are
0067  * really equivalent.
0068  *
0069  * Similarly, the string "ffi" can be encoded as three separate letters:
0070  *
0071  * \code
0072  *      0066    LATIN SMALL LETTER F
0073  *      0066    LATIN SMALL LETTER F
0074  *      0069    LATIN SMALL LETTER I
0075  * \endcode
0076  *
0077  * or as the single character
0078  *
0079  * \code
0080  *      FB03    LATIN SMALL LIGATURE FFI
0081  * \endcode
0082  *
0083  * The ffi ligature is not a distinct semantic character, and strictly speaking
0084  * it shouldn't be in Unicode at all, but it was included for compatibility
0085  * with existing character sets that already provided it.  The Unicode standard
0086  * identifies such characters by giving them "compatibility" decompositions
0087  * into the corresponding semantic characters.  When sorting and searching, you
0088  * will often want to use these mappings.
0089  *
0090  * <code>unorm_normalize</code> helps solve these problems by transforming text into the
0091  * canonical composed and decomposed forms as shown in the first example above.
0092  * In addition, you can have it perform compatibility decompositions so that
0093  * you can treat compatibility characters the same as their equivalents.
0094  * Finally, <code>unorm_normalize</code> rearranges accents into the proper canonical
0095  * order, so that you do not have to worry about accent rearrangement on your
0096  * own.
0097  *
0098  * Form FCD, "Fast C or D", is also designed for collation.
0099  * It allows to work on strings that are not necessarily normalized
0100  * with an algorithm (like in collation) that works under "canonical closure", i.e., it treats precomposed
0101  * characters and their decomposed equivalents the same.
0102  *
0103  * It is not a normalization form because it does not provide for uniqueness of representation. Multiple strings
0104  * may be canonically equivalent (their NFDs are identical) and may all conform to FCD without being identical
0105  * themselves.
0106  *
0107  * The form is defined such that the "raw decomposition", the recursive canonical decomposition of each character,
0108  * results in a string that is canonically ordered. This means that precomposed characters are allowed for as long
0109  * as their decompositions do not need canonical reordering.
0110  *
0111  * Its advantage for a process like collation is that all NFD and most NFC texts - and many unnormalized texts -
0112  * already conform to FCD and do not need to be normalized (NFD) for such a process. The FCD quick check will
0113  * return UNORM_YES for most strings in practice.
0114  *
0115  * unorm_normalize(UNORM_FCD) may be implemented with UNORM_NFD.
0116  *
0117  * For more details on FCD see the collation design document:
0118  * https://htmlpreview.github.io/?https://github.com/unicode-org/icu-docs/blob/main/design/collation/ICU_collation_design.htm
0119  *
0120  * ICU collation performs either NFD or FCD normalization automatically if normalization
0121  * is turned on for the collator object.
0122  * Beyond collation and string search, normalized strings may be useful for string equivalence comparisons,
0123  * transliteration/transcription, unique representations, etc.
0124  *
0125  * The W3C generally recommends to exchange texts in NFC.
0126  * Note also that most legacy character encodings use only precomposed forms and often do not
0127  * encode any combining marks by themselves. For conversion to such character encodings the
0128  * Unicode text needs to be normalized to NFC.
0129  * For more usage examples, see the Unicode Standard Annex.
0130  */
0131 
0132 // Do not conditionalize the following enum with #ifndef U_HIDE_DEPRECATED_API,
0133 // it is needed for layout of Normalizer object.
0134 #ifndef U_FORCE_HIDE_DEPRECATED_API
0135 
0136 /**
0137  * Constants for normalization modes.
0138  * @deprecated ICU 56 Use unorm2.h instead.
0139  */
0140 typedef enum {
0141   /** No decomposition/composition. @deprecated ICU 56 Use unorm2.h instead. */
0142   UNORM_NONE = 1,
0143   /** Canonical decomposition. @deprecated ICU 56 Use unorm2.h instead. */
0144   UNORM_NFD = 2,
0145   /** Compatibility decomposition. @deprecated ICU 56 Use unorm2.h instead. */
0146   UNORM_NFKD = 3,
0147   /** Canonical decomposition followed by canonical composition. @deprecated ICU 56 Use unorm2.h instead. */
0148   UNORM_NFC = 4,
0149   /** Default normalization. @deprecated ICU 56 Use unorm2.h instead. */
0150   UNORM_DEFAULT = UNORM_NFC,
0151   /** Compatibility decomposition followed by canonical composition. @deprecated ICU 56 Use unorm2.h instead. */
0152   UNORM_NFKC =5,
0153   /** "Fast C or D" form. @deprecated ICU 56 Use unorm2.h instead. */
0154   UNORM_FCD = 6,
0155 
0156   /** One more than the highest normalization mode constant. @deprecated ICU 56 Use unorm2.h instead. */
0157   UNORM_MODE_COUNT
0158 } UNormalizationMode;
0159 
0160 #endif  // U_FORCE_HIDE_DEPRECATED_API
0161 
0162 #ifndef U_HIDE_DEPRECATED_API
0163 
0164 /**
0165  * Constants for options flags for normalization.
0166  * Use 0 for default options,
0167  * including normalization according to the Unicode version
0168  * that is currently supported by ICU (see u_getUnicodeVersion).
0169  * @deprecated ICU 56 Use unorm2.h instead.
0170  */
0171 enum {
0172     /**
0173      * Options bit set value to select Unicode 3.2 normalization
0174      * (except NormalizationCorrections).
0175      * At most one Unicode version can be selected at a time.
0176      * @deprecated ICU 56 Use unorm2.h instead.
0177      */
0178     UNORM_UNICODE_3_2=0x20
0179 };
0180 
0181 /**
0182  * Lowest-order bit number of unorm_compare() options bits corresponding to
0183  * normalization options bits.
0184  *
0185  * The options parameter for unorm_compare() uses most bits for
0186  * itself and for various comparison and folding flags.
0187  * The most significant bits, however, are shifted down and passed on
0188  * to the normalization implementation.
0189  * (That is, from unorm_compare(..., options, ...),
0190  * options>>UNORM_COMPARE_NORM_OPTIONS_SHIFT will be passed on to the
0191  * internal normalization functions.)
0192  *
0193  * @see unorm_compare
0194  * @deprecated ICU 56 Use unorm2.h instead.
0195  */
0196 #define UNORM_COMPARE_NORM_OPTIONS_SHIFT 20
0197 
0198 /**
0199  * Normalize a string.
0200  * The string will be normalized according the specified normalization mode
0201  * and options.
0202  * The source and result buffers must not be the same, nor overlap.
0203  *
0204  * @param source The string to normalize.
0205  * @param sourceLength The length of source, or -1 if NUL-terminated.
0206  * @param mode The normalization mode; one of UNORM_NONE,
0207  *             UNORM_NFD, UNORM_NFC, UNORM_NFKC, UNORM_NFKD, UNORM_DEFAULT.
0208  * @param options The normalization options, ORed together (0 for no options).
0209  * @param result A pointer to a buffer to receive the result string.
0210  *               The result string is NUL-terminated if possible.
0211  * @param resultLength The maximum size of result.
0212  * @param status A pointer to a UErrorCode to receive any errors.
0213  * @return The total buffer size needed; if greater than resultLength,
0214  *         the output was truncated, and the error code is set to U_BUFFER_OVERFLOW_ERROR.
0215  * @deprecated ICU 56 Use unorm2.h instead.
0216  */
0217 U_DEPRECATED int32_t U_EXPORT2
0218 unorm_normalize(const UChar *source, int32_t sourceLength,
0219                 UNormalizationMode mode, int32_t options,
0220                 UChar *result, int32_t resultLength,
0221                 UErrorCode *status);
0222 
0223 /**
0224  * Performing quick check on a string, to quickly determine if the string is
0225  * in a particular normalization format.
0226  * Three types of result can be returned UNORM_YES, UNORM_NO or
0227  * UNORM_MAYBE. Result UNORM_YES indicates that the argument
0228  * string is in the desired normalized format, UNORM_NO determines that
0229  * argument string is not in the desired normalized format. A
0230  * UNORM_MAYBE result indicates that a more thorough check is required,
0231  * the user may have to put the string in its normalized form and compare the
0232  * results.
0233  *
0234  * @param source       string for determining if it is in a normalized format
0235  * @param sourcelength length of source to test, or -1 if NUL-terminated
0236  * @param mode         which normalization form to test for
0237  * @param status       a pointer to a UErrorCode to receive any errors
0238  * @return UNORM_YES, UNORM_NO or UNORM_MAYBE
0239  *
0240  * @see unorm_isNormalized
0241  * @deprecated ICU 56 Use unorm2.h instead.
0242  */
0243 U_DEPRECATED UNormalizationCheckResult U_EXPORT2
0244 unorm_quickCheck(const UChar *source, int32_t sourcelength,
0245                  UNormalizationMode mode,
0246                  UErrorCode *status);
0247 
0248 /**
0249  * Performing quick check on a string; same as unorm_quickCheck but
0250  * takes an extra options parameter like most normalization functions.
0251  *
0252  * @param src        String that is to be tested if it is in a normalization format.
0253  * @param srcLength  Length of source to test, or -1 if NUL-terminated.
0254  * @param mode       Which normalization form to test for.
0255  * @param options    The normalization options, ORed together (0 for no options).
0256  * @param pErrorCode ICU error code in/out parameter.
0257  *                   Must fulfill U_SUCCESS before the function call.
0258  * @return UNORM_YES, UNORM_NO or UNORM_MAYBE
0259  *
0260  * @see unorm_quickCheck
0261  * @see unorm_isNormalized
0262  * @deprecated ICU 56 Use unorm2.h instead.
0263  */
0264 U_DEPRECATED UNormalizationCheckResult U_EXPORT2
0265 unorm_quickCheckWithOptions(const UChar *src, int32_t srcLength,
0266                             UNormalizationMode mode, int32_t options,
0267                             UErrorCode *pErrorCode);
0268 
0269 /**
0270  * Test if a string is in a given normalization form.
0271  * This is semantically equivalent to source.equals(normalize(source, mode)) .
0272  *
0273  * Unlike unorm_quickCheck(), this function returns a definitive result,
0274  * never a "maybe".
0275  * For NFD, NFKD, and FCD, both functions work exactly the same.
0276  * For NFC and NFKC where quickCheck may return "maybe", this function will
0277  * perform further tests to arrive at a true/false result.
0278  *
0279  * @param src        String that is to be tested if it is in a normalization format.
0280  * @param srcLength  Length of source to test, or -1 if NUL-terminated.
0281  * @param mode       Which normalization form to test for.
0282  * @param pErrorCode ICU error code in/out parameter.
0283  *                   Must fulfill U_SUCCESS before the function call.
0284  * @return Boolean value indicating whether the source string is in the
0285  *         "mode" normalization form.
0286  *
0287  * @see unorm_quickCheck
0288  * @deprecated ICU 56 Use unorm2.h instead.
0289  */
0290 U_DEPRECATED UBool U_EXPORT2
0291 unorm_isNormalized(const UChar *src, int32_t srcLength,
0292                    UNormalizationMode mode,
0293                    UErrorCode *pErrorCode);
0294 
0295 /**
0296  * Test if a string is in a given normalization form; same as unorm_isNormalized but
0297  * takes an extra options parameter like most normalization functions.
0298  *
0299  * @param src        String that is to be tested if it is in a normalization format.
0300  * @param srcLength  Length of source to test, or -1 if NUL-terminated.
0301  * @param mode       Which normalization form to test for.
0302  * @param options    The normalization options, ORed together (0 for no options).
0303  * @param pErrorCode ICU error code in/out parameter.
0304  *                   Must fulfill U_SUCCESS before the function call.
0305  * @return Boolean value indicating whether the source string is in the
0306  *         "mode/options" normalization form.
0307  *
0308  * @see unorm_quickCheck
0309  * @see unorm_isNormalized
0310  * @deprecated ICU 56 Use unorm2.h instead.
0311  */
0312 U_DEPRECATED UBool U_EXPORT2
0313 unorm_isNormalizedWithOptions(const UChar *src, int32_t srcLength,
0314                               UNormalizationMode mode, int32_t options,
0315                               UErrorCode *pErrorCode);
0316 
0317 /**
0318  * Iterative normalization forward.
0319  * This function (together with unorm_previous) is somewhat
0320  * similar to the C++ Normalizer class (see its non-static functions).
0321  *
0322  * Iterative normalization is useful when only a small portion of a longer
0323  * string/text needs to be processed.
0324  *
0325  * For example, the likelihood may be high that processing the first 10% of some
0326  * text will be sufficient to find certain data.
0327  * Another example: When one wants to concatenate two normalized strings and get a
0328  * normalized result, it is much more efficient to normalize just a small part of
0329  * the result around the concatenation place instead of re-normalizing everything.
0330  *
0331  * The input text is an instance of the C character iteration API UCharIterator.
0332  * It may wrap around a simple string, a CharacterIterator, a Replaceable, or any
0333  * other kind of text object.
0334  *
0335  * If a buffer overflow occurs, then the caller needs to reset the iterator to the
0336  * old index and call the function again with a larger buffer - if the caller cares
0337  * for the actual output.
0338  * Regardless of the output buffer, the iterator will always be moved to the next
0339  * normalization boundary.
0340  *
0341  * This function (like unorm_previous) serves two purposes:
0342  *
0343  * 1) To find the next boundary so that the normalization of the part of the text
0344  * from the current position to that boundary does not affect and is not affected
0345  * by the part of the text beyond that boundary.
0346  *
0347  * 2) To normalize the text up to the boundary.
0348  *
0349  * The second step is optional, per the doNormalize parameter.
0350  * It is omitted for operations like string concatenation, where the two adjacent
0351  * string ends need to be normalized together.
0352  * In such a case, the output buffer will just contain a copy of the text up to the
0353  * boundary.
0354  *
0355  * pNeededToNormalize is an output-only parameter. Its output value is only defined
0356  * if normalization was requested (doNormalize) and successful (especially, no
0357  * buffer overflow).
0358  * It is useful for operations like a normalizing transliterator, where one would
0359  * not want to replace a piece of text if it is not modified.
0360  *
0361  * If doNormalize==true and pNeededToNormalize!=NULL then *pNeeded... is set true
0362  * if the normalization was necessary.
0363  *
0364  * If doNormalize==false then *pNeededToNormalize will be set to false.
0365  *
0366  * If the buffer overflows, then *pNeededToNormalize will be undefined;
0367  * essentially, whenever U_FAILURE is true (like in buffer overflows), this result
0368  * will be undefined.
0369  *
0370  * @param src The input text in the form of a C character iterator.
0371  * @param dest The output buffer; can be NULL if destCapacity==0 for pure preflighting.
0372  * @param destCapacity The number of UChars that fit into dest.
0373  * @param mode The normalization mode.
0374  * @param options The normalization options, ORed together (0 for no options).
0375  * @param doNormalize Indicates if the source text up to the next boundary
0376  *                    is to be normalized (true) or just copied (false).
0377  * @param pNeededToNormalize Output flag indicating if the normalization resulted in
0378  *                           different text from the input.
0379  *                           Not defined if an error occurs including buffer overflow.
0380  *                           Always false if !doNormalize.
0381  * @param pErrorCode ICU error code in/out parameter.
0382  *                   Must fulfill U_SUCCESS before the function call.
0383  * @return Length of output (number of UChars) when successful or buffer overflow.
0384  *
0385  * @see unorm_previous
0386  * @see unorm_normalize
0387  *
0388  * @deprecated ICU 56 Use unorm2.h instead.
0389  */
0390 U_DEPRECATED int32_t U_EXPORT2
0391 unorm_next(UCharIterator *src,
0392            UChar *dest, int32_t destCapacity,
0393            UNormalizationMode mode, int32_t options,
0394            UBool doNormalize, UBool *pNeededToNormalize,
0395            UErrorCode *pErrorCode);
0396 
0397 /**
0398  * Iterative normalization backward.
0399  * This function (together with unorm_next) is somewhat
0400  * similar to the C++ Normalizer class (see its non-static functions).
0401  * For all details see unorm_next.
0402  *
0403  * @param src The input text in the form of a C character iterator.
0404  * @param dest The output buffer; can be NULL if destCapacity==0 for pure preflighting.
0405  * @param destCapacity The number of UChars that fit into dest.
0406  * @param mode The normalization mode.
0407  * @param options The normalization options, ORed together (0 for no options).
0408  * @param doNormalize Indicates if the source text up to the next boundary
0409  *                    is to be normalized (true) or just copied (false).
0410  * @param pNeededToNormalize Output flag indicating if the normalization resulted in
0411  *                           different text from the input.
0412  *                           Not defined if an error occurs including buffer overflow.
0413  *                           Always false if !doNormalize.
0414  * @param pErrorCode ICU error code in/out parameter.
0415  *                   Must fulfill U_SUCCESS before the function call.
0416  * @return Length of output (number of UChars) when successful or buffer overflow.
0417  *
0418  * @see unorm_next
0419  * @see unorm_normalize
0420  *
0421  * @deprecated ICU 56 Use unorm2.h instead.
0422  */
0423 U_DEPRECATED int32_t U_EXPORT2
0424 unorm_previous(UCharIterator *src,
0425                UChar *dest, int32_t destCapacity,
0426                UNormalizationMode mode, int32_t options,
0427                UBool doNormalize, UBool *pNeededToNormalize,
0428                UErrorCode *pErrorCode);
0429 
0430 /**
0431  * Concatenate normalized strings, making sure that the result is normalized as well.
0432  *
0433  * If both the left and the right strings are in
0434  * the normalization form according to "mode/options",
0435  * then the result will be
0436  *
0437  * \code
0438  *     dest=normalize(left+right, mode, options)
0439  * \endcode
0440  *
0441  * With the input strings already being normalized,
0442  * this function will use unorm_next() and unorm_previous()
0443  * to find the adjacent end pieces of the input strings.
0444  * Only the concatenation of these end pieces will be normalized and
0445  * then concatenated with the remaining parts of the input strings.
0446  *
0447  * It is allowed to have dest==left to avoid copying the entire left string.
0448  *
0449  * @param left Left source string, may be same as dest.
0450  * @param leftLength Length of left source string, or -1 if NUL-terminated.
0451  * @param right Right source string. Must not be the same as dest, nor overlap.
0452  * @param rightLength Length of right source string, or -1 if NUL-terminated.
0453  * @param dest The output buffer; can be NULL if destCapacity==0 for pure preflighting.
0454  * @param destCapacity The number of UChars that fit into dest.
0455  * @param mode The normalization mode.
0456  * @param options The normalization options, ORed together (0 for no options).
0457  * @param pErrorCode ICU error code in/out parameter.
0458  *                   Must fulfill U_SUCCESS before the function call.
0459  * @return Length of output (number of UChars) when successful or buffer overflow.
0460  *
0461  * @see unorm_normalize
0462  * @see unorm_next
0463  * @see unorm_previous
0464  *
0465  * @deprecated ICU 56 Use unorm2.h instead.
0466  */
0467 U_DEPRECATED int32_t U_EXPORT2
0468 unorm_concatenate(const UChar *left, int32_t leftLength,
0469                   const UChar *right, int32_t rightLength,
0470                   UChar *dest, int32_t destCapacity,
0471                   UNormalizationMode mode, int32_t options,
0472                   UErrorCode *pErrorCode);
0473 
0474 #endif  /* U_HIDE_DEPRECATED_API */
0475 #endif /* #if !UCONFIG_NO_NORMALIZATION */
0476 #endif