utf8proc.c 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823
  1. /* -*- mode: c; c-basic-offset: 2; tab-width: 2; indent-tabs-mode: nil -*- */
  2. /*
  3. * Copyright (c) 2014-2021 Steven G. Johnson, Jiahao Chen, Peter Colberg, Tony Kelman, Scott P. Jones, and other contributors.
  4. * Copyright (c) 2009 Public Software Group e. V., Berlin, Germany
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a
  7. * copy of this software and associated documentation files (the "Software"),
  8. * to deal in the Software without restriction, including without limitation
  9. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  10. * and/or sell copies of the Software, and to permit persons to whom the
  11. * Software is furnished to do so, subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be included in
  14. * all copies or substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  19. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  21. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  22. * DEALINGS IN THE SOFTWARE.
  23. */
  24. /*
  25. * This library contains derived data from a modified version of the
  26. * Unicode data files.
  27. *
  28. * The original data files are available at
  29. * https://www.unicode.org/Public/UNIDATA/
  30. *
  31. * Please notice the copyright statement in the file "utf8proc_data.c".
  32. */
  33. /*
  34. * File name: utf8proc.c
  35. *
  36. * Description:
  37. * Implementation of libutf8proc.
  38. */
  39. #include "utf8proc.h"
  40. #ifndef SSIZE_MAX
  41. #define SSIZE_MAX ((size_t)SIZE_MAX/2)
  42. #endif
  43. #ifndef UINT16_MAX
  44. # define UINT16_MAX 65535U
  45. #endif
  46. #include "utf8proc_data.c"
  47. UTF8PROC_DLLEXPORT const utf8proc_int8_t utf8proc_utf8class[256] = {
  48. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  49. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  50. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  51. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  52. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  53. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  54. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  55. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  56. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  57. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  58. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  59. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  60. 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  61. 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  62. 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
  63. 4, 4, 4, 4, 4, 4, 4, 4, 0, 0, 0, 0, 0, 0, 0, 0 };
  64. #define UTF8PROC_HANGUL_SBASE 0xAC00
  65. #define UTF8PROC_HANGUL_LBASE 0x1100
  66. #define UTF8PROC_HANGUL_VBASE 0x1161
  67. #define UTF8PROC_HANGUL_TBASE 0x11A7
  68. #define UTF8PROC_HANGUL_LCOUNT 19
  69. #define UTF8PROC_HANGUL_VCOUNT 21
  70. #define UTF8PROC_HANGUL_TCOUNT 28
  71. #define UTF8PROC_HANGUL_NCOUNT 588
  72. #define UTF8PROC_HANGUL_SCOUNT 11172
  73. /* END is exclusive */
  74. #define UTF8PROC_HANGUL_L_START 0x1100
  75. #define UTF8PROC_HANGUL_L_END 0x115A
  76. #define UTF8PROC_HANGUL_L_FILLER 0x115F
  77. #define UTF8PROC_HANGUL_V_START 0x1160
  78. #define UTF8PROC_HANGUL_V_END 0x11A3
  79. #define UTF8PROC_HANGUL_T_START 0x11A8
  80. #define UTF8PROC_HANGUL_T_END 0x11FA
  81. #define UTF8PROC_HANGUL_S_START 0xAC00
  82. #define UTF8PROC_HANGUL_S_END 0xD7A4
  83. /* Should follow semantic-versioning rules (semver.org) based on API
  84. compatibility. (Note that the shared-library version number will
  85. be different, being based on ABI compatibility.): */
  86. #define STRINGIZEx(x) #x
  87. #define STRINGIZE(x) STRINGIZEx(x)
  88. UTF8PROC_DLLEXPORT const char *utf8proc_version(void) {
  89. return STRINGIZE(UTF8PROC_VERSION_MAJOR) "." STRINGIZE(UTF8PROC_VERSION_MINOR) "." STRINGIZE(UTF8PROC_VERSION_PATCH) "";
  90. }
  91. UTF8PROC_DLLEXPORT const char *utf8proc_unicode_version(void) {
  92. return "16.0.0";
  93. }
  94. UTF8PROC_DLLEXPORT const char *utf8proc_errmsg(utf8proc_ssize_t errcode) {
  95. switch (errcode) {
  96. case UTF8PROC_ERROR_NOMEM:
  97. return "Memory for processing UTF-8 data could not be allocated.";
  98. case UTF8PROC_ERROR_OVERFLOW:
  99. return "UTF-8 string is too long to be processed.";
  100. case UTF8PROC_ERROR_INVALIDUTF8:
  101. return "Invalid UTF-8 string";
  102. case UTF8PROC_ERROR_NOTASSIGNED:
  103. return "Unassigned Unicode code point found in UTF-8 string.";
  104. case UTF8PROC_ERROR_INVALIDOPTS:
  105. return "Invalid options for UTF-8 processing chosen.";
  106. default:
  107. return "An unknown error occurred while processing UTF-8 data.";
  108. }
  109. }
  110. #define utf_cont(ch) (((ch) & 0xc0) == 0x80)
  111. UTF8PROC_DLLEXPORT utf8proc_ssize_t utf8proc_iterate(
  112. const utf8proc_uint8_t *str, utf8proc_ssize_t strlen, utf8proc_int32_t *dst
  113. ) {
  114. utf8proc_int32_t uc;
  115. const utf8proc_uint8_t *end;
  116. *dst = -1;
  117. if (!strlen) return 0;
  118. end = str + ((strlen < 0) ? 4 : strlen);
  119. uc = *str++;
  120. if (uc < 0x80) {
  121. *dst = uc;
  122. return 1;
  123. }
  124. // Must be between 0xc2 and 0xf4 inclusive to be valid
  125. if ((utf8proc_uint32_t)(uc - 0xc2) > (0xf4-0xc2)) return UTF8PROC_ERROR_INVALIDUTF8;
  126. if (uc < 0xe0) { // 2-byte sequence
  127. // Must have valid continuation character
  128. if (str >= end || !utf_cont(*str)) return UTF8PROC_ERROR_INVALIDUTF8;
  129. *dst = ((uc & 0x1f)<<6) | (*str & 0x3f);
  130. return 2;
  131. }
  132. if (uc < 0xf0) { // 3-byte sequence
  133. if ((str + 1 >= end) || !utf_cont(*str) || !utf_cont(str[1]))
  134. return UTF8PROC_ERROR_INVALIDUTF8;
  135. // Check for surrogate chars
  136. if (uc == 0xed && *str > 0x9f)
  137. return UTF8PROC_ERROR_INVALIDUTF8;
  138. uc = ((uc & 0xf)<<12) | ((*str & 0x3f)<<6) | (str[1] & 0x3f);
  139. if (uc < 0x800)
  140. return UTF8PROC_ERROR_INVALIDUTF8;
  141. *dst = uc;
  142. return 3;
  143. }
  144. // 4-byte sequence
  145. // Must have 3 valid continuation characters
  146. if ((str + 2 >= end) || !utf_cont(*str) || !utf_cont(str[1]) || !utf_cont(str[2]))
  147. return UTF8PROC_ERROR_INVALIDUTF8;
  148. // Make sure in correct range (0x10000 - 0x10ffff)
  149. if (uc == 0xf0) {
  150. if (*str < 0x90) return UTF8PROC_ERROR_INVALIDUTF8;
  151. } else if (uc == 0xf4) {
  152. if (*str > 0x8f) return UTF8PROC_ERROR_INVALIDUTF8;
  153. }
  154. *dst = ((uc & 7)<<18) | ((*str & 0x3f)<<12) | ((str[1] & 0x3f)<<6) | (str[2] & 0x3f);
  155. return 4;
  156. }
  157. UTF8PROC_DLLEXPORT utf8proc_bool utf8proc_codepoint_valid(utf8proc_int32_t uc) {
  158. return (((utf8proc_uint32_t)uc)-0xd800 > 0x07ff) && ((utf8proc_uint32_t)uc < 0x110000);
  159. }
  160. UTF8PROC_DLLEXPORT utf8proc_ssize_t utf8proc_encode_char(utf8proc_int32_t uc, utf8proc_uint8_t *dst) {
  161. if (uc < 0x00) {
  162. return 0;
  163. } else if (uc < 0x80) {
  164. dst[0] = (utf8proc_uint8_t) uc;
  165. return 1;
  166. } else if (uc < 0x800) {
  167. dst[0] = (utf8proc_uint8_t)(0xC0 + (uc >> 6));
  168. dst[1] = (utf8proc_uint8_t)(0x80 + (uc & 0x3F));
  169. return 2;
  170. // Note: we allow encoding 0xd800-0xdfff here, so as not to change
  171. // the API, however, these are actually invalid in UTF-8
  172. } else if (uc < 0x10000) {
  173. dst[0] = (utf8proc_uint8_t)(0xE0 + (uc >> 12));
  174. dst[1] = (utf8proc_uint8_t)(0x80 + ((uc >> 6) & 0x3F));
  175. dst[2] = (utf8proc_uint8_t)(0x80 + (uc & 0x3F));
  176. return 3;
  177. } else if (uc < 0x110000) {
  178. dst[0] = (utf8proc_uint8_t)(0xF0 + (uc >> 18));
  179. dst[1] = (utf8proc_uint8_t)(0x80 + ((uc >> 12) & 0x3F));
  180. dst[2] = (utf8proc_uint8_t)(0x80 + ((uc >> 6) & 0x3F));
  181. dst[3] = (utf8proc_uint8_t)(0x80 + (uc & 0x3F));
  182. return 4;
  183. } else return 0;
  184. }
  185. /* internal version used for inserting 0xff bytes between graphemes */
  186. static utf8proc_ssize_t charbound_encode_char(utf8proc_int32_t uc, utf8proc_uint8_t *dst) {
  187. if (uc < 0x00) {
  188. if (uc == -1) { /* internal value used for grapheme breaks */
  189. dst[0] = (utf8proc_uint8_t)0xFF;
  190. return 1;
  191. }
  192. return 0;
  193. } else if (uc < 0x80) {
  194. dst[0] = (utf8proc_uint8_t)uc;
  195. return 1;
  196. } else if (uc < 0x800) {
  197. dst[0] = (utf8proc_uint8_t)(0xC0 + (uc >> 6));
  198. dst[1] = (utf8proc_uint8_t)(0x80 + (uc & 0x3F));
  199. return 2;
  200. } else if (uc < 0x10000) {
  201. dst[0] = (utf8proc_uint8_t)(0xE0 + (uc >> 12));
  202. dst[1] = (utf8proc_uint8_t)(0x80 + ((uc >> 6) & 0x3F));
  203. dst[2] = (utf8proc_uint8_t)(0x80 + (uc & 0x3F));
  204. return 3;
  205. } else if (uc < 0x110000) {
  206. dst[0] = (utf8proc_uint8_t)(0xF0 + (uc >> 18));
  207. dst[1] = (utf8proc_uint8_t)(0x80 + ((uc >> 12) & 0x3F));
  208. dst[2] = (utf8proc_uint8_t)(0x80 + ((uc >> 6) & 0x3F));
  209. dst[3] = (utf8proc_uint8_t)(0x80 + (uc & 0x3F));
  210. return 4;
  211. } else return 0;
  212. }
  213. /* internal "unsafe" version that does not check whether uc is in range */
  214. static const utf8proc_property_t *unsafe_get_property(utf8proc_int32_t uc) {
  215. /* ASSERT: uc >= 0 && uc < 0x110000 */
  216. return utf8proc_properties + (
  217. utf8proc_stage2table[
  218. utf8proc_stage1table[uc >> 8] + (uc & 0xFF)
  219. ]
  220. );
  221. }
  222. UTF8PROC_DLLEXPORT const utf8proc_property_t *utf8proc_get_property(utf8proc_int32_t uc) {
  223. return uc < 0 || uc >= 0x110000 ? utf8proc_properties : unsafe_get_property(uc);
  224. }
  225. /* return whether there is a grapheme break between boundclasses lbc and tbc
  226. (according to the definition of extended grapheme clusters)
  227. Rule numbering refers to TR29 Version 29 (Unicode 9.0.0):
  228. http://www.unicode.org/reports/tr29/tr29-29.html
  229. CAVEATS:
  230. Please note that evaluation of GB10 (grapheme breaks between emoji zwj sequences)
  231. and GB 12/13 (regional indicator code points) require knowledge of previous characters
  232. and are thus not handled by this function. This may result in an incorrect break before
  233. an E_Modifier class codepoint and an incorrectly missing break between two
  234. REGIONAL_INDICATOR class code points if such support does not exist in the caller.
  235. See the special support in grapheme_break_extended, for required bookkeeping by the caller.
  236. */
  237. static utf8proc_bool grapheme_break_simple(int lbc, int tbc) {
  238. return
  239. (lbc == UTF8PROC_BOUNDCLASS_START) ? true : // GB1
  240. (lbc == UTF8PROC_BOUNDCLASS_CR && // GB3
  241. tbc == UTF8PROC_BOUNDCLASS_LF) ? false : // ---
  242. (lbc >= UTF8PROC_BOUNDCLASS_CR && lbc <= UTF8PROC_BOUNDCLASS_CONTROL) ? true : // GB4
  243. (tbc >= UTF8PROC_BOUNDCLASS_CR && tbc <= UTF8PROC_BOUNDCLASS_CONTROL) ? true : // GB5
  244. (lbc == UTF8PROC_BOUNDCLASS_L && // GB6
  245. (tbc == UTF8PROC_BOUNDCLASS_L || // ---
  246. tbc == UTF8PROC_BOUNDCLASS_V || // ---
  247. tbc == UTF8PROC_BOUNDCLASS_LV || // ---
  248. tbc == UTF8PROC_BOUNDCLASS_LVT)) ? false : // ---
  249. ((lbc == UTF8PROC_BOUNDCLASS_LV || // GB7
  250. lbc == UTF8PROC_BOUNDCLASS_V) && // ---
  251. (tbc == UTF8PROC_BOUNDCLASS_V || // ---
  252. tbc == UTF8PROC_BOUNDCLASS_T)) ? false : // ---
  253. ((lbc == UTF8PROC_BOUNDCLASS_LVT || // GB8
  254. lbc == UTF8PROC_BOUNDCLASS_T) && // ---
  255. tbc == UTF8PROC_BOUNDCLASS_T) ? false : // ---
  256. (tbc == UTF8PROC_BOUNDCLASS_EXTEND || // GB9
  257. tbc == UTF8PROC_BOUNDCLASS_ZWJ || // ---
  258. tbc == UTF8PROC_BOUNDCLASS_SPACINGMARK || // GB9a
  259. lbc == UTF8PROC_BOUNDCLASS_PREPEND) ? false : // GB9b
  260. (lbc == UTF8PROC_BOUNDCLASS_E_ZWG && // GB11 (requires additional handling below)
  261. tbc == UTF8PROC_BOUNDCLASS_EXTENDED_PICTOGRAPHIC) ? false : // ----
  262. (lbc == UTF8PROC_BOUNDCLASS_REGIONAL_INDICATOR && // GB12/13 (requires additional handling below)
  263. tbc == UTF8PROC_BOUNDCLASS_REGIONAL_INDICATOR) ? false : // ----
  264. true; // GB999
  265. }
  266. static utf8proc_bool grapheme_break_extended(int lbc, int tbc, int licb, int ticb, utf8proc_int32_t *state)
  267. {
  268. if (state) {
  269. int state_bc, state_icb; /* boundclass and indic_conjunct_break state */
  270. if (*state == 0) { /* state initialization */
  271. state_bc = lbc;
  272. state_icb = licb == UTF8PROC_INDIC_CONJUNCT_BREAK_CONSONANT ? licb : UTF8PROC_INDIC_CONJUNCT_BREAK_NONE;
  273. }
  274. else { /* lbc and licb are already encoded in *state */
  275. state_bc = *state & 0xff; // 1st byte of state is bound class
  276. state_icb = *state >> 8; // 2nd byte of state is indic conjunct break
  277. }
  278. utf8proc_bool break_permitted = grapheme_break_simple(state_bc, tbc) &&
  279. !(state_icb == UTF8PROC_INDIC_CONJUNCT_BREAK_LINKER
  280. && ticb == UTF8PROC_INDIC_CONJUNCT_BREAK_CONSONANT); // GB9c
  281. // Special support for GB9c. Don't break between two consonants
  282. // separated 1+ linker characters and 0+ extend characters in any order.
  283. // After a consonant, we enter LINKER state after at least one linker.
  284. if (ticb == UTF8PROC_INDIC_CONJUNCT_BREAK_CONSONANT
  285. || state_icb == UTF8PROC_INDIC_CONJUNCT_BREAK_CONSONANT
  286. || state_icb == UTF8PROC_INDIC_CONJUNCT_BREAK_EXTEND)
  287. state_icb = ticb;
  288. else if (state_icb == UTF8PROC_INDIC_CONJUNCT_BREAK_LINKER)
  289. state_icb = ticb == UTF8PROC_INDIC_CONJUNCT_BREAK_EXTEND ?
  290. UTF8PROC_INDIC_CONJUNCT_BREAK_LINKER : ticb;
  291. // Special support for GB 12/13 made possible by GB999. After two RI
  292. // class codepoints we want to force a break. Do this by resetting the
  293. // second RI's bound class to UTF8PROC_BOUNDCLASS_OTHER, to force a break
  294. // after that character according to GB999 (unless of course such a break is
  295. // forbidden by a different rule such as GB9).
  296. if (state_bc == tbc && tbc == UTF8PROC_BOUNDCLASS_REGIONAL_INDICATOR)
  297. state_bc = UTF8PROC_BOUNDCLASS_OTHER;
  298. // Special support for GB11 (emoji extend* zwj / emoji)
  299. else if (state_bc == UTF8PROC_BOUNDCLASS_EXTENDED_PICTOGRAPHIC) {
  300. if (tbc == UTF8PROC_BOUNDCLASS_EXTEND) // fold EXTEND codepoints into emoji
  301. state_bc = UTF8PROC_BOUNDCLASS_EXTENDED_PICTOGRAPHIC;
  302. else if (tbc == UTF8PROC_BOUNDCLASS_ZWJ)
  303. state_bc = UTF8PROC_BOUNDCLASS_E_ZWG; // state to record emoji+zwg combo
  304. else
  305. state_bc = tbc;
  306. }
  307. else
  308. state_bc = tbc;
  309. *state = state_bc + (state_icb << 8);
  310. return break_permitted;
  311. }
  312. else
  313. return grapheme_break_simple(lbc, tbc);
  314. }
  315. UTF8PROC_DLLEXPORT utf8proc_bool utf8proc_grapheme_break_stateful(
  316. utf8proc_int32_t c1, utf8proc_int32_t c2, utf8proc_int32_t *state) {
  317. const utf8proc_property_t *p1 = utf8proc_get_property(c1);
  318. const utf8proc_property_t *p2 = utf8proc_get_property(c2);
  319. return grapheme_break_extended(p1->boundclass,
  320. p2->boundclass,
  321. p1->indic_conjunct_break,
  322. p2->indic_conjunct_break,
  323. state);
  324. }
  325. UTF8PROC_DLLEXPORT utf8proc_bool utf8proc_grapheme_break(
  326. utf8proc_int32_t c1, utf8proc_int32_t c2) {
  327. return utf8proc_grapheme_break_stateful(c1, c2, NULL);
  328. }
  329. static utf8proc_int32_t seqindex_decode_entry(const utf8proc_uint16_t **entry)
  330. {
  331. utf8proc_int32_t entry_cp = **entry;
  332. if ((entry_cp & 0xF800) == 0xD800) {
  333. *entry = *entry + 1;
  334. entry_cp = ((entry_cp & 0x03FF) << 10) | (**entry & 0x03FF);
  335. entry_cp += 0x10000;
  336. }
  337. return entry_cp;
  338. }
  339. static utf8proc_int32_t seqindex_decode_index(const utf8proc_uint32_t seqindex)
  340. {
  341. const utf8proc_uint16_t *entry = &utf8proc_sequences[seqindex];
  342. return seqindex_decode_entry(&entry);
  343. }
  344. static utf8proc_ssize_t seqindex_write_char_decomposed(utf8proc_uint16_t seqindex, utf8proc_int32_t *dst, utf8proc_ssize_t bufsize, utf8proc_option_t options, int *last_boundclass) {
  345. utf8proc_ssize_t written = 0;
  346. const utf8proc_uint16_t *entry = &utf8proc_sequences[seqindex & 0x3FFF];
  347. int len = seqindex >> 14;
  348. if (len >= 3) {
  349. len = *entry;
  350. entry++;
  351. }
  352. for (; len >= 0; entry++, len--) {
  353. utf8proc_int32_t entry_cp = seqindex_decode_entry(&entry);
  354. written += utf8proc_decompose_char(entry_cp, dst+written,
  355. (bufsize > written) ? (bufsize - written) : 0, options,
  356. last_boundclass);
  357. if (written < 0) return UTF8PROC_ERROR_OVERFLOW;
  358. }
  359. return written;
  360. }
  361. UTF8PROC_DLLEXPORT utf8proc_int32_t utf8proc_tolower(utf8proc_int32_t c)
  362. {
  363. utf8proc_int32_t cl = utf8proc_get_property(c)->lowercase_seqindex;
  364. return cl != UINT16_MAX ? seqindex_decode_index((utf8proc_uint32_t)cl) : c;
  365. }
  366. UTF8PROC_DLLEXPORT utf8proc_int32_t utf8proc_toupper(utf8proc_int32_t c)
  367. {
  368. utf8proc_int32_t cu = utf8proc_get_property(c)->uppercase_seqindex;
  369. return cu != UINT16_MAX ? seqindex_decode_index((utf8proc_uint32_t)cu) : c;
  370. }
  371. UTF8PROC_DLLEXPORT utf8proc_int32_t utf8proc_totitle(utf8proc_int32_t c)
  372. {
  373. utf8proc_int32_t cu = utf8proc_get_property(c)->titlecase_seqindex;
  374. return cu != UINT16_MAX ? seqindex_decode_index((utf8proc_uint32_t)cu) : c;
  375. }
  376. UTF8PROC_DLLEXPORT int utf8proc_islower(utf8proc_int32_t c)
  377. {
  378. const utf8proc_property_t *p = utf8proc_get_property(c);
  379. return p->lowercase_seqindex != p->uppercase_seqindex && p->lowercase_seqindex == UINT16_MAX;
  380. }
  381. UTF8PROC_DLLEXPORT int utf8proc_isupper(utf8proc_int32_t c)
  382. {
  383. const utf8proc_property_t *p = utf8proc_get_property(c);
  384. return p->lowercase_seqindex != p->uppercase_seqindex && p->uppercase_seqindex == UINT16_MAX && p->category != UTF8PROC_CATEGORY_LT;
  385. }
  386. /* return a character width analogous to wcwidth (except portable and
  387. hopefully less buggy than most system wcwidth functions). */
  388. UTF8PROC_DLLEXPORT int utf8proc_charwidth(utf8proc_int32_t c) {
  389. return utf8proc_get_property(c)->charwidth;
  390. }
  391. UTF8PROC_DLLEXPORT utf8proc_bool utf8proc_charwidth_ambiguous(utf8proc_int32_t c) {
  392. return utf8proc_get_property(c)->ambiguous_width;
  393. }
  394. UTF8PROC_DLLEXPORT utf8proc_category_t utf8proc_category(utf8proc_int32_t c) {
  395. return (utf8proc_category_t) utf8proc_get_property(c)->category;
  396. }
  397. UTF8PROC_DLLEXPORT const char *utf8proc_category_string(utf8proc_int32_t c) {
  398. static const char s[][3] = {"Cn","Lu","Ll","Lt","Lm","Lo","Mn","Mc","Me","Nd","Nl","No","Pc","Pd","Ps","Pe","Pi","Pf","Po","Sm","Sc","Sk","So","Zs","Zl","Zp","Cc","Cf","Cs","Co"};
  399. return s[utf8proc_category(c)];
  400. }
  401. #define utf8proc_decompose_lump(replacement_uc) \
  402. return utf8proc_decompose_char((replacement_uc), dst, bufsize, \
  403. options & ~(unsigned int)UTF8PROC_LUMP, last_boundclass)
  404. UTF8PROC_DLLEXPORT utf8proc_ssize_t utf8proc_decompose_char(utf8proc_int32_t uc, utf8proc_int32_t *dst, utf8proc_ssize_t bufsize, utf8proc_option_t options, int *last_boundclass) {
  405. const utf8proc_property_t *property;
  406. utf8proc_propval_t category;
  407. utf8proc_int32_t hangul_sindex;
  408. if (uc < 0 || uc >= 0x110000) return UTF8PROC_ERROR_NOTASSIGNED;
  409. property = unsafe_get_property(uc);
  410. category = property->category;
  411. hangul_sindex = uc - UTF8PROC_HANGUL_SBASE;
  412. if (options & (UTF8PROC_COMPOSE|UTF8PROC_DECOMPOSE)) {
  413. if (hangul_sindex >= 0 && hangul_sindex < UTF8PROC_HANGUL_SCOUNT) {
  414. utf8proc_int32_t hangul_tindex;
  415. if (bufsize >= 1) {
  416. dst[0] = UTF8PROC_HANGUL_LBASE +
  417. hangul_sindex / UTF8PROC_HANGUL_NCOUNT;
  418. if (bufsize >= 2) dst[1] = UTF8PROC_HANGUL_VBASE +
  419. (hangul_sindex % UTF8PROC_HANGUL_NCOUNT) / UTF8PROC_HANGUL_TCOUNT;
  420. }
  421. hangul_tindex = hangul_sindex % UTF8PROC_HANGUL_TCOUNT;
  422. if (!hangul_tindex) return 2;
  423. if (bufsize >= 3) dst[2] = UTF8PROC_HANGUL_TBASE + hangul_tindex;
  424. return 3;
  425. }
  426. }
  427. if (options & UTF8PROC_REJECTNA) {
  428. if (!category) return UTF8PROC_ERROR_NOTASSIGNED;
  429. }
  430. if (options & UTF8PROC_IGNORE) {
  431. if (property->ignorable) return 0;
  432. }
  433. if (options & UTF8PROC_STRIPNA) {
  434. if (!category) return 0;
  435. }
  436. if (options & UTF8PROC_LUMP) {
  437. if (category == UTF8PROC_CATEGORY_ZS) utf8proc_decompose_lump(0x0020);
  438. if (uc == 0x2018 || uc == 0x2019 || uc == 0x02BC || uc == 0x02C8)
  439. utf8proc_decompose_lump(0x0027);
  440. if (category == UTF8PROC_CATEGORY_PD || uc == 0x2212)
  441. utf8proc_decompose_lump(0x002D);
  442. if (uc == 0x2044 || uc == 0x2215) utf8proc_decompose_lump(0x002F);
  443. if (uc == 0x2236) utf8proc_decompose_lump(0x003A);
  444. if (uc == 0x2039 || uc == 0x2329 || uc == 0x3008)
  445. utf8proc_decompose_lump(0x003C);
  446. if (uc == 0x203A || uc == 0x232A || uc == 0x3009)
  447. utf8proc_decompose_lump(0x003E);
  448. if (uc == 0x2216) utf8proc_decompose_lump(0x005C);
  449. if (uc == 0x02C4 || uc == 0x02C6 || uc == 0x2038 || uc == 0x2303)
  450. utf8proc_decompose_lump(0x005E);
  451. if (category == UTF8PROC_CATEGORY_PC || uc == 0x02CD)
  452. utf8proc_decompose_lump(0x005F);
  453. if (uc == 0x02CB) utf8proc_decompose_lump(0x0060);
  454. if (uc == 0x2223) utf8proc_decompose_lump(0x007C);
  455. if (uc == 0x223C) utf8proc_decompose_lump(0x007E);
  456. if ((options & UTF8PROC_NLF2LS) && (options & UTF8PROC_NLF2PS)) {
  457. if (category == UTF8PROC_CATEGORY_ZL ||
  458. category == UTF8PROC_CATEGORY_ZP)
  459. utf8proc_decompose_lump(0x000A);
  460. }
  461. }
  462. if (options & UTF8PROC_STRIPMARK) {
  463. if (category == UTF8PROC_CATEGORY_MN ||
  464. category == UTF8PROC_CATEGORY_MC ||
  465. category == UTF8PROC_CATEGORY_ME) return 0;
  466. }
  467. if (options & UTF8PROC_CASEFOLD) {
  468. if (property->casefold_seqindex != UINT16_MAX) {
  469. return seqindex_write_char_decomposed(property->casefold_seqindex, dst, bufsize, options, last_boundclass);
  470. }
  471. }
  472. if (options & (UTF8PROC_COMPOSE|UTF8PROC_DECOMPOSE)) {
  473. if (property->decomp_seqindex != UINT16_MAX &&
  474. (!property->decomp_type || (options & UTF8PROC_COMPAT))) {
  475. return seqindex_write_char_decomposed(property->decomp_seqindex, dst, bufsize, options, last_boundclass);
  476. }
  477. }
  478. if (options & UTF8PROC_CHARBOUND) {
  479. utf8proc_bool boundary;
  480. boundary = grapheme_break_extended(0, property->boundclass, 0, property->indic_conjunct_break,
  481. last_boundclass);
  482. if (boundary) {
  483. if (bufsize >= 1) dst[0] = -1; /* sentinel value for grapheme break */
  484. if (bufsize >= 2) dst[1] = uc;
  485. return 2;
  486. }
  487. }
  488. if (bufsize >= 1) *dst = uc;
  489. return 1;
  490. }
  491. UTF8PROC_DLLEXPORT utf8proc_ssize_t utf8proc_decompose(
  492. const utf8proc_uint8_t *str, utf8proc_ssize_t strlen,
  493. utf8proc_int32_t *buffer, utf8proc_ssize_t bufsize, utf8proc_option_t options
  494. ) {
  495. return utf8proc_decompose_custom(str, strlen, buffer, bufsize, options, NULL, NULL);
  496. }
  497. UTF8PROC_DLLEXPORT utf8proc_ssize_t utf8proc_decompose_custom(
  498. const utf8proc_uint8_t *str, utf8proc_ssize_t strlen,
  499. utf8proc_int32_t *buffer, utf8proc_ssize_t bufsize, utf8proc_option_t options,
  500. utf8proc_custom_func custom_func, void *custom_data
  501. ) {
  502. /* strlen will be ignored, if UTF8PROC_NULLTERM is set in options */
  503. utf8proc_ssize_t wpos = 0;
  504. if ((options & UTF8PROC_COMPOSE) && (options & UTF8PROC_DECOMPOSE))
  505. return UTF8PROC_ERROR_INVALIDOPTS;
  506. if ((options & UTF8PROC_STRIPMARK) &&
  507. !(options & UTF8PROC_COMPOSE) && !(options & UTF8PROC_DECOMPOSE))
  508. return UTF8PROC_ERROR_INVALIDOPTS;
  509. {
  510. utf8proc_int32_t uc;
  511. utf8proc_ssize_t rpos = 0;
  512. utf8proc_ssize_t decomp_result;
  513. int boundclass = UTF8PROC_BOUNDCLASS_START;
  514. while (1) {
  515. if (options & UTF8PROC_NULLTERM) {
  516. rpos += utf8proc_iterate(str + rpos, -1, &uc);
  517. /* checking of return value is not necessary,
  518. as 'uc' is < 0 in case of error */
  519. if (uc < 0) return UTF8PROC_ERROR_INVALIDUTF8;
  520. if (rpos < 0) return UTF8PROC_ERROR_OVERFLOW;
  521. if (uc == 0) break;
  522. } else {
  523. if (rpos >= strlen) break;
  524. rpos += utf8proc_iterate(str + rpos, strlen - rpos, &uc);
  525. if (uc < 0) return UTF8PROC_ERROR_INVALIDUTF8;
  526. }
  527. if (custom_func != NULL) {
  528. uc = custom_func(uc, custom_data); /* user-specified custom mapping */
  529. }
  530. decomp_result = utf8proc_decompose_char(
  531. uc, buffer + wpos, (bufsize > wpos) ? (bufsize - wpos) : 0, options,
  532. &boundclass
  533. );
  534. if (decomp_result < 0) return decomp_result;
  535. wpos += decomp_result;
  536. /* prohibiting integer overflows due to too long strings: */
  537. if (wpos < 0 ||
  538. wpos > (utf8proc_ssize_t)(SSIZE_MAX/sizeof(utf8proc_int32_t)/2))
  539. return UTF8PROC_ERROR_OVERFLOW;
  540. }
  541. }
  542. if ((options & (UTF8PROC_COMPOSE|UTF8PROC_DECOMPOSE)) && bufsize >= wpos) {
  543. utf8proc_ssize_t pos = 0;
  544. while (pos < wpos-1) {
  545. utf8proc_int32_t uc1, uc2;
  546. const utf8proc_property_t *property1, *property2;
  547. uc1 = buffer[pos];
  548. uc2 = buffer[pos+1];
  549. property1 = unsafe_get_property(uc1);
  550. property2 = unsafe_get_property(uc2);
  551. if (property1->combining_class > property2->combining_class &&
  552. property2->combining_class > 0) {
  553. buffer[pos] = uc2;
  554. buffer[pos+1] = uc1;
  555. if (pos > 0) pos--; else pos++;
  556. } else {
  557. pos++;
  558. }
  559. }
  560. }
  561. return wpos;
  562. }
  563. UTF8PROC_DLLEXPORT utf8proc_ssize_t utf8proc_normalize_utf32(utf8proc_int32_t *buffer, utf8proc_ssize_t length, utf8proc_option_t options) {
  564. /* UTF8PROC_NULLTERM option will be ignored, 'length' is never ignored */
  565. if (options & (UTF8PROC_NLF2LS | UTF8PROC_NLF2PS | UTF8PROC_STRIPCC)) {
  566. utf8proc_ssize_t rpos;
  567. utf8proc_ssize_t wpos = 0;
  568. utf8proc_int32_t uc;
  569. for (rpos = 0; rpos < length; rpos++) {
  570. uc = buffer[rpos];
  571. if (uc == 0x000D && rpos < length-1 && buffer[rpos+1] == 0x000A) rpos++;
  572. if (uc == 0x000A || uc == 0x000D || uc == 0x0085 ||
  573. ((options & UTF8PROC_STRIPCC) && (uc == 0x000B || uc == 0x000C))) {
  574. if (options & UTF8PROC_NLF2LS) {
  575. if (options & UTF8PROC_NLF2PS) {
  576. buffer[wpos++] = 0x000A;
  577. } else {
  578. buffer[wpos++] = 0x2028;
  579. }
  580. } else {
  581. if (options & UTF8PROC_NLF2PS) {
  582. buffer[wpos++] = 0x2029;
  583. } else {
  584. buffer[wpos++] = 0x0020;
  585. }
  586. }
  587. } else if ((options & UTF8PROC_STRIPCC) &&
  588. (uc < 0x0020 || (uc >= 0x007F && uc < 0x00A0))) {
  589. if (uc == 0x0009) buffer[wpos++] = 0x0020;
  590. } else {
  591. buffer[wpos++] = uc;
  592. }
  593. }
  594. length = wpos;
  595. }
  596. if (options & UTF8PROC_COMPOSE) {
  597. utf8proc_int32_t *starter = NULL;
  598. const utf8proc_property_t *starter_property = NULL;
  599. utf8proc_propval_t max_combining_class = -1;
  600. utf8proc_ssize_t rpos;
  601. utf8proc_ssize_t wpos = 0;
  602. for (rpos = 0; rpos < length; rpos++) {
  603. utf8proc_int32_t current_char = buffer[rpos];
  604. const utf8proc_property_t *current_property = unsafe_get_property(current_char);
  605. if (starter && current_property->combining_class > max_combining_class) {
  606. /* combination perhaps possible */
  607. utf8proc_int32_t hangul_lindex;
  608. utf8proc_int32_t hangul_sindex;
  609. hangul_lindex = *starter - UTF8PROC_HANGUL_LBASE;
  610. if (hangul_lindex >= 0 && hangul_lindex < UTF8PROC_HANGUL_LCOUNT) {
  611. utf8proc_int32_t hangul_vindex;
  612. hangul_vindex = current_char - UTF8PROC_HANGUL_VBASE;
  613. if (hangul_vindex >= 0 && hangul_vindex < UTF8PROC_HANGUL_VCOUNT) {
  614. *starter = UTF8PROC_HANGUL_SBASE +
  615. (hangul_lindex * UTF8PROC_HANGUL_VCOUNT + hangul_vindex) *
  616. UTF8PROC_HANGUL_TCOUNT;
  617. starter_property = NULL;
  618. continue;
  619. }
  620. }
  621. hangul_sindex = *starter - UTF8PROC_HANGUL_SBASE;
  622. if (hangul_sindex >= 0 && hangul_sindex < UTF8PROC_HANGUL_SCOUNT &&
  623. (hangul_sindex % UTF8PROC_HANGUL_TCOUNT) == 0) {
  624. utf8proc_int32_t hangul_tindex;
  625. hangul_tindex = current_char - UTF8PROC_HANGUL_TBASE;
  626. if (hangul_tindex >= 0 && hangul_tindex < UTF8PROC_HANGUL_TCOUNT) {
  627. *starter += hangul_tindex;
  628. starter_property = NULL;
  629. continue;
  630. }
  631. }
  632. if (!starter_property) {
  633. starter_property = unsafe_get_property(*starter);
  634. }
  635. int idx = starter_property->comb_index;
  636. if (idx < 0x3FF && current_property->comb_issecond) {
  637. int len = starter_property->comb_length;
  638. utf8proc_int32_t max_second = utf8proc_combinations_second[idx + len - 1];
  639. if (current_char <= max_second) {
  640. // TODO: binary search? arithmetic search?
  641. for (int off = 0; off < len; ++off) {
  642. utf8proc_int32_t second = utf8proc_combinations_second[idx + off];
  643. if (current_char < second) {
  644. /* not found */
  645. break;
  646. }
  647. if (current_char == second) {
  648. /* found */
  649. utf8proc_int32_t composition = utf8proc_combinations_combined[idx + off];
  650. *starter = composition;
  651. starter_property = NULL;
  652. break;
  653. }
  654. }
  655. if (starter_property == NULL) {
  656. /* found */
  657. continue;
  658. }
  659. }
  660. }
  661. }
  662. buffer[wpos] = current_char;
  663. if (current_property->combining_class) {
  664. if (current_property->combining_class > max_combining_class) {
  665. max_combining_class = current_property->combining_class;
  666. }
  667. } else {
  668. starter = buffer + wpos;
  669. starter_property = NULL;
  670. max_combining_class = -1;
  671. }
  672. wpos++;
  673. }
  674. length = wpos;
  675. }
  676. return length;
  677. }
  678. UTF8PROC_DLLEXPORT utf8proc_ssize_t utf8proc_reencode(utf8proc_int32_t *buffer, utf8proc_ssize_t length, utf8proc_option_t options) {
  679. /* UTF8PROC_NULLTERM option will be ignored, 'length' is never ignored
  680. ASSERT: 'buffer' has one spare byte of free space at the end! */
  681. length = utf8proc_normalize_utf32(buffer, length, options);
  682. if (length < 0) return length;
  683. {
  684. utf8proc_ssize_t rpos, wpos = 0;
  685. utf8proc_int32_t uc;
  686. if (options & UTF8PROC_CHARBOUND) {
  687. for (rpos = 0; rpos < length; rpos++) {
  688. uc = buffer[rpos];
  689. wpos += charbound_encode_char(uc, ((utf8proc_uint8_t *)buffer) + wpos);
  690. }
  691. } else {
  692. for (rpos = 0; rpos < length; rpos++) {
  693. uc = buffer[rpos];
  694. wpos += utf8proc_encode_char(uc, ((utf8proc_uint8_t *)buffer) + wpos);
  695. }
  696. }
  697. ((utf8proc_uint8_t *)buffer)[wpos] = 0;
  698. return wpos;
  699. }
  700. }
  701. UTF8PROC_DLLEXPORT utf8proc_ssize_t utf8proc_map(
  702. const utf8proc_uint8_t *str, utf8proc_ssize_t strlen, utf8proc_uint8_t **dstptr, utf8proc_option_t options
  703. ) {
  704. return utf8proc_map_custom(str, strlen, dstptr, options, NULL, NULL);
  705. }
  706. UTF8PROC_DLLEXPORT utf8proc_ssize_t utf8proc_map_custom(
  707. const utf8proc_uint8_t *str, utf8proc_ssize_t strlen, utf8proc_uint8_t **dstptr, utf8proc_option_t options,
  708. utf8proc_custom_func custom_func, void *custom_data
  709. ) {
  710. utf8proc_int32_t *buffer;
  711. utf8proc_ssize_t result;
  712. *dstptr = NULL;
  713. result = utf8proc_decompose_custom(str, strlen, NULL, 0, options, custom_func, custom_data);
  714. if (result < 0) return result;
  715. buffer = (utf8proc_int32_t *) malloc(((utf8proc_size_t)result) * sizeof(utf8proc_int32_t) + 1);
  716. if (!buffer) return UTF8PROC_ERROR_NOMEM;
  717. result = utf8proc_decompose_custom(str, strlen, buffer, result, options, custom_func, custom_data);
  718. if (result < 0) {
  719. free(buffer);
  720. return result;
  721. }
  722. result = utf8proc_reencode(buffer, result, options);
  723. if (result < 0) {
  724. free(buffer);
  725. return result;
  726. }
  727. {
  728. utf8proc_int32_t *newptr;
  729. newptr = (utf8proc_int32_t *) realloc(buffer, (size_t)result+1);
  730. if (newptr) buffer = newptr;
  731. }
  732. *dstptr = (utf8proc_uint8_t *)buffer;
  733. return result;
  734. }
  735. UTF8PROC_DLLEXPORT utf8proc_uint8_t *utf8proc_NFD(const utf8proc_uint8_t *str) {
  736. utf8proc_uint8_t *retval;
  737. utf8proc_map(str, 0, &retval, UTF8PROC_NULLTERM | UTF8PROC_STABLE |
  738. UTF8PROC_DECOMPOSE);
  739. return retval;
  740. }
  741. UTF8PROC_DLLEXPORT utf8proc_uint8_t *utf8proc_NFC(const utf8proc_uint8_t *str) {
  742. utf8proc_uint8_t *retval;
  743. utf8proc_map(str, 0, &retval, UTF8PROC_NULLTERM | UTF8PROC_STABLE |
  744. UTF8PROC_COMPOSE);
  745. return retval;
  746. }
  747. UTF8PROC_DLLEXPORT utf8proc_uint8_t *utf8proc_NFKD(const utf8proc_uint8_t *str) {
  748. utf8proc_uint8_t *retval;
  749. utf8proc_map(str, 0, &retval, UTF8PROC_NULLTERM | UTF8PROC_STABLE |
  750. UTF8PROC_DECOMPOSE | UTF8PROC_COMPAT);
  751. return retval;
  752. }
  753. UTF8PROC_DLLEXPORT utf8proc_uint8_t *utf8proc_NFKC(const utf8proc_uint8_t *str) {
  754. utf8proc_uint8_t *retval;
  755. utf8proc_map(str, 0, &retval, UTF8PROC_NULLTERM | UTF8PROC_STABLE |
  756. UTF8PROC_COMPOSE | UTF8PROC_COMPAT);
  757. return retval;
  758. }
  759. UTF8PROC_DLLEXPORT utf8proc_uint8_t *utf8proc_NFKC_Casefold(const utf8proc_uint8_t *str) {
  760. utf8proc_uint8_t *retval;
  761. utf8proc_map(str, 0, &retval, UTF8PROC_NULLTERM | UTF8PROC_STABLE |
  762. UTF8PROC_COMPOSE | UTF8PROC_COMPAT | UTF8PROC_CASEFOLD | UTF8PROC_IGNORE);
  763. return retval;
  764. }