unorm.h 20 KB

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