utf8proc.h 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719
  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 8
  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:8;
  249. } utf8proc_property_t;
  250. /** Unicode categories. */
  251. typedef enum {
  252. UTF8PROC_CATEGORY_CN = 0, /**< Other, not assigned */
  253. UTF8PROC_CATEGORY_LU = 1, /**< Letter, uppercase */
  254. UTF8PROC_CATEGORY_LL = 2, /**< Letter, lowercase */
  255. UTF8PROC_CATEGORY_LT = 3, /**< Letter, titlecase */
  256. UTF8PROC_CATEGORY_LM = 4, /**< Letter, modifier */
  257. UTF8PROC_CATEGORY_LO = 5, /**< Letter, other */
  258. UTF8PROC_CATEGORY_MN = 6, /**< Mark, nonspacing */
  259. UTF8PROC_CATEGORY_MC = 7, /**< Mark, spacing combining */
  260. UTF8PROC_CATEGORY_ME = 8, /**< Mark, enclosing */
  261. UTF8PROC_CATEGORY_ND = 9, /**< Number, decimal digit */
  262. UTF8PROC_CATEGORY_NL = 10, /**< Number, letter */
  263. UTF8PROC_CATEGORY_NO = 11, /**< Number, other */
  264. UTF8PROC_CATEGORY_PC = 12, /**< Punctuation, connector */
  265. UTF8PROC_CATEGORY_PD = 13, /**< Punctuation, dash */
  266. UTF8PROC_CATEGORY_PS = 14, /**< Punctuation, open */
  267. UTF8PROC_CATEGORY_PE = 15, /**< Punctuation, close */
  268. UTF8PROC_CATEGORY_PI = 16, /**< Punctuation, initial quote */
  269. UTF8PROC_CATEGORY_PF = 17, /**< Punctuation, final quote */
  270. UTF8PROC_CATEGORY_PO = 18, /**< Punctuation, other */
  271. UTF8PROC_CATEGORY_SM = 19, /**< Symbol, math */
  272. UTF8PROC_CATEGORY_SC = 20, /**< Symbol, currency */
  273. UTF8PROC_CATEGORY_SK = 21, /**< Symbol, modifier */
  274. UTF8PROC_CATEGORY_SO = 22, /**< Symbol, other */
  275. UTF8PROC_CATEGORY_ZS = 23, /**< Separator, space */
  276. UTF8PROC_CATEGORY_ZL = 24, /**< Separator, line */
  277. UTF8PROC_CATEGORY_ZP = 25, /**< Separator, paragraph */
  278. UTF8PROC_CATEGORY_CC = 26, /**< Other, control */
  279. UTF8PROC_CATEGORY_CF = 27, /**< Other, format */
  280. UTF8PROC_CATEGORY_CS = 28, /**< Other, surrogate */
  281. UTF8PROC_CATEGORY_CO = 29, /**< Other, private use */
  282. } utf8proc_category_t;
  283. /** Bidirectional character classes. */
  284. typedef enum {
  285. UTF8PROC_BIDI_CLASS_L = 1, /**< Left-to-Right */
  286. UTF8PROC_BIDI_CLASS_LRE = 2, /**< Left-to-Right Embedding */
  287. UTF8PROC_BIDI_CLASS_LRO = 3, /**< Left-to-Right Override */
  288. UTF8PROC_BIDI_CLASS_R = 4, /**< Right-to-Left */
  289. UTF8PROC_BIDI_CLASS_AL = 5, /**< Right-to-Left Arabic */
  290. UTF8PROC_BIDI_CLASS_RLE = 6, /**< Right-to-Left Embedding */
  291. UTF8PROC_BIDI_CLASS_RLO = 7, /**< Right-to-Left Override */
  292. UTF8PROC_BIDI_CLASS_PDF = 8, /**< Pop Directional Format */
  293. UTF8PROC_BIDI_CLASS_EN = 9, /**< European Number */
  294. UTF8PROC_BIDI_CLASS_ES = 10, /**< European Separator */
  295. UTF8PROC_BIDI_CLASS_ET = 11, /**< European Number Terminator */
  296. UTF8PROC_BIDI_CLASS_AN = 12, /**< Arabic Number */
  297. UTF8PROC_BIDI_CLASS_CS = 13, /**< Common Number Separator */
  298. UTF8PROC_BIDI_CLASS_NSM = 14, /**< Nonspacing Mark */
  299. UTF8PROC_BIDI_CLASS_BN = 15, /**< Boundary Neutral */
  300. UTF8PROC_BIDI_CLASS_B = 16, /**< Paragraph Separator */
  301. UTF8PROC_BIDI_CLASS_S = 17, /**< Segment Separator */
  302. UTF8PROC_BIDI_CLASS_WS = 18, /**< Whitespace */
  303. UTF8PROC_BIDI_CLASS_ON = 19, /**< Other Neutrals */
  304. UTF8PROC_BIDI_CLASS_LRI = 20, /**< Left-to-Right Isolate */
  305. UTF8PROC_BIDI_CLASS_RLI = 21, /**< Right-to-Left Isolate */
  306. UTF8PROC_BIDI_CLASS_FSI = 22, /**< First Strong Isolate */
  307. UTF8PROC_BIDI_CLASS_PDI = 23, /**< Pop Directional Isolate */
  308. } utf8proc_bidi_class_t;
  309. /** Decomposition type. */
  310. typedef enum {
  311. UTF8PROC_DECOMP_TYPE_FONT = 1, /**< Font */
  312. UTF8PROC_DECOMP_TYPE_NOBREAK = 2, /**< Nobreak */
  313. UTF8PROC_DECOMP_TYPE_INITIAL = 3, /**< Initial */
  314. UTF8PROC_DECOMP_TYPE_MEDIAL = 4, /**< Medial */
  315. UTF8PROC_DECOMP_TYPE_FINAL = 5, /**< Final */
  316. UTF8PROC_DECOMP_TYPE_ISOLATED = 6, /**< Isolated */
  317. UTF8PROC_DECOMP_TYPE_CIRCLE = 7, /**< Circle */
  318. UTF8PROC_DECOMP_TYPE_SUPER = 8, /**< Super */
  319. UTF8PROC_DECOMP_TYPE_SUB = 9, /**< Sub */
  320. UTF8PROC_DECOMP_TYPE_VERTICAL = 10, /**< Vertical */
  321. UTF8PROC_DECOMP_TYPE_WIDE = 11, /**< Wide */
  322. UTF8PROC_DECOMP_TYPE_NARROW = 12, /**< Narrow */
  323. UTF8PROC_DECOMP_TYPE_SMALL = 13, /**< Small */
  324. UTF8PROC_DECOMP_TYPE_SQUARE = 14, /**< Square */
  325. UTF8PROC_DECOMP_TYPE_FRACTION = 15, /**< Fraction */
  326. UTF8PROC_DECOMP_TYPE_COMPAT = 16, /**< Compat */
  327. } utf8proc_decomp_type_t;
  328. /** Boundclass property. (TR29) */
  329. typedef enum {
  330. UTF8PROC_BOUNDCLASS_START = 0, /**< Start */
  331. UTF8PROC_BOUNDCLASS_OTHER = 1, /**< Other */
  332. UTF8PROC_BOUNDCLASS_CR = 2, /**< Cr */
  333. UTF8PROC_BOUNDCLASS_LF = 3, /**< Lf */
  334. UTF8PROC_BOUNDCLASS_CONTROL = 4, /**< Control */
  335. UTF8PROC_BOUNDCLASS_EXTEND = 5, /**< Extend */
  336. UTF8PROC_BOUNDCLASS_L = 6, /**< L */
  337. UTF8PROC_BOUNDCLASS_V = 7, /**< V */
  338. UTF8PROC_BOUNDCLASS_T = 8, /**< T */
  339. UTF8PROC_BOUNDCLASS_LV = 9, /**< Lv */
  340. UTF8PROC_BOUNDCLASS_LVT = 10, /**< Lvt */
  341. UTF8PROC_BOUNDCLASS_REGIONAL_INDICATOR = 11, /**< Regional indicator */
  342. UTF8PROC_BOUNDCLASS_SPACINGMARK = 12, /**< Spacingmark */
  343. UTF8PROC_BOUNDCLASS_PREPEND = 13, /**< Prepend */
  344. UTF8PROC_BOUNDCLASS_ZWJ = 14, /**< Zero Width Joiner */
  345. /* the following are no longer used in Unicode 11, but we keep
  346. the constants here for backward compatibility */
  347. UTF8PROC_BOUNDCLASS_E_BASE = 15, /**< Emoji Base */
  348. UTF8PROC_BOUNDCLASS_E_MODIFIER = 16, /**< Emoji Modifier */
  349. UTF8PROC_BOUNDCLASS_GLUE_AFTER_ZWJ = 17, /**< Glue_After_ZWJ */
  350. UTF8PROC_BOUNDCLASS_E_BASE_GAZ = 18, /**< E_BASE + GLUE_AFTER_ZJW */
  351. /* the Extended_Pictographic property is used in the Unicode 11
  352. grapheme-boundary rules, so we store it in the boundclass field */
  353. UTF8PROC_BOUNDCLASS_EXTENDED_PICTOGRAPHIC = 19,
  354. UTF8PROC_BOUNDCLASS_E_ZWG = 20, /* UTF8PROC_BOUNDCLASS_EXTENDED_PICTOGRAPHIC + ZWJ */
  355. } utf8proc_boundclass_t;
  356. /**
  357. * Function pointer type passed to @ref utf8proc_map_custom and
  358. * @ref utf8proc_decompose_custom, which is used to specify a user-defined
  359. * mapping of codepoints to be applied in conjunction with other mappings.
  360. */
  361. typedef utf8proc_int32_t (*utf8proc_custom_func)(utf8proc_int32_t codepoint, void *data);
  362. /**
  363. * Array containing the byte lengths of a UTF-8 encoded codepoint based
  364. * on the first byte.
  365. */
  366. UTF8PROC_DLLEXPORT extern const utf8proc_int8_t utf8proc_utf8class[256];
  367. /**
  368. * Returns the utf8proc API version as a string MAJOR.MINOR.PATCH
  369. * (http://semver.org format), possibly with a "-dev" suffix for
  370. * development versions.
  371. */
  372. UTF8PROC_DLLEXPORT const char *utf8proc_version(void);
  373. /**
  374. * Returns the utf8proc supported Unicode version as a string MAJOR.MINOR.PATCH.
  375. */
  376. UTF8PROC_DLLEXPORT const char *utf8proc_unicode_version(void);
  377. /**
  378. * Returns an informative error string for the given utf8proc error code
  379. * (e.g. the error codes returned by @ref utf8proc_map).
  380. */
  381. UTF8PROC_DLLEXPORT const char *utf8proc_errmsg(utf8proc_ssize_t errcode);
  382. /**
  383. * Reads a single codepoint from the UTF-8 sequence being pointed to by `str`.
  384. * The maximum number of bytes read is `strlen`, unless `strlen` is
  385. * negative (in which case up to 4 bytes are read).
  386. *
  387. * If a valid codepoint could be read, it is stored in the variable
  388. * pointed to by `codepoint_ref`, otherwise that variable will be set to -1.
  389. * In case of success, the number of bytes read is returned; otherwise, a
  390. * negative error code is returned.
  391. */
  392. UTF8PROC_DLLEXPORT utf8proc_ssize_t utf8proc_iterate(const utf8proc_uint8_t *str, utf8proc_ssize_t strlen, utf8proc_int32_t *codepoint_ref);
  393. /**
  394. * Check if a codepoint is valid (regardless of whether it has been
  395. * assigned a value by the current Unicode standard).
  396. *
  397. * @return 1 if the given `codepoint` is valid and otherwise return 0.
  398. */
  399. UTF8PROC_DLLEXPORT utf8proc_bool utf8proc_codepoint_valid(utf8proc_int32_t codepoint);
  400. /**
  401. * Encodes the codepoint as an UTF-8 string in the byte array pointed
  402. * to by `dst`. This array must be at least 4 bytes long.
  403. *
  404. * In case of success the number of bytes written is returned, and
  405. * otherwise 0 is returned.
  406. *
  407. * This function does not check whether `codepoint` is valid Unicode.
  408. */
  409. UTF8PROC_DLLEXPORT utf8proc_ssize_t utf8proc_encode_char(utf8proc_int32_t codepoint, utf8proc_uint8_t *dst);
  410. /**
  411. * Look up the properties for a given codepoint.
  412. *
  413. * @param codepoint The Unicode codepoint.
  414. *
  415. * @returns
  416. * A pointer to a (constant) struct containing information about
  417. * the codepoint.
  418. * @par
  419. * If the codepoint is unassigned or invalid, a pointer to a special struct is
  420. * returned in which `category` is 0 (@ref UTF8PROC_CATEGORY_CN).
  421. */
  422. UTF8PROC_DLLEXPORT const utf8proc_property_t *utf8proc_get_property(utf8proc_int32_t codepoint);
  423. /** Decompose a codepoint into an array of codepoints.
  424. *
  425. * @param codepoint the codepoint.
  426. * @param dst the destination buffer.
  427. * @param bufsize the size of the destination buffer.
  428. * @param options one or more of the following flags:
  429. * - @ref UTF8PROC_REJECTNA - return an error `codepoint` is unassigned
  430. * - @ref UTF8PROC_IGNORE - strip "default ignorable" codepoints
  431. * - @ref UTF8PROC_CASEFOLD - apply Unicode casefolding
  432. * - @ref UTF8PROC_COMPAT - replace certain codepoints with their
  433. * compatibility decomposition
  434. * - @ref UTF8PROC_CHARBOUND - insert 0xFF bytes before each grapheme cluster
  435. * - @ref UTF8PROC_LUMP - lump certain different codepoints together
  436. * - @ref UTF8PROC_STRIPMARK - remove all character marks
  437. * - @ref UTF8PROC_STRIPNA - remove unassigned codepoints
  438. * @param last_boundclass
  439. * Pointer to an integer variable containing
  440. * the previous codepoint's boundary class if the @ref UTF8PROC_CHARBOUND
  441. * option is used. Otherwise, this parameter is ignored.
  442. *
  443. * @return
  444. * In case of success, the number of codepoints written is returned; in case
  445. * of an error, a negative error code is returned (@ref utf8proc_errmsg).
  446. * @par
  447. * If the number of written codepoints would be bigger than `bufsize`, the
  448. * required buffer size is returned, while the buffer will be overwritten with
  449. * undefined data.
  450. */
  451. UTF8PROC_DLLEXPORT utf8proc_ssize_t utf8proc_decompose_char(
  452. utf8proc_int32_t codepoint, utf8proc_int32_t *dst, utf8proc_ssize_t bufsize,
  453. utf8proc_option_t options, int *last_boundclass
  454. );
  455. /**
  456. * The same as @ref utf8proc_decompose_char, but acts on a whole UTF-8
  457. * string and orders the decomposed sequences correctly.
  458. *
  459. * If the @ref UTF8PROC_NULLTERM flag in `options` is set, processing
  460. * will be stopped, when a NULL byte is encountered, otherwise `strlen`
  461. * bytes are processed. The result (in the form of 32-bit unicode
  462. * codepoints) is written into the buffer being pointed to by
  463. * `buffer` (which must contain at least `bufsize` entries). In case of
  464. * success, the number of codepoints written is returned; in case of an
  465. * error, a negative error code is returned (@ref utf8proc_errmsg).
  466. * See @ref utf8proc_decompose_custom to supply additional transformations.
  467. *
  468. * If the number of written codepoints would be bigger than `bufsize`, the
  469. * required buffer size is returned, while the buffer will be overwritten with
  470. * undefined data.
  471. */
  472. UTF8PROC_DLLEXPORT utf8proc_ssize_t utf8proc_decompose(
  473. const utf8proc_uint8_t *str, utf8proc_ssize_t strlen,
  474. utf8proc_int32_t *buffer, utf8proc_ssize_t bufsize, utf8proc_option_t options
  475. );
  476. /**
  477. * The same as @ref utf8proc_decompose, but also takes a `custom_func` mapping function
  478. * that is called on each codepoint in `str` before any other transformations
  479. * (along with a `custom_data` pointer that is passed through to `custom_func`).
  480. * The `custom_func` argument is ignored if it is `NULL`. See also @ref utf8proc_map_custom.
  481. */
  482. UTF8PROC_DLLEXPORT utf8proc_ssize_t utf8proc_decompose_custom(
  483. const utf8proc_uint8_t *str, utf8proc_ssize_t strlen,
  484. utf8proc_int32_t *buffer, utf8proc_ssize_t bufsize, utf8proc_option_t options,
  485. utf8proc_custom_func custom_func, void *custom_data
  486. );
  487. /**
  488. * Normalizes the sequence of `length` codepoints pointed to by `buffer`
  489. * in-place (i.e., the result is also stored in `buffer`).
  490. *
  491. * @param buffer the (native-endian UTF-32) unicode codepoints to re-encode.
  492. * @param length the length (in codepoints) of the buffer.
  493. * @param options a bitwise or (`|`) of one or more of the following flags:
  494. * - @ref UTF8PROC_NLF2LS - convert LF, CRLF, CR and NEL into LS
  495. * - @ref UTF8PROC_NLF2PS - convert LF, CRLF, CR and NEL into PS
  496. * - @ref UTF8PROC_NLF2LF - convert LF, CRLF, CR and NEL into LF
  497. * - @ref UTF8PROC_STRIPCC - strip or convert all non-affected control characters
  498. * - @ref UTF8PROC_COMPOSE - try to combine decomposed codepoints into composite
  499. * codepoints
  500. * - @ref UTF8PROC_STABLE - prohibit combining characters that would violate
  501. * the unicode versioning stability
  502. *
  503. * @return
  504. * In case of success, the length (in codepoints) of the normalized UTF-32 string is
  505. * returned; otherwise, a negative error code is returned (@ref utf8proc_errmsg).
  506. *
  507. * @warning The entries of the array pointed to by `str` have to be in the
  508. * range `0x0000` to `0x10FFFF`. Otherwise, the program might crash!
  509. */
  510. UTF8PROC_DLLEXPORT utf8proc_ssize_t utf8proc_normalize_utf32(utf8proc_int32_t *buffer, utf8proc_ssize_t length, utf8proc_option_t options);
  511. /**
  512. * Reencodes the sequence of `length` codepoints pointed to by `buffer`
  513. * UTF-8 data in-place (i.e., the result is also stored in `buffer`).
  514. * Can optionally normalize the UTF-32 sequence prior to UTF-8 conversion.
  515. *
  516. * @param buffer the (native-endian UTF-32) unicode codepoints to re-encode.
  517. * @param length the length (in codepoints) of the buffer.
  518. * @param options a bitwise or (`|`) of one or more of the following flags:
  519. * - @ref UTF8PROC_NLF2LS - convert LF, CRLF, CR and NEL into LS
  520. * - @ref UTF8PROC_NLF2PS - convert LF, CRLF, CR and NEL into PS
  521. * - @ref UTF8PROC_NLF2LF - convert LF, CRLF, CR and NEL into LF
  522. * - @ref UTF8PROC_STRIPCC - strip or convert all non-affected control characters
  523. * - @ref UTF8PROC_COMPOSE - try to combine decomposed codepoints into composite
  524. * codepoints
  525. * - @ref UTF8PROC_STABLE - prohibit combining characters that would violate
  526. * the unicode versioning stability
  527. * - @ref UTF8PROC_CHARBOUND - insert 0xFF bytes before each grapheme cluster
  528. *
  529. * @return
  530. * In case of success, the length (in bytes) of the resulting nul-terminated
  531. * UTF-8 string is returned; otherwise, a negative error code is returned
  532. * (@ref utf8proc_errmsg).
  533. *
  534. * @warning The amount of free space pointed to by `buffer` must
  535. * exceed the amount of the input data by one byte, and the
  536. * entries of the array pointed to by `str` have to be in the
  537. * range `0x0000` to `0x10FFFF`. Otherwise, the program might crash!
  538. */
  539. UTF8PROC_DLLEXPORT utf8proc_ssize_t utf8proc_reencode(utf8proc_int32_t *buffer, utf8proc_ssize_t length, utf8proc_option_t options);
  540. /**
  541. * Given a pair of consecutive codepoints, return whether a grapheme break is
  542. * permitted between them (as defined by the extended grapheme clusters in UAX#29).
  543. *
  544. * @param codepoint1 The first codepoint.
  545. * @param codepoint2 The second codepoint, occurring consecutively after `codepoint1`.
  546. * @param state Beginning with Version 29 (Unicode 9.0.0), this algorithm requires
  547. * state to break graphemes. This state can be passed in as a pointer
  548. * in the `state` argument and should initially be set to 0. If the
  549. * state is not passed in (i.e. a null pointer is passed), UAX#29 rules
  550. * GB10/12/13 which require this state will not be applied, essentially
  551. * matching the rules in Unicode 8.0.0.
  552. *
  553. * @warning If the state parameter is used, `utf8proc_grapheme_break_stateful` must
  554. * be called IN ORDER on ALL potential breaks in a string. However, it
  555. * is safe to reset the state to zero after a grapheme break.
  556. */
  557. UTF8PROC_DLLEXPORT utf8proc_bool utf8proc_grapheme_break_stateful(
  558. utf8proc_int32_t codepoint1, utf8proc_int32_t codepoint2, utf8proc_int32_t *state);
  559. /**
  560. * Same as @ref utf8proc_grapheme_break_stateful, except without support for the
  561. * Unicode 9 additions to the algorithm. Supported for legacy reasons.
  562. */
  563. UTF8PROC_DLLEXPORT utf8proc_bool utf8proc_grapheme_break(
  564. utf8proc_int32_t codepoint1, utf8proc_int32_t codepoint2);
  565. /**
  566. * Given a codepoint `c`, return the codepoint of the corresponding
  567. * lower-case character, if any; otherwise (if there is no lower-case
  568. * variant, or if `c` is not a valid codepoint) return `c`.
  569. */
  570. UTF8PROC_DLLEXPORT utf8proc_int32_t utf8proc_tolower(utf8proc_int32_t c);
  571. /**
  572. * Given a codepoint `c`, return the codepoint of the corresponding
  573. * upper-case character, if any; otherwise (if there is no upper-case
  574. * variant, or if `c` is not a valid codepoint) return `c`.
  575. */
  576. UTF8PROC_DLLEXPORT utf8proc_int32_t utf8proc_toupper(utf8proc_int32_t c);
  577. /**
  578. * Given a codepoint `c`, return the codepoint of the corresponding
  579. * title-case character, if any; otherwise (if there is no title-case
  580. * variant, or if `c` is not a valid codepoint) return `c`.
  581. */
  582. UTF8PROC_DLLEXPORT utf8proc_int32_t utf8proc_totitle(utf8proc_int32_t c);
  583. /**
  584. * Given a codepoint `c`, return `1` if the codepoint corresponds to a lower-case character
  585. * and `0` otherwise.
  586. */
  587. UTF8PROC_DLLEXPORT int utf8proc_islower(utf8proc_int32_t c);
  588. /**
  589. * Given a codepoint `c`, return `1` if the codepoint corresponds to an upper-case character
  590. * and `0` otherwise.
  591. */
  592. UTF8PROC_DLLEXPORT int utf8proc_isupper(utf8proc_int32_t c);
  593. /**
  594. * Given a codepoint, return a character width analogous to `wcwidth(codepoint)`,
  595. * except that a width of 0 is returned for non-printable codepoints
  596. * instead of -1 as in `wcwidth`.
  597. *
  598. * @note
  599. * If you want to check for particular types of non-printable characters,
  600. * (analogous to `isprint` or `iscntrl`), use @ref utf8proc_category. */
  601. UTF8PROC_DLLEXPORT int utf8proc_charwidth(utf8proc_int32_t codepoint);
  602. /**
  603. * Return the Unicode category for the codepoint (one of the
  604. * @ref utf8proc_category_t constants.)
  605. */
  606. UTF8PROC_DLLEXPORT utf8proc_category_t utf8proc_category(utf8proc_int32_t codepoint);
  607. /**
  608. * Return the two-letter (nul-terminated) Unicode category string for
  609. * the codepoint (e.g. `"Lu"` or `"Co"`).
  610. */
  611. UTF8PROC_DLLEXPORT const char *utf8proc_category_string(utf8proc_int32_t codepoint);
  612. /**
  613. * Maps the given UTF-8 string pointed to by `str` to a new UTF-8
  614. * string, allocated dynamically by `malloc` and returned via `dstptr`.
  615. *
  616. * If the @ref UTF8PROC_NULLTERM flag in the `options` field is set,
  617. * the length is determined by a NULL terminator, otherwise the
  618. * parameter `strlen` is evaluated to determine the string length, but
  619. * in any case the result will be NULL terminated (though it might
  620. * contain NULL characters with the string if `str` contained NULL
  621. * characters). Other flags in the `options` field are passed to the
  622. * functions defined above, and regarded as described. See also
  623. * @ref utf8proc_map_custom to supply a custom codepoint transformation.
  624. *
  625. * In case of success the length of the new string is returned,
  626. * otherwise a negative error code is returned.
  627. *
  628. * @note The memory of the new UTF-8 string will have been allocated
  629. * with `malloc`, and should therefore be deallocated with `free`.
  630. */
  631. UTF8PROC_DLLEXPORT utf8proc_ssize_t utf8proc_map(
  632. const utf8proc_uint8_t *str, utf8proc_ssize_t strlen, utf8proc_uint8_t **dstptr, utf8proc_option_t options
  633. );
  634. /**
  635. * Like @ref utf8proc_map, but also takes a `custom_func` mapping function
  636. * that is called on each codepoint in `str` before any other transformations
  637. * (along with a `custom_data` pointer that is passed through to `custom_func`).
  638. * The `custom_func` argument is ignored if it is `NULL`.
  639. */
  640. UTF8PROC_DLLEXPORT utf8proc_ssize_t utf8proc_map_custom(
  641. const utf8proc_uint8_t *str, utf8proc_ssize_t strlen, utf8proc_uint8_t **dstptr, utf8proc_option_t options,
  642. utf8proc_custom_func custom_func, void *custom_data
  643. );
  644. /** @name Unicode normalization
  645. *
  646. * Returns a pointer to newly allocated memory of a NFD, NFC, NFKD, NFKC or
  647. * NFKC_Casefold normalized version of the null-terminated string `str`. These
  648. * are shortcuts to calling @ref utf8proc_map with @ref UTF8PROC_NULLTERM
  649. * combined with @ref UTF8PROC_STABLE and flags indicating the normalization.
  650. */
  651. /** @{ */
  652. /** NFD normalization (@ref UTF8PROC_DECOMPOSE). */
  653. UTF8PROC_DLLEXPORT utf8proc_uint8_t *utf8proc_NFD(const utf8proc_uint8_t *str);
  654. /** NFC normalization (@ref UTF8PROC_COMPOSE). */
  655. UTF8PROC_DLLEXPORT utf8proc_uint8_t *utf8proc_NFC(const utf8proc_uint8_t *str);
  656. /** NFKD normalization (@ref UTF8PROC_DECOMPOSE and @ref UTF8PROC_COMPAT). */
  657. UTF8PROC_DLLEXPORT utf8proc_uint8_t *utf8proc_NFKD(const utf8proc_uint8_t *str);
  658. /** NFKC normalization (@ref UTF8PROC_COMPOSE and @ref UTF8PROC_COMPAT). */
  659. UTF8PROC_DLLEXPORT utf8proc_uint8_t *utf8proc_NFKC(const utf8proc_uint8_t *str);
  660. /**
  661. * NFKC_Casefold normalization (@ref UTF8PROC_COMPOSE and @ref UTF8PROC_COMPAT
  662. * and @ref UTF8PROC_CASEFOLD and @ref UTF8PROC_IGNORE).
  663. **/
  664. UTF8PROC_DLLEXPORT utf8proc_uint8_t *utf8proc_NFKC_Casefold(const utf8proc_uint8_t *str);
  665. /** @} */
  666. #ifdef __cplusplus
  667. }
  668. #endif
  669. #endif