utf8proc.h 32 KB

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