unorm.h 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476
  1. // © 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. * https://htmlpreview.github.io/?https://github.com/unicode-org/icu-docs/blob/main/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. #ifndef U_FORCE_HIDE_DEPRECATED_API
  130. /**
  131. * Constants for normalization modes.
  132. * @deprecated ICU 56 Use unorm2.h instead.
  133. */
  134. typedef enum {
  135. /** No decomposition/composition. @deprecated ICU 56 Use unorm2.h instead. */
  136. UNORM_NONE = 1,
  137. /** Canonical decomposition. @deprecated ICU 56 Use unorm2.h instead. */
  138. UNORM_NFD = 2,
  139. /** Compatibility decomposition. @deprecated ICU 56 Use unorm2.h instead. */
  140. UNORM_NFKD = 3,
  141. /** Canonical decomposition followed by canonical composition. @deprecated ICU 56 Use unorm2.h instead. */
  142. UNORM_NFC = 4,
  143. /** Default normalization. @deprecated ICU 56 Use unorm2.h instead. */
  144. UNORM_DEFAULT = UNORM_NFC,
  145. /** Compatibility decomposition followed by canonical composition. @deprecated ICU 56 Use unorm2.h instead. */
  146. UNORM_NFKC =5,
  147. /** "Fast C or D" form. @deprecated ICU 56 Use unorm2.h instead. */
  148. UNORM_FCD = 6,
  149. /** One more than the highest normalization mode constant. @deprecated ICU 56 Use unorm2.h instead. */
  150. UNORM_MODE_COUNT
  151. } UNormalizationMode;
  152. #endif // U_FORCE_HIDE_DEPRECATED_API
  153. #ifndef U_HIDE_DEPRECATED_API
  154. /**
  155. * Constants for options flags for normalization.
  156. * Use 0 for default options,
  157. * including normalization according to the Unicode version
  158. * that is currently supported by ICU (see u_getUnicodeVersion).
  159. * @deprecated ICU 56 Use unorm2.h instead.
  160. */
  161. enum {
  162. /**
  163. * Options bit set value to select Unicode 3.2 normalization
  164. * (except NormalizationCorrections).
  165. * At most one Unicode version can be selected at a time.
  166. * @deprecated ICU 56 Use unorm2.h instead.
  167. */
  168. UNORM_UNICODE_3_2=0x20
  169. };
  170. /**
  171. * Lowest-order bit number of unorm_compare() options bits corresponding to
  172. * normalization options bits.
  173. *
  174. * The options parameter for unorm_compare() uses most bits for
  175. * itself and for various comparison and folding flags.
  176. * The most significant bits, however, are shifted down and passed on
  177. * to the normalization implementation.
  178. * (That is, from unorm_compare(..., options, ...),
  179. * options>>UNORM_COMPARE_NORM_OPTIONS_SHIFT will be passed on to the
  180. * internal normalization functions.)
  181. *
  182. * @see unorm_compare
  183. * @deprecated ICU 56 Use unorm2.h instead.
  184. */
  185. #define UNORM_COMPARE_NORM_OPTIONS_SHIFT 20
  186. /**
  187. * Normalize a string.
  188. * The string will be normalized according the specified normalization mode
  189. * and options.
  190. * The source and result buffers must not be the same, nor overlap.
  191. *
  192. * @param source The string to normalize.
  193. * @param sourceLength The length of source, or -1 if NUL-terminated.
  194. * @param mode The normalization mode; one of UNORM_NONE,
  195. * UNORM_NFD, UNORM_NFC, UNORM_NFKC, UNORM_NFKD, UNORM_DEFAULT.
  196. * @param options The normalization options, ORed together (0 for no options).
  197. * @param result A pointer to a buffer to receive the result string.
  198. * The result string is NUL-terminated if possible.
  199. * @param resultLength The maximum size of result.
  200. * @param status A pointer to a UErrorCode to receive any errors.
  201. * @return The total buffer size needed; if greater than resultLength,
  202. * the output was truncated, and the error code is set to U_BUFFER_OVERFLOW_ERROR.
  203. * @deprecated ICU 56 Use unorm2.h instead.
  204. */
  205. U_DEPRECATED int32_t U_EXPORT2
  206. unorm_normalize(const UChar *source, int32_t sourceLength,
  207. UNormalizationMode mode, int32_t options,
  208. UChar *result, int32_t resultLength,
  209. UErrorCode *status);
  210. /**
  211. * Performing quick check on a string, to quickly determine if the string is
  212. * in a particular normalization format.
  213. * Three types of result can be returned UNORM_YES, UNORM_NO or
  214. * UNORM_MAYBE. Result UNORM_YES indicates that the argument
  215. * string is in the desired normalized format, UNORM_NO determines that
  216. * argument string is not in the desired normalized format. A
  217. * UNORM_MAYBE result indicates that a more thorough check is required,
  218. * the user may have to put the string in its normalized form and compare the
  219. * results.
  220. *
  221. * @param source string for determining if it is in a normalized format
  222. * @param sourcelength length of source to test, or -1 if NUL-terminated
  223. * @param mode which normalization form to test for
  224. * @param status a pointer to a UErrorCode to receive any errors
  225. * @return UNORM_YES, UNORM_NO or UNORM_MAYBE
  226. *
  227. * @see unorm_isNormalized
  228. * @deprecated ICU 56 Use unorm2.h instead.
  229. */
  230. U_DEPRECATED UNormalizationCheckResult U_EXPORT2
  231. unorm_quickCheck(const UChar *source, int32_t sourcelength,
  232. UNormalizationMode mode,
  233. UErrorCode *status);
  234. /**
  235. * Performing quick check on a string; same as unorm_quickCheck but
  236. * takes an extra options parameter like most normalization functions.
  237. *
  238. * @param src String that is to be tested if it is in a normalization format.
  239. * @param srcLength Length of source to test, or -1 if NUL-terminated.
  240. * @param mode Which normalization form to test for.
  241. * @param options The normalization options, ORed together (0 for no options).
  242. * @param pErrorCode ICU error code in/out parameter.
  243. * Must fulfill U_SUCCESS before the function call.
  244. * @return UNORM_YES, UNORM_NO or UNORM_MAYBE
  245. *
  246. * @see unorm_quickCheck
  247. * @see unorm_isNormalized
  248. * @deprecated ICU 56 Use unorm2.h instead.
  249. */
  250. U_DEPRECATED UNormalizationCheckResult U_EXPORT2
  251. unorm_quickCheckWithOptions(const UChar *src, int32_t srcLength,
  252. UNormalizationMode mode, int32_t options,
  253. UErrorCode *pErrorCode);
  254. /**
  255. * Test if a string is in a given normalization form.
  256. * This is semantically equivalent to source.equals(normalize(source, mode)) .
  257. *
  258. * Unlike unorm_quickCheck(), this function returns a definitive result,
  259. * never a "maybe".
  260. * For NFD, NFKD, and FCD, both functions work exactly the same.
  261. * For NFC and NFKC where quickCheck may return "maybe", this function will
  262. * perform further tests to arrive at a true/false result.
  263. *
  264. * @param src String that is to be tested if it is in a normalization format.
  265. * @param srcLength Length of source to test, or -1 if NUL-terminated.
  266. * @param mode Which normalization form to test for.
  267. * @param pErrorCode ICU error code in/out parameter.
  268. * Must fulfill U_SUCCESS before the function call.
  269. * @return Boolean value indicating whether the source string is in the
  270. * "mode" normalization form.
  271. *
  272. * @see unorm_quickCheck
  273. * @deprecated ICU 56 Use unorm2.h instead.
  274. */
  275. U_DEPRECATED UBool U_EXPORT2
  276. unorm_isNormalized(const UChar *src, int32_t srcLength,
  277. UNormalizationMode mode,
  278. UErrorCode *pErrorCode);
  279. /**
  280. * Test if a string is in a given normalization form; same as unorm_isNormalized but
  281. * takes an extra options parameter like most normalization functions.
  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 options The normalization options, ORed together (0 for no options).
  287. * @param pErrorCode ICU error code in/out parameter.
  288. * Must fulfill U_SUCCESS before the function call.
  289. * @return Boolean value indicating whether the source string is in the
  290. * "mode/options" normalization form.
  291. *
  292. * @see unorm_quickCheck
  293. * @see unorm_isNormalized
  294. * @deprecated ICU 56 Use unorm2.h instead.
  295. */
  296. U_DEPRECATED UBool U_EXPORT2
  297. unorm_isNormalizedWithOptions(const UChar *src, int32_t srcLength,
  298. UNormalizationMode mode, int32_t options,
  299. UErrorCode *pErrorCode);
  300. /**
  301. * Iterative normalization forward.
  302. * This function (together with unorm_previous) is somewhat
  303. * similar to the C++ Normalizer class (see its non-static functions).
  304. *
  305. * Iterative normalization is useful when only a small portion of a longer
  306. * string/text needs to be processed.
  307. *
  308. * For example, the likelihood may be high that processing the first 10% of some
  309. * text will be sufficient to find certain data.
  310. * Another example: When one wants to concatenate two normalized strings and get a
  311. * normalized result, it is much more efficient to normalize just a small part of
  312. * the result around the concatenation place instead of re-normalizing everything.
  313. *
  314. * The input text is an instance of the C character iteration API UCharIterator.
  315. * It may wrap around a simple string, a CharacterIterator, a Replaceable, or any
  316. * other kind of text object.
  317. *
  318. * If a buffer overflow occurs, then the caller needs to reset the iterator to the
  319. * old index and call the function again with a larger buffer - if the caller cares
  320. * for the actual output.
  321. * Regardless of the output buffer, the iterator will always be moved to the next
  322. * normalization boundary.
  323. *
  324. * This function (like unorm_previous) serves two purposes:
  325. *
  326. * 1) To find the next boundary so that the normalization of the part of the text
  327. * from the current position to that boundary does not affect and is not affected
  328. * by the part of the text beyond that boundary.
  329. *
  330. * 2) To normalize the text up to the boundary.
  331. *
  332. * The second step is optional, per the doNormalize parameter.
  333. * It is omitted for operations like string concatenation, where the two adjacent
  334. * string ends need to be normalized together.
  335. * In such a case, the output buffer will just contain a copy of the text up to the
  336. * boundary.
  337. *
  338. * pNeededToNormalize is an output-only parameter. Its output value is only defined
  339. * if normalization was requested (doNormalize) and successful (especially, no
  340. * buffer overflow).
  341. * It is useful for operations like a normalizing transliterator, where one would
  342. * not want to replace a piece of text if it is not modified.
  343. *
  344. * If doNormalize==true and pNeededToNormalize!=NULL then *pNeeded... is set true
  345. * if the normalization was necessary.
  346. *
  347. * If doNormalize==false then *pNeededToNormalize will be set to false.
  348. *
  349. * If the buffer overflows, then *pNeededToNormalize will be undefined;
  350. * essentially, whenever U_FAILURE is true (like in buffer overflows), this result
  351. * will be undefined.
  352. *
  353. * @param src The input text in the form of a C character iterator.
  354. * @param dest The output buffer; can be NULL if destCapacity==0 for pure preflighting.
  355. * @param destCapacity The number of UChars that fit into dest.
  356. * @param mode The normalization mode.
  357. * @param options The normalization options, ORed together (0 for no options).
  358. * @param doNormalize Indicates if the source text up to the next boundary
  359. * is to be normalized (true) or just copied (false).
  360. * @param pNeededToNormalize Output flag indicating if the normalization resulted in
  361. * different text from the input.
  362. * Not defined if an error occurs including buffer overflow.
  363. * Always false if !doNormalize.
  364. * @param pErrorCode ICU error code in/out parameter.
  365. * Must fulfill U_SUCCESS before the function call.
  366. * @return Length of output (number of UChars) when successful or buffer overflow.
  367. *
  368. * @see unorm_previous
  369. * @see unorm_normalize
  370. *
  371. * @deprecated ICU 56 Use unorm2.h instead.
  372. */
  373. U_DEPRECATED int32_t U_EXPORT2
  374. unorm_next(UCharIterator *src,
  375. UChar *dest, int32_t destCapacity,
  376. UNormalizationMode mode, int32_t options,
  377. UBool doNormalize, UBool *pNeededToNormalize,
  378. UErrorCode *pErrorCode);
  379. /**
  380. * Iterative normalization backward.
  381. * This function (together with unorm_next) is somewhat
  382. * similar to the C++ Normalizer class (see its non-static functions).
  383. * For all details see unorm_next.
  384. *
  385. * @param src The input text in the form of a C character iterator.
  386. * @param dest The output buffer; can be NULL if destCapacity==0 for pure preflighting.
  387. * @param destCapacity The number of UChars that fit into dest.
  388. * @param mode The normalization mode.
  389. * @param options The normalization options, ORed together (0 for no options).
  390. * @param doNormalize Indicates if the source text up to the next boundary
  391. * is to be normalized (true) or just copied (false).
  392. * @param pNeededToNormalize Output flag indicating if the normalization resulted in
  393. * different text from the input.
  394. * Not defined if an error occurs including buffer overflow.
  395. * Always false if !doNormalize.
  396. * @param pErrorCode ICU error code in/out parameter.
  397. * Must fulfill U_SUCCESS before the function call.
  398. * @return Length of output (number of UChars) when successful or buffer overflow.
  399. *
  400. * @see unorm_next
  401. * @see unorm_normalize
  402. *
  403. * @deprecated ICU 56 Use unorm2.h instead.
  404. */
  405. U_DEPRECATED int32_t U_EXPORT2
  406. unorm_previous(UCharIterator *src,
  407. UChar *dest, int32_t destCapacity,
  408. UNormalizationMode mode, int32_t options,
  409. UBool doNormalize, UBool *pNeededToNormalize,
  410. UErrorCode *pErrorCode);
  411. /**
  412. * Concatenate normalized strings, making sure that the result is normalized as well.
  413. *
  414. * If both the left and the right strings are in
  415. * the normalization form according to "mode/options",
  416. * then the result will be
  417. *
  418. * \code
  419. * dest=normalize(left+right, mode, options)
  420. * \endcode
  421. *
  422. * With the input strings already being normalized,
  423. * this function will use unorm_next() and unorm_previous()
  424. * to find the adjacent end pieces of the input strings.
  425. * Only the concatenation of these end pieces will be normalized and
  426. * then concatenated with the remaining parts of the input strings.
  427. *
  428. * It is allowed to have dest==left to avoid copying the entire left string.
  429. *
  430. * @param left Left source string, may be same as dest.
  431. * @param leftLength Length of left source string, or -1 if NUL-terminated.
  432. * @param right Right source string. Must not be the same as dest, nor overlap.
  433. * @param rightLength Length of right source string, or -1 if NUL-terminated.
  434. * @param dest The output buffer; can be NULL if destCapacity==0 for pure preflighting.
  435. * @param destCapacity The number of UChars that fit into dest.
  436. * @param mode The normalization mode.
  437. * @param options The normalization options, ORed together (0 for no options).
  438. * @param pErrorCode ICU error code in/out parameter.
  439. * Must fulfill U_SUCCESS before the function call.
  440. * @return Length of output (number of UChars) when successful or buffer overflow.
  441. *
  442. * @see unorm_normalize
  443. * @see unorm_next
  444. * @see unorm_previous
  445. *
  446. * @deprecated ICU 56 Use unorm2.h instead.
  447. */
  448. U_DEPRECATED int32_t U_EXPORT2
  449. unorm_concatenate(const UChar *left, int32_t leftLength,
  450. const UChar *right, int32_t rightLength,
  451. UChar *dest, int32_t destCapacity,
  452. UNormalizationMode mode, int32_t options,
  453. UErrorCode *pErrorCode);
  454. #endif /* U_HIDE_DEPRECATED_API */
  455. #endif /* #if !UCONFIG_NO_NORMALIZATION */
  456. #endif