utf8proc.h 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703
  1. /*
  2. * Copyright (c) 2015 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 Unicode version
  29. * 9.0.0. 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 1
  70. /** The PATCH version (increased for fixes that do not change the API). */
  71. #define UTF8PROC_VERSION_PATCH 1
  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. #ifdef UTF8PROC_STATIC
  116. # define UTF8PROC_DLLEXPORT
  117. #else
  118. # ifdef _WIN32
  119. # ifdef UTF8PROC_EXPORTS
  120. # define UTF8PROC_DLLEXPORT __declspec(dllexport)
  121. # else
  122. # define UTF8PROC_DLLEXPORT __declspec(dllimport)
  123. # endif
  124. # elif __GNUC__ >= 4
  125. # define UTF8PROC_DLLEXPORT __attribute__ ((visibility("default")))
  126. # else
  127. # define UTF8PROC_DLLEXPORT
  128. # endif
  129. #endif
  130. #ifdef __cplusplus
  131. extern "C" {
  132. #endif
  133. #ifndef SSIZE_MAX
  134. #define SSIZE_MAX ((size_t)SIZE_MAX/2)
  135. #endif
  136. #ifndef UINT16_MAX
  137. # define UINT16_MAX 65535U
  138. #endif
  139. /**
  140. * Option flags used by several functions in the library.
  141. */
  142. typedef enum {
  143. /** The given UTF-8 input is NULL terminated. */
  144. UTF8PROC_NULLTERM = (1<<0),
  145. /** Unicode Versioning Stability has to be respected. */
  146. UTF8PROC_STABLE = (1<<1),
  147. /** Compatibility decomposition (i.e. formatting information is lost). */
  148. UTF8PROC_COMPAT = (1<<2),
  149. /** Return a result with decomposed characters. */
  150. UTF8PROC_COMPOSE = (1<<3),
  151. /** Return a result with decomposed characters. */
  152. UTF8PROC_DECOMPOSE = (1<<4),
  153. /** Strip "default ignorable characters" such as SOFT-HYPHEN or ZERO-WIDTH-SPACE. */
  154. UTF8PROC_IGNORE = (1<<5),
  155. /** Return an error, if the input contains unassigned codepoints. */
  156. UTF8PROC_REJECTNA = (1<<6),
  157. /**
  158. * Indicating that NLF-sequences (LF, CRLF, CR, NEL) are representing a
  159. * line break, and should be converted to the codepoint for line
  160. * separation (LS).
  161. */
  162. UTF8PROC_NLF2LS = (1<<7),
  163. /**
  164. * Indicating that NLF-sequences are representing a paragraph break, and
  165. * should be converted to the codepoint for paragraph separation
  166. * (PS).
  167. */
  168. UTF8PROC_NLF2PS = (1<<8),
  169. /** Indicating that the meaning of NLF-sequences is unknown. */
  170. UTF8PROC_NLF2LF = (UTF8PROC_NLF2LS | UTF8PROC_NLF2PS),
  171. /** Strips and/or convers control characters.
  172. *
  173. * NLF-sequences are transformed into space, except if one of the
  174. * NLF2LS/PS/LF options is given. HorizontalTab (HT) and FormFeed (FF)
  175. * are treated as a NLF-sequence in this case. All other control
  176. * characters are simply removed.
  177. */
  178. UTF8PROC_STRIPCC = (1<<9),
  179. /**
  180. * Performs unicode case folding, to be able to do a case-insensitive
  181. * string comparison.
  182. */
  183. UTF8PROC_CASEFOLD = (1<<10),
  184. /**
  185. * Inserts 0xFF bytes at the beginning of each sequence which is
  186. * representing a single grapheme cluster (see UAX#29).
  187. */
  188. UTF8PROC_CHARBOUND = (1<<11),
  189. /** Lumps certain characters together.
  190. *
  191. * E.g. HYPHEN U+2010 and MINUS U+2212 to ASCII "-". See lump.md for details.
  192. *
  193. * If NLF2LF is set, this includes a transformation of paragraph and
  194. * line separators to ASCII line-feed (LF).
  195. */
  196. UTF8PROC_LUMP = (1<<12),
  197. /** Strips all character markings.
  198. *
  199. * This includes non-spacing, spacing and enclosing (i.e. accents).
  200. * @note This option works only with @ref UTF8PROC_COMPOSE or
  201. * @ref UTF8PROC_DECOMPOSE
  202. */
  203. UTF8PROC_STRIPMARK = (1<<13),
  204. } utf8proc_option_t;
  205. /** @name Error codes
  206. * Error codes being returned by almost all functions.
  207. */
  208. /** @{ */
  209. /** Memory could not be allocated. */
  210. #define UTF8PROC_ERROR_NOMEM -1
  211. /** The given string is too long to be processed. */
  212. #define UTF8PROC_ERROR_OVERFLOW -2
  213. /** The given string is not a legal UTF-8 string. */
  214. #define UTF8PROC_ERROR_INVALIDUTF8 -3
  215. /** The @ref UTF8PROC_REJECTNA flag was set and an unassigned codepoint was found. */
  216. #define UTF8PROC_ERROR_NOTASSIGNED -4
  217. /** Invalid options have been used. */
  218. #define UTF8PROC_ERROR_INVALIDOPTS -5
  219. /** @} */
  220. /* @name Types */
  221. /** Holds the value of a property. */
  222. typedef utf8proc_int16_t utf8proc_propval_t;
  223. /** Struct containing information about a codepoint. */
  224. typedef struct utf8proc_property_struct {
  225. /**
  226. * Unicode category.
  227. * @see utf8proc_category_t.
  228. */
  229. utf8proc_propval_t category;
  230. utf8proc_propval_t combining_class;
  231. /**
  232. * Bidirectional class.
  233. * @see utf8proc_bidi_class_t.
  234. */
  235. utf8proc_propval_t bidi_class;
  236. /**
  237. * @anchor Decomposition type.
  238. * @see utf8proc_decomp_type_t.
  239. */
  240. utf8proc_propval_t decomp_type;
  241. utf8proc_uint16_t decomp_seqindex;
  242. utf8proc_uint16_t casefold_seqindex;
  243. utf8proc_uint16_t uppercase_seqindex;
  244. utf8proc_uint16_t lowercase_seqindex;
  245. utf8proc_uint16_t titlecase_seqindex;
  246. utf8proc_uint16_t comb_index;
  247. unsigned bidi_mirrored:1;
  248. unsigned comp_exclusion:1;
  249. /**
  250. * Can this codepoint be ignored?
  251. *
  252. * Used by @ref utf8proc_decompose_char when @ref UTF8PROC_IGNORE is
  253. * passed as an option.
  254. */
  255. unsigned ignorable:1;
  256. unsigned control_boundary:1;
  257. /** The width of the codepoint. */
  258. unsigned charwidth:2;
  259. unsigned pad:2;
  260. /**
  261. * Boundclass.
  262. * @see utf8proc_boundclass_t.
  263. */
  264. unsigned boundclass:8;
  265. } utf8proc_property_t;
  266. /** Unicode categories. */
  267. typedef enum {
  268. UTF8PROC_CATEGORY_CN = 0, /**< Other, not assigned */
  269. UTF8PROC_CATEGORY_LU = 1, /**< Letter, uppercase */
  270. UTF8PROC_CATEGORY_LL = 2, /**< Letter, lowercase */
  271. UTF8PROC_CATEGORY_LT = 3, /**< Letter, titlecase */
  272. UTF8PROC_CATEGORY_LM = 4, /**< Letter, modifier */
  273. UTF8PROC_CATEGORY_LO = 5, /**< Letter, other */
  274. UTF8PROC_CATEGORY_MN = 6, /**< Mark, nonspacing */
  275. UTF8PROC_CATEGORY_MC = 7, /**< Mark, spacing combining */
  276. UTF8PROC_CATEGORY_ME = 8, /**< Mark, enclosing */
  277. UTF8PROC_CATEGORY_ND = 9, /**< Number, decimal digit */
  278. UTF8PROC_CATEGORY_NL = 10, /**< Number, letter */
  279. UTF8PROC_CATEGORY_NO = 11, /**< Number, other */
  280. UTF8PROC_CATEGORY_PC = 12, /**< Punctuation, connector */
  281. UTF8PROC_CATEGORY_PD = 13, /**< Punctuation, dash */
  282. UTF8PROC_CATEGORY_PS = 14, /**< Punctuation, open */
  283. UTF8PROC_CATEGORY_PE = 15, /**< Punctuation, close */
  284. UTF8PROC_CATEGORY_PI = 16, /**< Punctuation, initial quote */
  285. UTF8PROC_CATEGORY_PF = 17, /**< Punctuation, final quote */
  286. UTF8PROC_CATEGORY_PO = 18, /**< Punctuation, other */
  287. UTF8PROC_CATEGORY_SM = 19, /**< Symbol, math */
  288. UTF8PROC_CATEGORY_SC = 20, /**< Symbol, currency */
  289. UTF8PROC_CATEGORY_SK = 21, /**< Symbol, modifier */
  290. UTF8PROC_CATEGORY_SO = 22, /**< Symbol, other */
  291. UTF8PROC_CATEGORY_ZS = 23, /**< Separator, space */
  292. UTF8PROC_CATEGORY_ZL = 24, /**< Separator, line */
  293. UTF8PROC_CATEGORY_ZP = 25, /**< Separator, paragraph */
  294. UTF8PROC_CATEGORY_CC = 26, /**< Other, control */
  295. UTF8PROC_CATEGORY_CF = 27, /**< Other, format */
  296. UTF8PROC_CATEGORY_CS = 28, /**< Other, surrogate */
  297. UTF8PROC_CATEGORY_CO = 29, /**< Other, private use */
  298. } utf8proc_category_t;
  299. /** Bidirectional character classes. */
  300. typedef enum {
  301. UTF8PROC_BIDI_CLASS_L = 1, /**< Left-to-Right */
  302. UTF8PROC_BIDI_CLASS_LRE = 2, /**< Left-to-Right Embedding */
  303. UTF8PROC_BIDI_CLASS_LRO = 3, /**< Left-to-Right Override */
  304. UTF8PROC_BIDI_CLASS_R = 4, /**< Right-to-Left */
  305. UTF8PROC_BIDI_CLASS_AL = 5, /**< Right-to-Left Arabic */
  306. UTF8PROC_BIDI_CLASS_RLE = 6, /**< Right-to-Left Embedding */
  307. UTF8PROC_BIDI_CLASS_RLO = 7, /**< Right-to-Left Override */
  308. UTF8PROC_BIDI_CLASS_PDF = 8, /**< Pop Directional Format */
  309. UTF8PROC_BIDI_CLASS_EN = 9, /**< European Number */
  310. UTF8PROC_BIDI_CLASS_ES = 10, /**< European Separator */
  311. UTF8PROC_BIDI_CLASS_ET = 11, /**< European Number Terminator */
  312. UTF8PROC_BIDI_CLASS_AN = 12, /**< Arabic Number */
  313. UTF8PROC_BIDI_CLASS_CS = 13, /**< Common Number Separator */
  314. UTF8PROC_BIDI_CLASS_NSM = 14, /**< Nonspacing Mark */
  315. UTF8PROC_BIDI_CLASS_BN = 15, /**< Boundary Neutral */
  316. UTF8PROC_BIDI_CLASS_B = 16, /**< Paragraph Separator */
  317. UTF8PROC_BIDI_CLASS_S = 17, /**< Segment Separator */
  318. UTF8PROC_BIDI_CLASS_WS = 18, /**< Whitespace */
  319. UTF8PROC_BIDI_CLASS_ON = 19, /**< Other Neutrals */
  320. UTF8PROC_BIDI_CLASS_LRI = 20, /**< Left-to-Right Isolate */
  321. UTF8PROC_BIDI_CLASS_RLI = 21, /**< Right-to-Left Isolate */
  322. UTF8PROC_BIDI_CLASS_FSI = 22, /**< First Strong Isolate */
  323. UTF8PROC_BIDI_CLASS_PDI = 23, /**< Pop Directional Isolate */
  324. } utf8proc_bidi_class_t;
  325. /** Decomposition type. */
  326. typedef enum {
  327. UTF8PROC_DECOMP_TYPE_FONT = 1, /**< Font */
  328. UTF8PROC_DECOMP_TYPE_NOBREAK = 2, /**< Nobreak */
  329. UTF8PROC_DECOMP_TYPE_INITIAL = 3, /**< Initial */
  330. UTF8PROC_DECOMP_TYPE_MEDIAL = 4, /**< Medial */
  331. UTF8PROC_DECOMP_TYPE_FINAL = 5, /**< Final */
  332. UTF8PROC_DECOMP_TYPE_ISOLATED = 6, /**< Isolated */
  333. UTF8PROC_DECOMP_TYPE_CIRCLE = 7, /**< Circle */
  334. UTF8PROC_DECOMP_TYPE_SUPER = 8, /**< Super */
  335. UTF8PROC_DECOMP_TYPE_SUB = 9, /**< Sub */
  336. UTF8PROC_DECOMP_TYPE_VERTICAL = 10, /**< Vertical */
  337. UTF8PROC_DECOMP_TYPE_WIDE = 11, /**< Wide */
  338. UTF8PROC_DECOMP_TYPE_NARROW = 12, /**< Narrow */
  339. UTF8PROC_DECOMP_TYPE_SMALL = 13, /**< Small */
  340. UTF8PROC_DECOMP_TYPE_SQUARE = 14, /**< Square */
  341. UTF8PROC_DECOMP_TYPE_FRACTION = 15, /**< Fraction */
  342. UTF8PROC_DECOMP_TYPE_COMPAT = 16, /**< Compat */
  343. } utf8proc_decomp_type_t;
  344. /** Boundclass property. (TR29) */
  345. typedef enum {
  346. UTF8PROC_BOUNDCLASS_START = 0, /**< Start */
  347. UTF8PROC_BOUNDCLASS_OTHER = 1, /**< Other */
  348. UTF8PROC_BOUNDCLASS_CR = 2, /**< Cr */
  349. UTF8PROC_BOUNDCLASS_LF = 3, /**< Lf */
  350. UTF8PROC_BOUNDCLASS_CONTROL = 4, /**< Control */
  351. UTF8PROC_BOUNDCLASS_EXTEND = 5, /**< Extend */
  352. UTF8PROC_BOUNDCLASS_L = 6, /**< L */
  353. UTF8PROC_BOUNDCLASS_V = 7, /**< V */
  354. UTF8PROC_BOUNDCLASS_T = 8, /**< T */
  355. UTF8PROC_BOUNDCLASS_LV = 9, /**< Lv */
  356. UTF8PROC_BOUNDCLASS_LVT = 10, /**< Lvt */
  357. UTF8PROC_BOUNDCLASS_REGIONAL_INDICATOR = 11, /**< Regional indicator */
  358. UTF8PROC_BOUNDCLASS_SPACINGMARK = 12, /**< Spacingmark */
  359. UTF8PROC_BOUNDCLASS_PREPEND = 13, /**< Prepend */
  360. UTF8PROC_BOUNDCLASS_ZWJ = 14, /**< Zero Width Joiner */
  361. UTF8PROC_BOUNDCLASS_E_BASE = 15, /**< Emoji Base */
  362. UTF8PROC_BOUNDCLASS_E_MODIFIER = 16, /**< Emoji Modifier */
  363. UTF8PROC_BOUNDCLASS_GLUE_AFTER_ZWJ = 17, /**< Glue_After_ZWJ */
  364. UTF8PROC_BOUNDCLASS_E_BASE_GAZ = 18, /**< E_BASE + GLUE_AFTER_ZJW */
  365. } utf8proc_boundclass_t;
  366. /**
  367. * Function pointer type passed to @ref utf8proc_map_custom and
  368. * @ref utf8proc_decompose_custom, which is used to specify a user-defined
  369. * mapping of codepoints to be applied in conjunction with other mappings.
  370. */
  371. typedef utf8proc_int32_t (*utf8proc_custom_func)(utf8proc_int32_t codepoint, void *data);
  372. /**
  373. * Array containing the byte lengths of a UTF-8 encoded codepoint based
  374. * on the first byte.
  375. */
  376. UTF8PROC_DLLEXPORT extern const utf8proc_int8_t utf8proc_utf8class[256];
  377. /**
  378. * Returns the utf8proc API version as a string MAJOR.MINOR.PATCH
  379. * (http://semver.org format), possibly with a "-dev" suffix for
  380. * development versions.
  381. */
  382. UTF8PROC_DLLEXPORT const char *utf8proc_version(void);
  383. /**
  384. * Returns an informative error string for the given utf8proc error code
  385. * (e.g. the error codes returned by @ref utf8proc_map).
  386. */
  387. UTF8PROC_DLLEXPORT const char *utf8proc_errmsg(utf8proc_ssize_t errcode);
  388. /**
  389. * Reads a single codepoint from the UTF-8 sequence being pointed to by `str`.
  390. * The maximum number of bytes read is `strlen`, unless `strlen` is
  391. * negative (in which case up to 4 bytes are read).
  392. *
  393. * If a valid codepoint could be read, it is stored in the variable
  394. * pointed to by `codepoint_ref`, otherwise that variable will be set to -1.
  395. * In case of success, the number of bytes read is returned; otherwise, a
  396. * negative error code is returned.
  397. */
  398. UTF8PROC_DLLEXPORT utf8proc_ssize_t utf8proc_iterate(const utf8proc_uint8_t *str, utf8proc_ssize_t strlen, utf8proc_int32_t *codepoint_ref);
  399. /**
  400. * Check if a codepoint is valid (regardless of whether it has been
  401. * assigned a value by the current Unicode standard).
  402. *
  403. * @return 1 if the given `codepoint` is valid and otherwise return 0.
  404. */
  405. UTF8PROC_DLLEXPORT utf8proc_bool utf8proc_codepoint_valid(utf8proc_int32_t codepoint);
  406. /**
  407. * Encodes the codepoint as an UTF-8 string in the byte array pointed
  408. * to by `dst`. This array must be at least 4 bytes long.
  409. *
  410. * In case of success the number of bytes written is returned, and
  411. * otherwise 0 is returned.
  412. *
  413. * This function does not check whether `codepoint` is valid Unicode.
  414. */
  415. UTF8PROC_DLLEXPORT utf8proc_ssize_t utf8proc_encode_char(utf8proc_int32_t codepoint, utf8proc_uint8_t *dst);
  416. /**
  417. * Look up the properties for a given codepoint.
  418. *
  419. * @param codepoint The Unicode codepoint.
  420. *
  421. * @returns
  422. * A pointer to a (constant) struct containing information about
  423. * the codepoint.
  424. * @par
  425. * If the codepoint is unassigned or invalid, a pointer to a special struct is
  426. * returned in which `category` is 0 (@ref UTF8PROC_CATEGORY_CN).
  427. */
  428. UTF8PROC_DLLEXPORT const utf8proc_property_t *utf8proc_get_property(utf8proc_int32_t codepoint);
  429. /** Decompose a codepoint into an array of codepoints.
  430. *
  431. * @param codepoint the codepoint.
  432. * @param dst the destination buffer.
  433. * @param bufsize the size of the destination buffer.
  434. * @param options one or more of the following flags:
  435. * - @ref UTF8PROC_REJECTNA - return an error `codepoint` is unassigned
  436. * - @ref UTF8PROC_IGNORE - strip "default ignorable" codepoints
  437. * - @ref UTF8PROC_CASEFOLD - apply Unicode casefolding
  438. * - @ref UTF8PROC_COMPAT - replace certain codepoints with their
  439. * compatibility decomposition
  440. * - @ref UTF8PROC_CHARBOUND - insert 0xFF bytes before each grapheme cluster
  441. * - @ref UTF8PROC_LUMP - lump certain different codepoints together
  442. * - @ref UTF8PROC_STRIPMARK - remove all character marks
  443. * @param last_boundclass
  444. * Pointer to an integer variable containing
  445. * the previous codepoint's boundary class if the @ref UTF8PROC_CHARBOUND
  446. * option is used. Otherwise, this parameter is ignored.
  447. *
  448. * @return
  449. * In case of success, the number of codepoints written is returned; in case
  450. * of an error, a negative error code is returned (@ref utf8proc_errmsg).
  451. * @par
  452. * If the number of written codepoints would be bigger than `bufsize`, the
  453. * required buffer size is returned, while the buffer will be overwritten with
  454. * undefined data.
  455. */
  456. UTF8PROC_DLLEXPORT utf8proc_ssize_t utf8proc_decompose_char(
  457. utf8proc_int32_t codepoint, utf8proc_int32_t *dst, utf8proc_ssize_t bufsize,
  458. utf8proc_option_t options, int *last_boundclass
  459. );
  460. /**
  461. * The same as @ref utf8proc_decompose_char, but acts on a whole UTF-8
  462. * string and orders the decomposed sequences correctly.
  463. *
  464. * If the @ref UTF8PROC_NULLTERM flag in `options` is set, processing
  465. * will be stopped, when a NULL byte is encounted, otherwise `strlen`
  466. * bytes are processed. The result (in the form of 32-bit unicode
  467. * codepoints) is written into the buffer being pointed to by
  468. * `buffer` (which must contain at least `bufsize` entries). In case of
  469. * success, the number of codepoints written is returned; in case of an
  470. * error, a negative error code is returned (@ref utf8proc_errmsg).
  471. * See @ref utf8proc_decompose_custom to supply additional transformations.
  472. *
  473. * If the number of written codepoints would be bigger than `bufsize`, the
  474. * required buffer size is returned, while the buffer will be overwritten with
  475. * undefined data.
  476. */
  477. UTF8PROC_DLLEXPORT utf8proc_ssize_t utf8proc_decompose(
  478. const utf8proc_uint8_t *str, utf8proc_ssize_t strlen,
  479. utf8proc_int32_t *buffer, utf8proc_ssize_t bufsize, utf8proc_option_t options
  480. );
  481. /**
  482. * The same as @ref utf8proc_decompose, but also takes a `custom_func` mapping function
  483. * that is called on each codepoint in `str` before any other transformations
  484. * (along with a `custom_data` pointer that is passed through to `custom_func`).
  485. * The `custom_func` argument is ignored if it is `NULL`. See also @ref utf8proc_map_custom.
  486. */
  487. UTF8PROC_DLLEXPORT utf8proc_ssize_t utf8proc_decompose_custom(
  488. const utf8proc_uint8_t *str, utf8proc_ssize_t strlen,
  489. utf8proc_int32_t *buffer, utf8proc_ssize_t bufsize, utf8proc_option_t options,
  490. utf8proc_custom_func custom_func, void *custom_data
  491. );
  492. /**
  493. * Normalizes the sequence of `length` codepoints pointed to by `buffer`
  494. * in-place (i.e., the result is also stored in `buffer`).
  495. *
  496. * @param buffer the (native-endian UTF-32) unicode codepoints to re-encode.
  497. * @param length the length (in codepoints) of the buffer.
  498. * @param options a bitwise or (`|`) of one or more of the following flags:
  499. * - @ref UTF8PROC_NLF2LS - convert LF, CRLF, CR and NEL into LS
  500. * - @ref UTF8PROC_NLF2PS - convert LF, CRLF, CR and NEL into PS
  501. * - @ref UTF8PROC_NLF2LF - convert LF, CRLF, CR and NEL into LF
  502. * - @ref UTF8PROC_STRIPCC - strip or convert all non-affected control characters
  503. * - @ref UTF8PROC_COMPOSE - try to combine decomposed codepoints into composite
  504. * codepoints
  505. * - @ref UTF8PROC_STABLE - prohibit combining characters that would violate
  506. * the unicode versioning stability
  507. *
  508. * @return
  509. * In case of success, the length (in codepoints) of the normalized UTF-32 string is
  510. * returned; otherwise, a negative error code is returned (@ref utf8proc_errmsg).
  511. *
  512. * @warning The entries of the array pointed to by `str` have to be in the
  513. * range `0x0000` to `0x10FFFF`. Otherwise, the program might crash!
  514. */
  515. UTF8PROC_DLLEXPORT utf8proc_ssize_t utf8proc_normalize_utf32(utf8proc_int32_t *buffer, utf8proc_ssize_t length, utf8proc_option_t options);
  516. /**
  517. * Reencodes the sequence of `length` codepoints pointed to by `buffer`
  518. * UTF-8 data in-place (i.e., the result is also stored in `buffer`).
  519. * Can optionally normalize the UTF-32 sequence prior to UTF-8 conversion.
  520. *
  521. * @param buffer the (native-endian UTF-32) unicode codepoints to re-encode.
  522. * @param length the length (in codepoints) of the buffer.
  523. * @param options a bitwise or (`|`) of one or more of the following flags:
  524. * - @ref UTF8PROC_NLF2LS - convert LF, CRLF, CR and NEL into LS
  525. * - @ref UTF8PROC_NLF2PS - convert LF, CRLF, CR and NEL into PS
  526. * - @ref UTF8PROC_NLF2LF - convert LF, CRLF, CR and NEL into LF
  527. * - @ref UTF8PROC_STRIPCC - strip or convert all non-affected control characters
  528. * - @ref UTF8PROC_COMPOSE - try to combine decomposed codepoints into composite
  529. * codepoints
  530. * - @ref UTF8PROC_STABLE - prohibit combining characters that would violate
  531. * the unicode versioning stability
  532. * - @ref UTF8PROC_CHARBOUND - insert 0xFF bytes before each grapheme cluster
  533. *
  534. * @return
  535. * In case of success, the length (in bytes) of the resulting nul-terminated
  536. * UTF-8 string is returned; otherwise, a negative error code is returned
  537. * (@ref utf8proc_errmsg).
  538. *
  539. * @warning The amount of free space pointed to by `buffer` must
  540. * exceed the amount of the input data by one byte, and the
  541. * entries of the array pointed to by `str` have to be in the
  542. * range `0x0000` to `0x10FFFF`. Otherwise, the program might crash!
  543. */
  544. UTF8PROC_DLLEXPORT utf8proc_ssize_t utf8proc_reencode(utf8proc_int32_t *buffer, utf8proc_ssize_t length, utf8proc_option_t options);
  545. /**
  546. * Given a pair of consecutive codepoints, return whether a grapheme break is
  547. * permitted between them (as defined by the extended grapheme clusters in UAX#29).
  548. *
  549. * @param state Beginning with Version 29 (Unicode 9.0.0), this algorithm requires
  550. * state to break graphemes. This state can be passed in as a pointer
  551. * in the `state` argument and should initially be set to 0. If the
  552. * state is not passed in (i.e. a null pointer is passed), UAX#29 rules
  553. * GB10/12/13 which require this state will not be applied, essentially
  554. * matching the rules in Unicode 8.0.0.
  555. *
  556. * @warning If the state parameter is used, `utf8proc_grapheme_break_stateful` must
  557. * be called IN ORDER on ALL potential breaks in a string.
  558. */
  559. UTF8PROC_DLLEXPORT utf8proc_bool utf8proc_grapheme_break_stateful(
  560. utf8proc_int32_t codepoint1, utf8proc_int32_t codepoint2, utf8proc_int32_t *state);
  561. /**
  562. * Same as @ref utf8proc_grapheme_break_stateful, except without support for the
  563. * Unicode 9 additions to the algorithm. Supported for legacy reasons.
  564. */
  565. UTF8PROC_DLLEXPORT utf8proc_bool utf8proc_grapheme_break(
  566. utf8proc_int32_t codepoint1, utf8proc_int32_t codepoint2);
  567. /**
  568. * Given a codepoint `c`, return the codepoint of the corresponding
  569. * lower-case character, if any; otherwise (if there is no lower-case
  570. * variant, or if `c` is not a valid codepoint) return `c`.
  571. */
  572. UTF8PROC_DLLEXPORT utf8proc_int32_t utf8proc_tolower(utf8proc_int32_t c);
  573. /**
  574. * Given a codepoint `c`, return the codepoint of the corresponding
  575. * upper-case character, if any; otherwise (if there is no upper-case
  576. * variant, or if `c` is not a valid codepoint) return `c`.
  577. */
  578. UTF8PROC_DLLEXPORT utf8proc_int32_t utf8proc_toupper(utf8proc_int32_t c);
  579. /**
  580. * Given a codepoint `c`, return the codepoint of the corresponding
  581. * title-case character, if any; otherwise (if there is no title-case
  582. * variant, or if `c` is not a valid codepoint) return `c`.
  583. */
  584. UTF8PROC_DLLEXPORT utf8proc_int32_t utf8proc_totitle(utf8proc_int32_t c);
  585. /**
  586. * Given a codepoint, return a character width analogous to `wcwidth(codepoint)`,
  587. * except that a width of 0 is returned for non-printable codepoints
  588. * instead of -1 as in `wcwidth`.
  589. *
  590. * @note
  591. * If you want to check for particular types of non-printable characters,
  592. * (analogous to `isprint` or `iscntrl`), use @ref utf8proc_category. */
  593. UTF8PROC_DLLEXPORT int utf8proc_charwidth(utf8proc_int32_t codepoint);
  594. /**
  595. * Return the Unicode category for the codepoint (one of the
  596. * @ref utf8proc_category_t constants.)
  597. */
  598. UTF8PROC_DLLEXPORT utf8proc_category_t utf8proc_category(utf8proc_int32_t codepoint);
  599. /**
  600. * Return the two-letter (nul-terminated) Unicode category string for
  601. * the codepoint (e.g. `"Lu"` or `"Co"`).
  602. */
  603. UTF8PROC_DLLEXPORT const char *utf8proc_category_string(utf8proc_int32_t codepoint);
  604. /**
  605. * Maps the given UTF-8 string pointed to by `str` to a new UTF-8
  606. * string, allocated dynamically by `malloc` and returned via `dstptr`.
  607. *
  608. * If the @ref UTF8PROC_NULLTERM flag in the `options` field is set,
  609. * the length is determined by a NULL terminator, otherwise the
  610. * parameter `strlen` is evaluated to determine the string length, but
  611. * in any case the result will be NULL terminated (though it might
  612. * contain NULL characters with the string if `str` contained NULL
  613. * characters). Other flags in the `options` field are passed to the
  614. * functions defined above, and regarded as described. See also
  615. * @ref utfproc_map_custom to supply a custom codepoint transformation.
  616. *
  617. * In case of success the length of the new string is returned,
  618. * otherwise a negative error code is returned.
  619. *
  620. * @note The memory of the new UTF-8 string will have been allocated
  621. * with `malloc`, and should therefore be deallocated with `free`.
  622. */
  623. UTF8PROC_DLLEXPORT utf8proc_ssize_t utf8proc_map(
  624. const utf8proc_uint8_t *str, utf8proc_ssize_t strlen, utf8proc_uint8_t **dstptr, utf8proc_option_t options
  625. );
  626. /**
  627. * Like @ref utf8proc_map, but also takes a `custom_func` mapping function
  628. * that is called on each codepoint in `str` before any other transformations
  629. * (along with a `custom_data` pointer that is passed through to `custom_func`).
  630. * The `custom_func` argument is ignored if it is `NULL`.
  631. */
  632. UTF8PROC_DLLEXPORT utf8proc_ssize_t utf8proc_map_custom(
  633. const utf8proc_uint8_t *str, utf8proc_ssize_t strlen, utf8proc_uint8_t **dstptr, utf8proc_option_t options,
  634. utf8proc_custom_func custom_func, void *custom_data
  635. );
  636. /** @name Unicode normalization
  637. *
  638. * Returns a pointer to newly allocated memory of a NFD, NFC, NFKD or NFKC
  639. * normalized version of the null-terminated string `str`. These
  640. * are shortcuts to calling @ref utf8proc_map with @ref UTF8PROC_NULLTERM
  641. * combined with @ref UTF8PROC_STABLE and flags indicating the normalization.
  642. */
  643. /** @{ */
  644. /** NFD normalization (@ref UTF8PROC_DECOMPOSE). */
  645. UTF8PROC_DLLEXPORT utf8proc_uint8_t *utf8proc_NFD(const utf8proc_uint8_t *str);
  646. /** NFC normalization (@ref UTF8PROC_COMPOSE). */
  647. UTF8PROC_DLLEXPORT utf8proc_uint8_t *utf8proc_NFC(const utf8proc_uint8_t *str);
  648. /** NFKD normalization (@ref UTF8PROC_DECOMPOSE and @ref UTF8PROC_COMPAT). */
  649. UTF8PROC_DLLEXPORT utf8proc_uint8_t *utf8proc_NFKD(const utf8proc_uint8_t *str);
  650. /** NFKC normalization (@ref UTF8PROC_COMPOSE and @ref UTF8PROC_COMPAT). */
  651. UTF8PROC_DLLEXPORT utf8proc_uint8_t *utf8proc_NFKC(const utf8proc_uint8_t *str);
  652. /** @} */
  653. #ifdef __cplusplus
  654. }
  655. #endif
  656. #endif