unorm.h 20 KB

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