utf8proc.h 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729
  1. /*
  2. * Copyright (c) 2014-2021 Steven G. Johnson, Jiahao Chen, Peter Colberg, Tony Kelman, Scott P. Jones, and other contributors.
  3. * Copyright (c) 2009 Public Software Group e. V., Berlin, Germany
  4. *
  5. * Permission is hereby granted, free of charge, to any person obtaining a
  6. * copy of this software and associated documentation files (the "Software"),
  7. * to deal in the Software without restriction, including without limitation
  8. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  9. * and/or sell copies of the Software, and to permit persons to whom the
  10. * Software is furnished to do so, subject to the following conditions:
  11. *
  12. * The above copyright notice and this permission notice shall be included in
  13. * all copies or substantial portions of the Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  21. * DEALINGS IN THE SOFTWARE.
  22. */
  23. /**
  24. * @mainpage
  25. *
  26. * utf8proc is a free/open-source (MIT/expat licensed) C library
  27. * providing Unicode normalization, case-folding, and other operations
  28. * for strings in the UTF-8 encoding, supporting up-to-date Unicode versions.
  29. * See the utf8proc home page (http://julialang.org/utf8proc/)
  30. * for downloads and other information, or the source code on github
  31. * (https://github.com/JuliaLang/utf8proc).
  32. *
  33. * For the utf8proc API documentation, see: @ref utf8proc.h
  34. *
  35. * The features of utf8proc include:
  36. *
  37. * - Transformation of strings (@ref utf8proc_map) to:
  38. * - decompose (@ref UTF8PROC_DECOMPOSE) or compose (@ref UTF8PROC_COMPOSE) Unicode combining characters (http://en.wikipedia.org/wiki/Combining_character)
  39. * - canonicalize Unicode compatibility characters (@ref UTF8PROC_COMPAT)
  40. * - strip "ignorable" (@ref UTF8PROC_IGNORE) characters, control characters (@ref UTF8PROC_STRIPCC), or combining characters such as accents (@ref UTF8PROC_STRIPMARK)
  41. * - case-folding (@ref UTF8PROC_CASEFOLD)
  42. * - Unicode normalization: @ref utf8proc_NFD, @ref utf8proc_NFC, @ref utf8proc_NFKD, @ref utf8proc_NFKC
  43. * - Detecting grapheme boundaries (@ref utf8proc_grapheme_break and @ref UTF8PROC_CHARBOUND)
  44. * - Character-width computation: @ref utf8proc_charwidth
  45. * - Classification of characters by Unicode category: @ref utf8proc_category and @ref utf8proc_category_string
  46. * - Encode (@ref utf8proc_encode_char) and decode (@ref utf8proc_iterate) Unicode codepoints to/from UTF-8.
  47. */
  48. /** @file */
  49. #ifndef UTF8PROC_H
  50. #define UTF8PROC_H
  51. /** @name API version
  52. *
  53. * The utf8proc API version MAJOR.MINOR.PATCH, following
  54. * semantic-versioning rules (http://semver.org) based on API
  55. * compatibility.
  56. *
  57. * This is also returned at runtime by @ref utf8proc_version; however, the
  58. * runtime version may append a string like "-dev" to the version number
  59. * for prerelease versions.
  60. *
  61. * @note The shared-library version number in the Makefile
  62. * (and CMakeLists.txt, and MANIFEST) may be different,
  63. * being based on ABI compatibility rather than API compatibility.
  64. */
  65. /** @{ */
  66. /** The MAJOR version number (increased when backwards API compatibility is broken). */
  67. #define UTF8PROC_VERSION_MAJOR 2
  68. /** The MINOR version number (increased when new functionality is added in a backwards-compatible manner). */
  69. #define UTF8PROC_VERSION_MINOR 9
  70. /** The PATCH version (increased for fixes that do not change the API). */
  71. #define UTF8PROC_VERSION_PATCH 0
  72. /** @} */
  73. #include <stdlib.h>
  74. #if defined(_MSC_VER) && _MSC_VER < 1800
  75. // MSVC prior to 2013 lacked stdbool.h and inttypes.h
  76. typedef signed char utf8proc_int8_t;
  77. typedef unsigned char utf8proc_uint8_t;
  78. typedef short utf8proc_int16_t;
  79. typedef unsigned short utf8proc_uint16_t;
  80. typedef int utf8proc_int32_t;
  81. typedef unsigned int utf8proc_uint32_t;
  82. # ifdef _WIN64
  83. typedef __int64 utf8proc_ssize_t;
  84. typedef unsigned __int64 utf8proc_size_t;
  85. # else
  86. typedef int utf8proc_ssize_t;
  87. typedef unsigned int utf8proc_size_t;
  88. # endif
  89. # ifndef __cplusplus
  90. // emulate C99 bool
  91. typedef unsigned char utf8proc_bool;
  92. # ifndef __bool_true_false_are_defined
  93. # define false 0
  94. # define true 1
  95. # define __bool_true_false_are_defined 1
  96. # endif
  97. # else
  98. typedef bool utf8proc_bool;
  99. # endif
  100. #else
  101. # include <stddef.h>
  102. # include <stdbool.h>
  103. # include <inttypes.h>
  104. typedef int8_t utf8proc_int8_t;
  105. typedef uint8_t utf8proc_uint8_t;
  106. typedef int16_t utf8proc_int16_t;
  107. typedef uint16_t utf8proc_uint16_t;
  108. typedef int32_t utf8proc_int32_t;
  109. typedef uint32_t utf8proc_uint32_t;
  110. typedef size_t utf8proc_size_t;
  111. typedef ptrdiff_t utf8proc_ssize_t;
  112. typedef bool utf8proc_bool;
  113. #endif
  114. #include <limits.h>
  115. #define UTF8PROC_DLLEXPORT
  116. #ifdef __cplusplus
  117. extern "C" {
  118. #endif
  119. /**
  120. * Option flags used by several functions in the library.
  121. */
  122. typedef enum {
  123. /** The given UTF-8 input is NULL terminated. */
  124. UTF8PROC_NULLTERM = (1<<0),
  125. /** Unicode Versioning Stability has to be respected. */
  126. UTF8PROC_STABLE = (1<<1),
  127. /** Compatibility decomposition (i.e. formatting information is lost). */
  128. UTF8PROC_COMPAT = (1<<2),
  129. /** Return a result with decomposed characters. */
  130. UTF8PROC_COMPOSE = (1<<3),
  131. /** Return a result with decomposed characters. */
  132. UTF8PROC_DECOMPOSE = (1<<4),
  133. /** Strip "default ignorable characters" such as SOFT-HYPHEN or ZERO-WIDTH-SPACE. */
  134. UTF8PROC_IGNORE = (1<<5),
  135. /** Return an error, if the input contains unassigned codepoints. */
  136. UTF8PROC_REJECTNA = (1<<6),
  137. /**
  138. * Indicating that NLF-sequences (LF, CRLF, CR, NEL) are representing a
  139. * line break, and should be converted to the codepoint for line
  140. * separation (LS).
  141. */
  142. UTF8PROC_NLF2LS = (1<<7),
  143. /**
  144. * Indicating that NLF-sequences are representing a paragraph break, and
  145. * should be converted to the codepoint for paragraph separation
  146. * (PS).
  147. */
  148. UTF8PROC_NLF2PS = (1<<8),
  149. /** Indicating that the meaning of NLF-sequences is unknown. */
  150. UTF8PROC_NLF2LF = (UTF8PROC_NLF2LS | UTF8PROC_NLF2PS),
  151. /** Strips and/or convers control characters.
  152. *
  153. * NLF-sequences are transformed into space, except if one of the
  154. * NLF2LS/PS/LF options is given. HorizontalTab (HT) and FormFeed (FF)
  155. * are treated as a NLF-sequence in this case. All other control
  156. * characters are simply removed.
  157. */
  158. UTF8PROC_STRIPCC = (1<<9),
  159. /**
  160. * Performs unicode case folding, to be able to do a case-insensitive
  161. * string comparison.
  162. */
  163. UTF8PROC_CASEFOLD = (1<<10),
  164. /**
  165. * Inserts 0xFF bytes at the beginning of each sequence which is
  166. * representing a single grapheme cluster (see UAX#29).
  167. */
  168. UTF8PROC_CHARBOUND = (1<<11),
  169. /** Lumps certain characters together.
  170. *
  171. * E.g. HYPHEN U+2010 and MINUS U+2212 to ASCII "-". See lump.md for details.
  172. *
  173. * If NLF2LF is set, this includes a transformation of paragraph and
  174. * line separators to ASCII line-feed (LF).
  175. */
  176. UTF8PROC_LUMP = (1<<12),
  177. /** Strips all character markings.
  178. *
  179. * This includes non-spacing, spacing and enclosing (i.e. accents).
  180. * @note This option works only with @ref UTF8PROC_COMPOSE or
  181. * @ref UTF8PROC_DECOMPOSE
  182. */
  183. UTF8PROC_STRIPMARK = (1<<13),
  184. /**
  185. * Strip unassigned codepoints.
  186. */
  187. UTF8PROC_STRIPNA = (1<<14),
  188. } utf8proc_option_t;
  189. /** @name Error codes
  190. * Error codes being returned by almost all functions.
  191. */
  192. /** @{ */
  193. /** Memory could not be allocated. */
  194. #define UTF8PROC_ERROR_NOMEM -1
  195. /** The given string is too long to be processed. */
  196. #define UTF8PROC_ERROR_OVERFLOW -2
  197. /** The given string is not a legal UTF-8 string. */
  198. #define UTF8PROC_ERROR_INVALIDUTF8 -3
  199. /** The @ref UTF8PROC_REJECTNA flag was set and an unassigned codepoint was found. */
  200. #define UTF8PROC_ERROR_NOTASSIGNED -4
  201. /** Invalid options have been used. */
  202. #define UTF8PROC_ERROR_INVALIDOPTS -5
  203. /** @} */
  204. /* @name Types */
  205. /** Holds the value of a property. */
  206. typedef utf8proc_int16_t utf8proc_propval_t;
  207. /** Struct containing information about a codepoint. */
  208. typedef struct utf8proc_property_struct {
  209. /**
  210. * Unicode category.
  211. * @see utf8proc_category_t.
  212. */
  213. utf8proc_propval_t category;
  214. utf8proc_propval_t combining_class;
  215. /**
  216. * Bidirectional class.
  217. * @see utf8proc_bidi_class_t.
  218. */
  219. utf8proc_propval_t bidi_class;
  220. /**
  221. * @anchor Decomposition type.
  222. * @see utf8proc_decomp_type_t.
  223. */
  224. utf8proc_propval_t decomp_type;
  225. utf8proc_uint16_t decomp_seqindex;
  226. utf8proc_uint16_t casefold_seqindex;
  227. utf8proc_uint16_t uppercase_seqindex;
  228. utf8proc_uint16_t lowercase_seqindex;
  229. utf8proc_uint16_t titlecase_seqindex;
  230. utf8proc_uint16_t comb_index;
  231. unsigned bidi_mirrored:1;
  232. unsigned comp_exclusion:1;
  233. /**
  234. * Can this codepoint be ignored?
  235. *
  236. * Used by @ref utf8proc_decompose_char when @ref UTF8PROC_IGNORE is
  237. * passed as an option.
  238. */
  239. unsigned ignorable:1;
  240. unsigned control_boundary:1;
  241. /** The width of the codepoint. */
  242. unsigned charwidth:2;
  243. unsigned pad:2;
  244. /**
  245. * Boundclass.
  246. * @see utf8proc_boundclass_t.
  247. */
  248. unsigned boundclass:6;
  249. unsigned indic_conjunct_break:2;
  250. } utf8proc_property_t;
  251. /** Unicode categories. */
  252. typedef enum {
  253. UTF8PROC_CATEGORY_CN = 0, /**< Other, not assigned */
  254. UTF8PROC_CATEGORY_LU = 1, /**< Letter, uppercase */
  255. UTF8PROC_CATEGORY_LL = 2, /**< Letter, lowercase */
  256. UTF8PROC_CATEGORY_LT = 3, /**< Letter, titlecase */
  257. UTF8PROC_CATEGORY_LM = 4, /**< Letter, modifier */
  258. UTF8PROC_CATEGORY_LO = 5, /**< Letter, other */
  259. UTF8PROC_CATEGORY_MN = 6, /**< Mark, nonspacing */
  260. UTF8PROC_CATEGORY_MC = 7, /**< Mark, spacing combining */
  261. UTF8PROC_CATEGORY_ME = 8, /**< Mark, enclosing */
  262. UTF8PROC_CATEGORY_ND = 9, /**< Number, decimal digit */
  263. UTF8PROC_CATEGORY_NL = 10, /**< Number, letter */
  264. UTF8PROC_CATEGORY_NO = 11, /**< Number, other */
  265. UTF8PROC_CATEGORY_PC = 12, /**< Punctuation, connector */
  266. UTF8PROC_CATEGORY_PD = 13, /**< Punctuation, dash */
  267. UTF8PROC_CATEGORY_PS = 14, /**< Punctuation, open */
  268. UTF8PROC_CATEGORY_PE = 15, /**< Punctuation, close */
  269. UTF8PROC_CATEGORY_PI = 16, /**< Punctuation, initial quote */
  270. UTF8PROC_CATEGORY_PF = 17, /**< Punctuation, final quote */
  271. UTF8PROC_CATEGORY_PO = 18, /**< Punctuation, other */
  272. UTF8PROC_CATEGORY_SM = 19, /**< Symbol, math */
  273. UTF8PROC_CATEGORY_SC = 20, /**< Symbol, currency */
  274. UTF8PROC_CATEGORY_SK = 21, /**< Symbol, modifier */
  275. UTF8PROC_CATEGORY_SO = 22, /**< Symbol, other */
  276. UTF8PROC_CATEGORY_ZS = 23, /**< Separator, space */
  277. UTF8PROC_CATEGORY_ZL = 24, /**< Separator, line */
  278. UTF8PROC_CATEGORY_ZP = 25, /**< Separator, paragraph */
  279. UTF8PROC_CATEGORY_CC = 26, /**< Other, control */
  280. UTF8PROC_CATEGORY_CF = 27, /**< Other, format */
  281. UTF8PROC_CATEGORY_CS = 28, /**< Other, surrogate */
  282. UTF8PROC_CATEGORY_CO = 29, /**< Other, private use */
  283. } utf8proc_category_t;
  284. /** Bidirectional character classes. */
  285. typedef enum {
  286. UTF8PROC_BIDI_CLASS_L = 1, /**< Left-to-Right */
  287. UTF8PROC_BIDI_CLASS_LRE = 2, /**< Left-to-Right Embedding */
  288. UTF8PROC_BIDI_CLASS_LRO = 3, /**< Left-to-Right Override */
  289. UTF8PROC_BIDI_CLASS_R = 4, /**< Right-to-Left */
  290. UTF8PROC_BIDI_CLASS_AL = 5, /**< Right-to-Left Arabic */
  291. UTF8PROC_BIDI_CLASS_RLE = 6, /**< Right-to-Left Embedding */
  292. UTF8PROC_BIDI_CLASS_RLO = 7, /**< Right-to-Left Override */
  293. UTF8PROC_BIDI_CLASS_PDF = 8, /**< Pop Directional Format */
  294. UTF8PROC_BIDI_CLASS_EN = 9, /**< European Number */
  295. UTF8PROC_BIDI_CLASS_ES = 10, /**< European Separator */
  296. UTF8PROC_BIDI_CLASS_ET = 11, /**< European Number Terminator */
  297. UTF8PROC_BIDI_CLASS_AN = 12, /**< Arabic Number */
  298. UTF8PROC_BIDI_CLASS_CS = 13, /**< Common Number Separator */
  299. UTF8PROC_BIDI_CLASS_NSM = 14, /**< Nonspacing Mark */
  300. UTF8PROC_BIDI_CLASS_BN = 15, /**< Boundary Neutral */
  301. UTF8PROC_BIDI_CLASS_B = 16, /**< Paragraph Separator */
  302. UTF8PROC_BIDI_CLASS_S = 17, /**< Segment Separator */
  303. UTF8PROC_BIDI_CLASS_WS = 18, /**< Whitespace */
  304. UTF8PROC_BIDI_CLASS_ON = 19, /**< Other Neutrals */
  305. UTF8PROC_BIDI_CLASS_LRI = 20, /**< Left-to-Right Isolate */
  306. UTF8PROC_BIDI_CLASS_RLI = 21, /**< Right-to-Left Isolate */
  307. UTF8PROC_BIDI_CLASS_FSI = 22, /**< First Strong Isolate */
  308. UTF8PROC_BIDI_CLASS_PDI = 23, /**< Pop Directional Isolate */
  309. } utf8proc_bidi_class_t;
  310. /** Decomposition type. */
  311. typedef enum {
  312. UTF8PROC_DECOMP_TYPE_FONT = 1, /**< Font */
  313. UTF8PROC_DECOMP_TYPE_NOBREAK = 2, /**< Nobreak */
  314. UTF8PROC_DECOMP_TYPE_INITIAL = 3, /**< Initial */
  315. UTF8PROC_DECOMP_TYPE_MEDIAL = 4, /**< Medial */
  316. UTF8PROC_DECOMP_TYPE_FINAL = 5, /**< Final */
  317. UTF8PROC_DECOMP_TYPE_ISOLATED = 6, /**< Isolated */
  318. UTF8PROC_DECOMP_TYPE_CIRCLE = 7, /**< Circle */
  319. UTF8PROC_DECOMP_TYPE_SUPER = 8, /**< Super */
  320. UTF8PROC_DECOMP_TYPE_SUB = 9, /**< Sub */
  321. UTF8PROC_DECOMP_TYPE_VERTICAL = 10, /**< Vertical */
  322. UTF8PROC_DECOMP_TYPE_WIDE = 11, /**< Wide */
  323. UTF8PROC_DECOMP_TYPE_NARROW = 12, /**< Narrow */
  324. UTF8PROC_DECOMP_TYPE_SMALL = 13, /**< Small */
  325. UTF8PROC_DECOMP_TYPE_SQUARE = 14, /**< Square */
  326. UTF8PROC_DECOMP_TYPE_FRACTION = 15, /**< Fraction */
  327. UTF8PROC_DECOMP_TYPE_COMPAT = 16, /**< Compat */
  328. } utf8proc_decomp_type_t;
  329. /** Boundclass property. (TR29) */
  330. typedef enum {
  331. UTF8PROC_BOUNDCLASS_START = 0, /**< Start */
  332. UTF8PROC_BOUNDCLASS_OTHER = 1, /**< Other */
  333. UTF8PROC_BOUNDCLASS_CR = 2, /**< Cr */
  334. UTF8PROC_BOUNDCLASS_LF = 3, /**< Lf */
  335. UTF8PROC_BOUNDCLASS_CONTROL = 4, /**< Control */
  336. UTF8PROC_BOUNDCLASS_EXTEND = 5, /**< Extend */
  337. UTF8PROC_BOUNDCLASS_L = 6, /**< L */
  338. UTF8PROC_BOUNDCLASS_V = 7, /**< V */
  339. UTF8PROC_BOUNDCLASS_T = 8, /**< T */
  340. UTF8PROC_BOUNDCLASS_LV = 9, /**< Lv */
  341. UTF8PROC_BOUNDCLASS_LVT = 10, /**< Lvt */
  342. UTF8PROC_BOUNDCLASS_REGIONAL_INDICATOR = 11, /**< Regional indicator */
  343. UTF8PROC_BOUNDCLASS_SPACINGMARK = 12, /**< Spacingmark */
  344. UTF8PROC_BOUNDCLASS_PREPEND = 13, /**< Prepend */
  345. UTF8PROC_BOUNDCLASS_ZWJ = 14, /**< Zero Width Joiner */
  346. /* the following are no longer used in Unicode 11, but we keep
  347. the constants here for backward compatibility */
  348. UTF8PROC_BOUNDCLASS_E_BASE = 15, /**< Emoji Base */
  349. UTF8PROC_BOUNDCLASS_E_MODIFIER = 16, /**< Emoji Modifier */
  350. UTF8PROC_BOUNDCLASS_GLUE_AFTER_ZWJ = 17, /**< Glue_After_ZWJ */
  351. UTF8PROC_BOUNDCLASS_E_BASE_GAZ = 18, /**< E_BASE + GLUE_AFTER_ZJW */
  352. /* the Extended_Pictographic property is used in the Unicode 11
  353. grapheme-boundary rules, so we store it in the boundclass field */
  354. UTF8PROC_BOUNDCLASS_EXTENDED_PICTOGRAPHIC = 19,
  355. UTF8PROC_BOUNDCLASS_E_ZWG = 20, /* UTF8PROC_BOUNDCLASS_EXTENDED_PICTOGRAPHIC + ZWJ */
  356. } utf8proc_boundclass_t;
  357. /** Indic_Conjunct_Break property. (TR44) */
  358. typedef enum {
  359. UTF8PROC_INDIC_CONJUNCT_BREAK_NONE = 0,
  360. UTF8PROC_INDIC_CONJUNCT_BREAK_LINKER = 1,
  361. UTF8PROC_INDIC_CONJUNCT_BREAK_CONSONANT = 2,
  362. UTF8PROC_INDIC_CONJUNCT_BREAK_EXTEND = 3,
  363. } utf8proc_indic_conjunct_break_t;
  364. /**
  365. * Function pointer type passed to @ref utf8proc_map_custom and
  366. * @ref utf8proc_decompose_custom, which is used to specify a user-defined
  367. * mapping of codepoints to be applied in conjunction with other mappings.
  368. */
  369. typedef utf8proc_int32_t (*utf8proc_custom_func)(utf8proc_int32_t codepoint, void *data);
  370. /**
  371. * Array containing the byte lengths of a UTF-8 encoded codepoint based
  372. * on the first byte.
  373. */
  374. UTF8PROC_DLLEXPORT extern const utf8proc_int8_t utf8proc_utf8class[256];
  375. /**
  376. * Returns the utf8proc API version as a string MAJOR.MINOR.PATCH
  377. * (http://semver.org format), possibly with a "-dev" suffix for
  378. * development versions.
  379. */
  380. UTF8PROC_DLLEXPORT const char *utf8proc_version(void);
  381. /**
  382. * Returns the utf8proc supported Unicode version as a string MAJOR.MINOR.PATCH.
  383. */
  384. UTF8PROC_DLLEXPORT const char *utf8proc_unicode_version(void);
  385. /**
  386. * Returns an informative error string for the given utf8proc error code
  387. * (e.g. the error codes returned by @ref utf8proc_map).
  388. */
  389. UTF8PROC_DLLEXPORT const char *utf8proc_errmsg(utf8proc_ssize_t errcode);
  390. /**
  391. * Reads a single codepoint from the UTF-8 sequence being pointed to by `str`.
  392. * The maximum number of bytes read is `strlen`, unless `strlen` is
  393. * negative (in which case up to 4 bytes are read).
  394. *
  395. * If a valid codepoint could be read, it is stored in the variable
  396. * pointed to by `codepoint_ref`, otherwise that variable will be set to -1.
  397. * In case of success, the number of bytes read is returned; otherwise, a
  398. * negative error code is returned.
  399. */
  400. UTF8PROC_DLLEXPORT utf8proc_ssize_t utf8proc_iterate(const utf8proc_uint8_t *str, utf8proc_ssize_t strlen, utf8proc_int32_t *codepoint_ref);
  401. /**
  402. * Check if a codepoint is valid (regardless of whether it has been
  403. * assigned a value by the current Unicode standard).
  404. *
  405. * @return 1 if the given `codepoint` is valid and otherwise return 0.
  406. */
  407. UTF8PROC_DLLEXPORT utf8proc_bool utf8proc_codepoint_valid(utf8proc_int32_t codepoint);
  408. /**
  409. * Encodes the codepoint as an UTF-8 string in the byte array pointed
  410. * to by `dst`. This array must be at least 4 bytes long.
  411. *
  412. * In case of success the number of bytes written is returned, and
  413. * otherwise 0 is returned.
  414. *
  415. * This function does not check whether `codepoint` is valid Unicode.
  416. */
  417. UTF8PROC_DLLEXPORT utf8proc_ssize_t utf8proc_encode_char(utf8proc_int32_t codepoint, utf8proc_uint8_t *dst);
  418. /**
  419. * Look up the properties for a given codepoint.
  420. *
  421. * @param codepoint The Unicode codepoint.
  422. *
  423. * @returns
  424. * A pointer to a (constant) struct containing information about
  425. * the codepoint.
  426. * @par
  427. * If the codepoint is unassigned or invalid, a pointer to a special struct is
  428. * returned in which `category` is 0 (@ref UTF8PROC_CATEGORY_CN).
  429. */
  430. UTF8PROC_DLLEXPORT const utf8proc_property_t *utf8proc_get_property(utf8proc_int32_t codepoint);
  431. /** Decompose a codepoint into an array of codepoints.
  432. *
  433. * @param codepoint the codepoint.
  434. * @param dst the destination buffer.
  435. * @param bufsize the size of the destination buffer.
  436. * @param options one or more of the following flags:
  437. * - @ref UTF8PROC_REJECTNA - return an error `codepoint` is unassigned
  438. * - @ref UTF8PROC_IGNORE - strip "default ignorable" codepoints
  439. * - @ref UTF8PROC_CASEFOLD - apply Unicode casefolding
  440. * - @ref UTF8PROC_COMPAT - replace certain codepoints with their
  441. * compatibility decomposition
  442. * - @ref UTF8PROC_CHARBOUND - insert 0xFF bytes before each grapheme cluster
  443. * - @ref UTF8PROC_LUMP - lump certain different codepoints together
  444. * - @ref UTF8PROC_STRIPMARK - remove all character marks
  445. * - @ref UTF8PROC_STRIPNA - remove unassigned codepoints
  446. * @param last_boundclass
  447. * Pointer to an integer variable containing
  448. * the previous codepoint's (boundclass + indic_conjunct_break << 1) if the @ref UTF8PROC_CHARBOUND
  449. * option is used. If the string is being processed in order, this can be initialized to 0 for
  450. * the beginning of the string, and is thereafter updated automatically. Otherwise, this parameter is ignored.
  451. *
  452. * @return
  453. * In case of success, the number of codepoints written is returned; in case
  454. * of an error, a negative error code is returned (@ref utf8proc_errmsg).
  455. * @par
  456. * If the number of written codepoints would be bigger than `bufsize`, the
  457. * required buffer size is returned, while the buffer will be overwritten with
  458. * undefined data.
  459. */
  460. UTF8PROC_DLLEXPORT utf8proc_ssize_t utf8proc_decompose_char(
  461. utf8proc_int32_t codepoint, utf8proc_int32_t *dst, utf8proc_ssize_t bufsize,
  462. utf8proc_option_t options, int *last_boundclass
  463. );
  464. /**
  465. * The same as @ref utf8proc_decompose_char, but acts on a whole UTF-8
  466. * string and orders the decomposed sequences correctly.
  467. *
  468. * If the @ref UTF8PROC_NULLTERM flag in `options` is set, processing
  469. * will be stopped, when a NULL byte is encountered, otherwise `strlen`
  470. * bytes are processed. The result (in the form of 32-bit unicode
  471. * codepoints) is written into the buffer being pointed to by
  472. * `buffer` (which must contain at least `bufsize` entries). In case of
  473. * success, the number of codepoints written is returned; in case of an
  474. * error, a negative error code is returned (@ref utf8proc_errmsg).
  475. * See @ref utf8proc_decompose_custom to supply additional transformations.
  476. *
  477. * If the number of written codepoints would be bigger than `bufsize`, the
  478. * required buffer size is returned, while the buffer will be overwritten with
  479. * undefined data.
  480. */
  481. UTF8PROC_DLLEXPORT utf8proc_ssize_t utf8proc_decompose(
  482. const utf8proc_uint8_t *str, utf8proc_ssize_t strlen,
  483. utf8proc_int32_t *buffer, utf8proc_ssize_t bufsize, utf8proc_option_t options
  484. );
  485. /**
  486. * The same as @ref utf8proc_decompose, but also takes a `custom_func` mapping function
  487. * that is called on each codepoint in `str` before any other transformations
  488. * (along with a `custom_data` pointer that is passed through to `custom_func`).
  489. * The `custom_func` argument is ignored if it is `NULL`. See also @ref utf8proc_map_custom.
  490. */
  491. UTF8PROC_DLLEXPORT utf8proc_ssize_t utf8proc_decompose_custom(
  492. const utf8proc_uint8_t *str, utf8proc_ssize_t strlen,
  493. utf8proc_int32_t *buffer, utf8proc_ssize_t bufsize, utf8proc_option_t options,
  494. utf8proc_custom_func custom_func, void *custom_data
  495. );
  496. /**
  497. * Normalizes the sequence of `length` codepoints pointed to by `buffer`
  498. * in-place (i.e., the result is also stored in `buffer`).
  499. *
  500. * @param buffer the (native-endian UTF-32) unicode codepoints to re-encode.
  501. * @param length the length (in codepoints) of the buffer.
  502. * @param options a bitwise or (`|`) of one or more of the following flags:
  503. * - @ref UTF8PROC_NLF2LS - convert LF, CRLF, CR and NEL into LS
  504. * - @ref UTF8PROC_NLF2PS - convert LF, CRLF, CR and NEL into PS
  505. * - @ref UTF8PROC_NLF2LF - convert LF, CRLF, CR and NEL into LF
  506. * - @ref UTF8PROC_STRIPCC - strip or convert all non-affected control characters
  507. * - @ref UTF8PROC_COMPOSE - try to combine decomposed codepoints into composite
  508. * codepoints
  509. * - @ref UTF8PROC_STABLE - prohibit combining characters that would violate
  510. * the unicode versioning stability
  511. *
  512. * @return
  513. * In case of success, the length (in codepoints) of the normalized UTF-32 string is
  514. * returned; otherwise, a negative error code is returned (@ref utf8proc_errmsg).
  515. *
  516. * @warning The entries of the array pointed to by `str` have to be in the
  517. * range `0x0000` to `0x10FFFF`. Otherwise, the program might crash!
  518. */
  519. UTF8PROC_DLLEXPORT utf8proc_ssize_t utf8proc_normalize_utf32(utf8proc_int32_t *buffer, utf8proc_ssize_t length, utf8proc_option_t options);
  520. /**
  521. * Reencodes the sequence of `length` codepoints pointed to by `buffer`
  522. * UTF-8 data in-place (i.e., the result is also stored in `buffer`).
  523. * Can optionally normalize the UTF-32 sequence prior to UTF-8 conversion.
  524. *
  525. * @param buffer the (native-endian UTF-32) unicode codepoints to re-encode.
  526. * @param length the length (in codepoints) of the buffer.
  527. * @param options a bitwise or (`|`) of one or more of the following flags:
  528. * - @ref UTF8PROC_NLF2LS - convert LF, CRLF, CR and NEL into LS
  529. * - @ref UTF8PROC_NLF2PS - convert LF, CRLF, CR and NEL into PS
  530. * - @ref UTF8PROC_NLF2LF - convert LF, CRLF, CR and NEL into LF
  531. * - @ref UTF8PROC_STRIPCC - strip or convert all non-affected control characters
  532. * - @ref UTF8PROC_COMPOSE - try to combine decomposed codepoints into composite
  533. * codepoints
  534. * - @ref UTF8PROC_STABLE - prohibit combining characters that would violate
  535. * the unicode versioning stability
  536. * - @ref UTF8PROC_CHARBOUND - insert 0xFF bytes before each grapheme cluster
  537. *
  538. * @return
  539. * In case of success, the length (in bytes) of the resulting nul-terminated
  540. * UTF-8 string is returned; otherwise, a negative error code is returned
  541. * (@ref utf8proc_errmsg).
  542. *
  543. * @warning The amount of free space pointed to by `buffer` must
  544. * exceed the amount of the input data by one byte, and the
  545. * entries of the array pointed to by `str` have to be in the
  546. * range `0x0000` to `0x10FFFF`. Otherwise, the program might crash!
  547. */
  548. UTF8PROC_DLLEXPORT utf8proc_ssize_t utf8proc_reencode(utf8proc_int32_t *buffer, utf8proc_ssize_t length, utf8proc_option_t options);
  549. /**
  550. * Given a pair of consecutive codepoints, return whether a grapheme break is
  551. * permitted between them (as defined by the extended grapheme clusters in UAX#29).
  552. *
  553. * @param codepoint1 The first codepoint.
  554. * @param codepoint2 The second codepoint, occurring consecutively after `codepoint1`.
  555. * @param state Beginning with Version 29 (Unicode 9.0.0), this algorithm requires
  556. * state to break graphemes. This state can be passed in as a pointer
  557. * in the `state` argument and should initially be set to 0. If the
  558. * state is not passed in (i.e. a null pointer is passed), UAX#29 rules
  559. * GB10/12/13 which require this state will not be applied, essentially
  560. * matching the rules in Unicode 8.0.0.
  561. *
  562. * @warning If the state parameter is used, `utf8proc_grapheme_break_stateful` must
  563. * be called IN ORDER on ALL potential breaks in a string. However, it
  564. * is safe to reset the state to zero after a grapheme break.
  565. */
  566. UTF8PROC_DLLEXPORT utf8proc_bool utf8proc_grapheme_break_stateful(
  567. utf8proc_int32_t codepoint1, utf8proc_int32_t codepoint2, utf8proc_int32_t *state);
  568. /**
  569. * Same as @ref utf8proc_grapheme_break_stateful, except without support for the
  570. * Unicode 9 additions to the algorithm. Supported for legacy reasons.
  571. */
  572. UTF8PROC_DLLEXPORT utf8proc_bool utf8proc_grapheme_break(
  573. utf8proc_int32_t codepoint1, utf8proc_int32_t codepoint2);
  574. /**
  575. * Given a codepoint `c`, return the codepoint of the corresponding
  576. * lower-case character, if any; otherwise (if there is no lower-case
  577. * variant, or if `c` is not a valid codepoint) return `c`.
  578. */
  579. UTF8PROC_DLLEXPORT utf8proc_int32_t utf8proc_tolower(utf8proc_int32_t c);
  580. /**
  581. * Given a codepoint `c`, return the codepoint of the corresponding
  582. * upper-case character, if any; otherwise (if there is no upper-case
  583. * variant, or if `c` is not a valid codepoint) return `c`.
  584. */
  585. UTF8PROC_DLLEXPORT utf8proc_int32_t utf8proc_toupper(utf8proc_int32_t c);
  586. /**
  587. * Given a codepoint `c`, return the codepoint of the corresponding
  588. * title-case character, if any; otherwise (if there is no title-case
  589. * variant, or if `c` is not a valid codepoint) return `c`.
  590. */
  591. UTF8PROC_DLLEXPORT utf8proc_int32_t utf8proc_totitle(utf8proc_int32_t c);
  592. /**
  593. * Given a codepoint `c`, return `1` if the codepoint corresponds to a lower-case character
  594. * and `0` otherwise.
  595. */
  596. UTF8PROC_DLLEXPORT int utf8proc_islower(utf8proc_int32_t c);
  597. /**
  598. * Given a codepoint `c`, return `1` if the codepoint corresponds to an upper-case character
  599. * and `0` otherwise.
  600. */
  601. UTF8PROC_DLLEXPORT int utf8proc_isupper(utf8proc_int32_t c);
  602. /**
  603. * Given a codepoint, return a character width analogous to `wcwidth(codepoint)`,
  604. * except that a width of 0 is returned for non-printable codepoints
  605. * instead of -1 as in `wcwidth`.
  606. *
  607. * @note
  608. * If you want to check for particular types of non-printable characters,
  609. * (analogous to `isprint` or `iscntrl`), use @ref utf8proc_category. */
  610. UTF8PROC_DLLEXPORT int utf8proc_charwidth(utf8proc_int32_t codepoint);
  611. /**
  612. * Return the Unicode category for the codepoint (one of the
  613. * @ref utf8proc_category_t constants.)
  614. */
  615. UTF8PROC_DLLEXPORT utf8proc_category_t utf8proc_category(utf8proc_int32_t codepoint);
  616. /**
  617. * Return the two-letter (nul-terminated) Unicode category string for
  618. * the codepoint (e.g. `"Lu"` or `"Co"`).
  619. */
  620. UTF8PROC_DLLEXPORT const char *utf8proc_category_string(utf8proc_int32_t codepoint);
  621. /**
  622. * Maps the given UTF-8 string pointed to by `str` to a new UTF-8
  623. * string, allocated dynamically by `malloc` and returned via `dstptr`.
  624. *
  625. * If the @ref UTF8PROC_NULLTERM flag in the `options` field is set,
  626. * the length is determined by a NULL terminator, otherwise the
  627. * parameter `strlen` is evaluated to determine the string length, but
  628. * in any case the result will be NULL terminated (though it might
  629. * contain NULL characters with the string if `str` contained NULL
  630. * characters). Other flags in the `options` field are passed to the
  631. * functions defined above, and regarded as described. See also
  632. * @ref utf8proc_map_custom to supply a custom codepoint transformation.
  633. *
  634. * In case of success the length of the new string is returned,
  635. * otherwise a negative error code is returned.
  636. *
  637. * @note The memory of the new UTF-8 string will have been allocated
  638. * with `malloc`, and should therefore be deallocated with `free`.
  639. */
  640. UTF8PROC_DLLEXPORT utf8proc_ssize_t utf8proc_map(
  641. const utf8proc_uint8_t *str, utf8proc_ssize_t strlen, utf8proc_uint8_t **dstptr, utf8proc_option_t options
  642. );
  643. /**
  644. * Like @ref utf8proc_map, but also takes a `custom_func` mapping function
  645. * that is called on each codepoint in `str` before any other transformations
  646. * (along with a `custom_data` pointer that is passed through to `custom_func`).
  647. * The `custom_func` argument is ignored if it is `NULL`.
  648. */
  649. UTF8PROC_DLLEXPORT utf8proc_ssize_t utf8proc_map_custom(
  650. const utf8proc_uint8_t *str, utf8proc_ssize_t strlen, utf8proc_uint8_t **dstptr, utf8proc_option_t options,
  651. utf8proc_custom_func custom_func, void *custom_data
  652. );
  653. /** @name Unicode normalization
  654. *
  655. * Returns a pointer to newly allocated memory of a NFD, NFC, NFKD, NFKC or
  656. * NFKC_Casefold normalized version of the null-terminated string `str`. These
  657. * are shortcuts to calling @ref utf8proc_map with @ref UTF8PROC_NULLTERM
  658. * combined with @ref UTF8PROC_STABLE and flags indicating the normalization.
  659. */
  660. /** @{ */
  661. /** NFD normalization (@ref UTF8PROC_DECOMPOSE). */
  662. UTF8PROC_DLLEXPORT utf8proc_uint8_t *utf8proc_NFD(const utf8proc_uint8_t *str);
  663. /** NFC normalization (@ref UTF8PROC_COMPOSE). */
  664. UTF8PROC_DLLEXPORT utf8proc_uint8_t *utf8proc_NFC(const utf8proc_uint8_t *str);
  665. /** NFKD normalization (@ref UTF8PROC_DECOMPOSE and @ref UTF8PROC_COMPAT). */
  666. UTF8PROC_DLLEXPORT utf8proc_uint8_t *utf8proc_NFKD(const utf8proc_uint8_t *str);
  667. /** NFKC normalization (@ref UTF8PROC_COMPOSE and @ref UTF8PROC_COMPAT). */
  668. UTF8PROC_DLLEXPORT utf8proc_uint8_t *utf8proc_NFKC(const utf8proc_uint8_t *str);
  669. /**
  670. * NFKC_Casefold normalization (@ref UTF8PROC_COMPOSE and @ref UTF8PROC_COMPAT
  671. * and @ref UTF8PROC_CASEFOLD and @ref UTF8PROC_IGNORE).
  672. **/
  673. UTF8PROC_DLLEXPORT utf8proc_uint8_t *utf8proc_NFKC_Casefold(const utf8proc_uint8_t *str);
  674. /** @} */
  675. #ifdef __cplusplus
  676. }
  677. #endif
  678. #endif