utf8proc.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754
  1. /* -*- mode: c; c-basic-offset: 2; tab-width: 2; indent-tabs-mode: nil -*- */
  2. /*
  3. * Copyright (c) 2015 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. * http://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. #include "utf8proc_data.c"
  41. UTF8PROC_DLLEXPORT const utf8proc_int8_t utf8proc_utf8class[256] = {
  42. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  43. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  44. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  45. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  46. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  47. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  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. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  51. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  52. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  53. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  54. 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  55. 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  56. 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
  57. 4, 4, 4, 4, 4, 4, 4, 4, 0, 0, 0, 0, 0, 0, 0, 0 };
  58. #define UTF8PROC_HANGUL_SBASE 0xAC00
  59. #define UTF8PROC_HANGUL_LBASE 0x1100
  60. #define UTF8PROC_HANGUL_VBASE 0x1161
  61. #define UTF8PROC_HANGUL_TBASE 0x11A7
  62. #define UTF8PROC_HANGUL_LCOUNT 19
  63. #define UTF8PROC_HANGUL_VCOUNT 21
  64. #define UTF8PROC_HANGUL_TCOUNT 28
  65. #define UTF8PROC_HANGUL_NCOUNT 588
  66. #define UTF8PROC_HANGUL_SCOUNT 11172
  67. /* END is exclusive */
  68. #define UTF8PROC_HANGUL_L_START 0x1100
  69. #define UTF8PROC_HANGUL_L_END 0x115A
  70. #define UTF8PROC_HANGUL_L_FILLER 0x115F
  71. #define UTF8PROC_HANGUL_V_START 0x1160
  72. #define UTF8PROC_HANGUL_V_END 0x11A3
  73. #define UTF8PROC_HANGUL_T_START 0x11A8
  74. #define UTF8PROC_HANGUL_T_END 0x11FA
  75. #define UTF8PROC_HANGUL_S_START 0xAC00
  76. #define UTF8PROC_HANGUL_S_END 0xD7A4
  77. /* Should follow semantic-versioning rules (semver.org) based on API
  78. compatibility. (Note that the shared-library version number will
  79. be different, being based on ABI compatibility.): */
  80. #define STRINGIZEx(x) #x
  81. #define STRINGIZE(x) STRINGIZEx(x)
  82. UTF8PROC_DLLEXPORT const char *utf8proc_version(void) {
  83. return STRINGIZE(UTF8PROC_VERSION_MAJOR) "." STRINGIZE(UTF8PROC_VERSION_MINOR) "." STRINGIZE(UTF8PROC_VERSION_PATCH) "";
  84. }
  85. UTF8PROC_DLLEXPORT const char *utf8proc_errmsg(utf8proc_ssize_t errcode) {
  86. switch (errcode) {
  87. case UTF8PROC_ERROR_NOMEM:
  88. return "Memory for processing UTF-8 data could not be allocated.";
  89. case UTF8PROC_ERROR_OVERFLOW:
  90. return "UTF-8 string is too long to be processed.";
  91. case UTF8PROC_ERROR_INVALIDUTF8:
  92. return "Invalid UTF-8 string";
  93. case UTF8PROC_ERROR_NOTASSIGNED:
  94. return "Unassigned Unicode code point found in UTF-8 string.";
  95. case UTF8PROC_ERROR_INVALIDOPTS:
  96. return "Invalid options for UTF-8 processing chosen.";
  97. default:
  98. return "An unknown error occurred while processing UTF-8 data.";
  99. }
  100. }
  101. #define utf_cont(ch) (((ch) & 0xc0) == 0x80)
  102. UTF8PROC_DLLEXPORT utf8proc_ssize_t utf8proc_iterate(
  103. const utf8proc_uint8_t *str, utf8proc_ssize_t strlen, utf8proc_int32_t *dst
  104. ) {
  105. utf8proc_uint32_t uc;
  106. const utf8proc_uint8_t *end;
  107. *dst = -1;
  108. if (!strlen) return 0;
  109. end = str + ((strlen < 0) ? 4 : strlen);
  110. uc = *str++;
  111. if (uc < 0x80) {
  112. *dst = uc;
  113. return 1;
  114. }
  115. // Must be between 0xc2 and 0xf4 inclusive to be valid
  116. if ((uc - 0xc2) > (0xf4-0xc2)) return UTF8PROC_ERROR_INVALIDUTF8;
  117. if (uc < 0xe0) { // 2-byte sequence
  118. // Must have valid continuation character
  119. if (str >= end || !utf_cont(*str)) return UTF8PROC_ERROR_INVALIDUTF8;
  120. *dst = ((uc & 0x1f)<<6) | (*str & 0x3f);
  121. return 2;
  122. }
  123. if (uc < 0xf0) { // 3-byte sequence
  124. if ((str + 1 >= end) || !utf_cont(*str) || !utf_cont(str[1]))
  125. return UTF8PROC_ERROR_INVALIDUTF8;
  126. // Check for surrogate chars
  127. if (uc == 0xed && *str > 0x9f)
  128. return UTF8PROC_ERROR_INVALIDUTF8;
  129. uc = ((uc & 0xf)<<12) | ((*str & 0x3f)<<6) | (str[1] & 0x3f);
  130. if (uc < 0x800)
  131. return UTF8PROC_ERROR_INVALIDUTF8;
  132. *dst = uc;
  133. return 3;
  134. }
  135. // 4-byte sequence
  136. // Must have 3 valid continuation characters
  137. if ((str + 2 >= end) || !utf_cont(*str) || !utf_cont(str[1]) || !utf_cont(str[2]))
  138. return UTF8PROC_ERROR_INVALIDUTF8;
  139. // Make sure in correct range (0x10000 - 0x10ffff)
  140. if (uc == 0xf0) {
  141. if (*str < 0x90) return UTF8PROC_ERROR_INVALIDUTF8;
  142. } else if (uc == 0xf4) {
  143. if (*str > 0x8f) return UTF8PROC_ERROR_INVALIDUTF8;
  144. }
  145. *dst = ((uc & 7)<<18) | ((*str & 0x3f)<<12) | ((str[1] & 0x3f)<<6) | (str[2] & 0x3f);
  146. return 4;
  147. }
  148. UTF8PROC_DLLEXPORT utf8proc_bool utf8proc_codepoint_valid(utf8proc_int32_t uc) {
  149. return (((utf8proc_uint32_t)uc)-0xd800 > 0x07ff) && ((utf8proc_uint32_t)uc < 0x110000);
  150. }
  151. UTF8PROC_DLLEXPORT utf8proc_ssize_t utf8proc_encode_char(utf8proc_int32_t uc, utf8proc_uint8_t *dst) {
  152. if (uc < 0x00) {
  153. return 0;
  154. } else if (uc < 0x80) {
  155. dst[0] = (utf8proc_uint8_t) uc;
  156. return 1;
  157. } else if (uc < 0x800) {
  158. dst[0] = (utf8proc_uint8_t)(0xC0 + (uc >> 6));
  159. dst[1] = (utf8proc_uint8_t)(0x80 + (uc & 0x3F));
  160. return 2;
  161. // Note: we allow encoding 0xd800-0xdfff here, so as not to change
  162. // the API, however, these are actually invalid in UTF-8
  163. } else if (uc < 0x10000) {
  164. dst[0] = (utf8proc_uint8_t)(0xE0 + (uc >> 12));
  165. dst[1] = (utf8proc_uint8_t)(0x80 + ((uc >> 6) & 0x3F));
  166. dst[2] = (utf8proc_uint8_t)(0x80 + (uc & 0x3F));
  167. return 3;
  168. } else if (uc < 0x110000) {
  169. dst[0] = (utf8proc_uint8_t)(0xF0 + (uc >> 18));
  170. dst[1] = (utf8proc_uint8_t)(0x80 + ((uc >> 12) & 0x3F));
  171. dst[2] = (utf8proc_uint8_t)(0x80 + ((uc >> 6) & 0x3F));
  172. dst[3] = (utf8proc_uint8_t)(0x80 + (uc & 0x3F));
  173. return 4;
  174. } else return 0;
  175. }
  176. /* internal "unsafe" version that does not check whether uc is in range */
  177. static utf8proc_ssize_t unsafe_encode_char(utf8proc_int32_t uc, utf8proc_uint8_t *dst) {
  178. if (uc < 0x00) {
  179. return 0;
  180. } else if (uc < 0x80) {
  181. dst[0] = (utf8proc_uint8_t)uc;
  182. return 1;
  183. } else if (uc < 0x800) {
  184. dst[0] = (utf8proc_uint8_t)(0xC0 + (uc >> 6));
  185. dst[1] = (utf8proc_uint8_t)(0x80 + (uc & 0x3F));
  186. return 2;
  187. } else if (uc == 0xFFFF) {
  188. dst[0] = (utf8proc_uint8_t)0xFF;
  189. return 1;
  190. } else if (uc == 0xFFFE) {
  191. dst[0] = (utf8proc_uint8_t)0xFE;
  192. return 1;
  193. } else if (uc < 0x10000) {
  194. dst[0] = (utf8proc_uint8_t)(0xE0 + (uc >> 12));
  195. dst[1] = (utf8proc_uint8_t)(0x80 + ((uc >> 6) & 0x3F));
  196. dst[2] = (utf8proc_uint8_t)(0x80 + (uc & 0x3F));
  197. return 3;
  198. } else if (uc < 0x110000) {
  199. dst[0] = (utf8proc_uint8_t)(0xF0 + (uc >> 18));
  200. dst[1] = (utf8proc_uint8_t)(0x80 + ((uc >> 12) & 0x3F));
  201. dst[2] = (utf8proc_uint8_t)(0x80 + ((uc >> 6) & 0x3F));
  202. dst[3] = (utf8proc_uint8_t)(0x80 + (uc & 0x3F));
  203. return 4;
  204. } else return 0;
  205. }
  206. /* internal "unsafe" version that does not check whether uc is in range */
  207. static const utf8proc_property_t *unsafe_get_property(utf8proc_int32_t uc) {
  208. /* ASSERT: uc >= 0 && uc < 0x110000 */
  209. return utf8proc_properties + (
  210. utf8proc_stage2table[
  211. utf8proc_stage1table[uc >> 8] + (uc & 0xFF)
  212. ]
  213. );
  214. }
  215. UTF8PROC_DLLEXPORT const utf8proc_property_t *utf8proc_get_property(utf8proc_int32_t uc) {
  216. return uc < 0 || uc >= 0x110000 ? utf8proc_properties : unsafe_get_property(uc);
  217. }
  218. /* return whether there is a grapheme break between boundclasses lbc and tbc
  219. (according to the definition of extended grapheme clusters)
  220. Rule numbering refers to TR29 Version 29 (Unicode 9.0.0):
  221. http://www.unicode.org/reports/tr29/tr29-29.html
  222. CAVEATS:
  223. Please note that evaluation of GB10 (grapheme breaks between emoji zwj sequences)
  224. and GB 12/13 (regional indicator code points) require knowledge of previous characters
  225. and are thus not handled by this function. This may result in an incorrect break before
  226. an E_Modifier class codepoint and an incorrectly missing break between two
  227. REGIONAL_INDICATOR class code points if such support does not exist in the caller.
  228. See the special support in grapheme_break_extended, for required bookkeeping by the caller.
  229. */
  230. static utf8proc_bool grapheme_break_simple(int lbc, int tbc) {
  231. return
  232. (lbc == UTF8PROC_BOUNDCLASS_START) ? true : // GB1
  233. (lbc == UTF8PROC_BOUNDCLASS_CR && // GB3
  234. tbc == UTF8PROC_BOUNDCLASS_LF) ? false : // ---
  235. (lbc >= UTF8PROC_BOUNDCLASS_CR && lbc <= UTF8PROC_BOUNDCLASS_CONTROL) ? true : // GB4
  236. (tbc >= UTF8PROC_BOUNDCLASS_CR && tbc <= UTF8PROC_BOUNDCLASS_CONTROL) ? true : // GB5
  237. (lbc == UTF8PROC_BOUNDCLASS_L && // GB6
  238. (tbc == UTF8PROC_BOUNDCLASS_L || // ---
  239. tbc == UTF8PROC_BOUNDCLASS_V || // ---
  240. tbc == UTF8PROC_BOUNDCLASS_LV || // ---
  241. tbc == UTF8PROC_BOUNDCLASS_LVT)) ? false : // ---
  242. ((lbc == UTF8PROC_BOUNDCLASS_LV || // GB7
  243. lbc == UTF8PROC_BOUNDCLASS_V) && // ---
  244. (tbc == UTF8PROC_BOUNDCLASS_V || // ---
  245. tbc == UTF8PROC_BOUNDCLASS_T)) ? false : // ---
  246. ((lbc == UTF8PROC_BOUNDCLASS_LVT || // GB8
  247. lbc == UTF8PROC_BOUNDCLASS_T) && // ---
  248. tbc == UTF8PROC_BOUNDCLASS_T) ? false : // ---
  249. (tbc == UTF8PROC_BOUNDCLASS_EXTEND || // GB9
  250. tbc == UTF8PROC_BOUNDCLASS_ZWJ || // ---
  251. tbc == UTF8PROC_BOUNDCLASS_SPACINGMARK || // GB9a
  252. lbc == UTF8PROC_BOUNDCLASS_PREPEND) ? false : // GB9b
  253. ((lbc == UTF8PROC_BOUNDCLASS_E_BASE || // GB10 (requires additional handling below)
  254. lbc == UTF8PROC_BOUNDCLASS_E_BASE_GAZ) && // ----
  255. tbc == UTF8PROC_BOUNDCLASS_E_MODIFIER) ? false : // ----
  256. (lbc == UTF8PROC_BOUNDCLASS_ZWJ && // GB11
  257. (tbc == UTF8PROC_BOUNDCLASS_GLUE_AFTER_ZWJ || // ----
  258. tbc == UTF8PROC_BOUNDCLASS_E_BASE_GAZ)) ? false : // ----
  259. (lbc == UTF8PROC_BOUNDCLASS_REGIONAL_INDICATOR && // GB12/13 (requires additional handling below)
  260. tbc == UTF8PROC_BOUNDCLASS_REGIONAL_INDICATOR) ? false : // ----
  261. true; // GB999
  262. }
  263. static utf8proc_bool grapheme_break_extended(int lbc, int tbc, utf8proc_int32_t *state)
  264. {
  265. int lbc_override = ((state && *state != UTF8PROC_BOUNDCLASS_START)
  266. ? *state : lbc);
  267. utf8proc_bool break_permitted = grapheme_break_simple(lbc_override, tbc);
  268. if (state) {
  269. // Special support for GB 12/13 made possible by GB999. After two RI
  270. // class codepoints we want to force a break. Do this by resetting the
  271. // second RI's bound class to UTF8PROC_BOUNDCLASS_OTHER, to force a break
  272. // after that character according to GB999 (unless of course such a break is
  273. // forbidden by a different rule such as GB9).
  274. if (*state == tbc && tbc == UTF8PROC_BOUNDCLASS_REGIONAL_INDICATOR)
  275. *state = UTF8PROC_BOUNDCLASS_OTHER;
  276. // Special support for GB10. Fold any EXTEND codepoints into the previous
  277. // boundclass if we're dealing with an emoji base boundclass.
  278. else if ((*state == UTF8PROC_BOUNDCLASS_E_BASE ||
  279. *state == UTF8PROC_BOUNDCLASS_E_BASE_GAZ) &&
  280. tbc == UTF8PROC_BOUNDCLASS_EXTEND)
  281. *state = UTF8PROC_BOUNDCLASS_E_BASE;
  282. else
  283. *state = tbc;
  284. }
  285. return break_permitted;
  286. }
  287. UTF8PROC_DLLEXPORT utf8proc_bool utf8proc_grapheme_break_stateful(
  288. utf8proc_int32_t c1, utf8proc_int32_t c2, utf8proc_int32_t *state) {
  289. return grapheme_break_extended(utf8proc_get_property(c1)->boundclass,
  290. utf8proc_get_property(c2)->boundclass,
  291. state);
  292. }
  293. UTF8PROC_DLLEXPORT utf8proc_bool utf8proc_grapheme_break(
  294. utf8proc_int32_t c1, utf8proc_int32_t c2) {
  295. return utf8proc_grapheme_break_stateful(c1, c2, NULL);
  296. }
  297. static utf8proc_int32_t seqindex_decode_entry(const utf8proc_uint16_t **entry)
  298. {
  299. utf8proc_int32_t entry_cp = **entry;
  300. if ((entry_cp & 0xF800) == 0xD800) {
  301. *entry = *entry + 1;
  302. entry_cp = ((entry_cp & 0x03FF) << 10) | (**entry & 0x03FF);
  303. entry_cp += 0x10000;
  304. }
  305. return entry_cp;
  306. }
  307. static utf8proc_int32_t seqindex_decode_index(const utf8proc_uint32_t seqindex)
  308. {
  309. const utf8proc_uint16_t *entry = &utf8proc_sequences[seqindex];
  310. return seqindex_decode_entry(&entry);
  311. }
  312. 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) {
  313. utf8proc_ssize_t written = 0;
  314. const utf8proc_uint16_t *entry = &utf8proc_sequences[seqindex & 0x1FFF];
  315. int len = seqindex >> 13;
  316. if (len >= 7) {
  317. len = *entry;
  318. entry++;
  319. }
  320. for (; len >= 0; entry++, len--) {
  321. utf8proc_int32_t entry_cp = seqindex_decode_entry(&entry);
  322. written += utf8proc_decompose_char(entry_cp, dst+written,
  323. (bufsize > written) ? (bufsize - written) : 0, options,
  324. last_boundclass);
  325. if (written < 0) return UTF8PROC_ERROR_OVERFLOW;
  326. }
  327. return written;
  328. }
  329. UTF8PROC_DLLEXPORT utf8proc_int32_t utf8proc_tolower(utf8proc_int32_t c)
  330. {
  331. utf8proc_int32_t cl = utf8proc_get_property(c)->lowercase_seqindex;
  332. return cl != UINT16_MAX ? seqindex_decode_index(cl) : c;
  333. }
  334. UTF8PROC_DLLEXPORT utf8proc_int32_t utf8proc_toupper(utf8proc_int32_t c)
  335. {
  336. utf8proc_int32_t cu = utf8proc_get_property(c)->uppercase_seqindex;
  337. return cu != UINT16_MAX ? seqindex_decode_index(cu) : c;
  338. }
  339. UTF8PROC_DLLEXPORT utf8proc_int32_t utf8proc_totitle(utf8proc_int32_t c)
  340. {
  341. utf8proc_int32_t cu = utf8proc_get_property(c)->titlecase_seqindex;
  342. return cu != UINT16_MAX ? seqindex_decode_index(cu) : c;
  343. }
  344. /* return a character width analogous to wcwidth (except portable and
  345. hopefully less buggy than most system wcwidth functions). */
  346. UTF8PROC_DLLEXPORT int utf8proc_charwidth(utf8proc_int32_t c) {
  347. return utf8proc_get_property(c)->charwidth;
  348. }
  349. UTF8PROC_DLLEXPORT utf8proc_category_t utf8proc_category(utf8proc_int32_t c) {
  350. return utf8proc_get_property(c)->category;
  351. }
  352. UTF8PROC_DLLEXPORT const char *utf8proc_category_string(utf8proc_int32_t c) {
  353. 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"};
  354. return s[utf8proc_category(c)];
  355. }
  356. #define utf8proc_decompose_lump(replacement_uc) \
  357. return utf8proc_decompose_char((replacement_uc), dst, bufsize, \
  358. options & ~UTF8PROC_LUMP, last_boundclass)
  359. 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) {
  360. const utf8proc_property_t *property;
  361. utf8proc_propval_t category;
  362. utf8proc_int32_t hangul_sindex;
  363. if (uc < 0 || uc >= 0x110000) return UTF8PROC_ERROR_NOTASSIGNED;
  364. property = unsafe_get_property(uc);
  365. category = property->category;
  366. hangul_sindex = uc - UTF8PROC_HANGUL_SBASE;
  367. if (options & (UTF8PROC_COMPOSE|UTF8PROC_DECOMPOSE)) {
  368. if (hangul_sindex >= 0 && hangul_sindex < UTF8PROC_HANGUL_SCOUNT) {
  369. utf8proc_int32_t hangul_tindex;
  370. if (bufsize >= 1) {
  371. dst[0] = UTF8PROC_HANGUL_LBASE +
  372. hangul_sindex / UTF8PROC_HANGUL_NCOUNT;
  373. if (bufsize >= 2) dst[1] = UTF8PROC_HANGUL_VBASE +
  374. (hangul_sindex % UTF8PROC_HANGUL_NCOUNT) / UTF8PROC_HANGUL_TCOUNT;
  375. }
  376. hangul_tindex = hangul_sindex % UTF8PROC_HANGUL_TCOUNT;
  377. if (!hangul_tindex) return 2;
  378. if (bufsize >= 3) dst[2] = UTF8PROC_HANGUL_TBASE + hangul_tindex;
  379. return 3;
  380. }
  381. }
  382. if (options & UTF8PROC_REJECTNA) {
  383. if (!category) return UTF8PROC_ERROR_NOTASSIGNED;
  384. }
  385. if (options & UTF8PROC_IGNORE) {
  386. if (property->ignorable) return 0;
  387. }
  388. if (options & UTF8PROC_LUMP) {
  389. if (category == UTF8PROC_CATEGORY_ZS) utf8proc_decompose_lump(0x0020);
  390. if (uc == 0x2018 || uc == 0x2019 || uc == 0x02BC || uc == 0x02C8)
  391. utf8proc_decompose_lump(0x0027);
  392. if (category == UTF8PROC_CATEGORY_PD || uc == 0x2212)
  393. utf8proc_decompose_lump(0x002D);
  394. if (uc == 0x2044 || uc == 0x2215) utf8proc_decompose_lump(0x002F);
  395. if (uc == 0x2236) utf8proc_decompose_lump(0x003A);
  396. if (uc == 0x2039 || uc == 0x2329 || uc == 0x3008)
  397. utf8proc_decompose_lump(0x003C);
  398. if (uc == 0x203A || uc == 0x232A || uc == 0x3009)
  399. utf8proc_decompose_lump(0x003E);
  400. if (uc == 0x2216) utf8proc_decompose_lump(0x005C);
  401. if (uc == 0x02C4 || uc == 0x02C6 || uc == 0x2038 || uc == 0x2303)
  402. utf8proc_decompose_lump(0x005E);
  403. if (category == UTF8PROC_CATEGORY_PC || uc == 0x02CD)
  404. utf8proc_decompose_lump(0x005F);
  405. if (uc == 0x02CB) utf8proc_decompose_lump(0x0060);
  406. if (uc == 0x2223) utf8proc_decompose_lump(0x007C);
  407. if (uc == 0x223C) utf8proc_decompose_lump(0x007E);
  408. if ((options & UTF8PROC_NLF2LS) && (options & UTF8PROC_NLF2PS)) {
  409. if (category == UTF8PROC_CATEGORY_ZL ||
  410. category == UTF8PROC_CATEGORY_ZP)
  411. utf8proc_decompose_lump(0x000A);
  412. }
  413. }
  414. if (options & UTF8PROC_STRIPMARK) {
  415. if (category == UTF8PROC_CATEGORY_MN ||
  416. category == UTF8PROC_CATEGORY_MC ||
  417. category == UTF8PROC_CATEGORY_ME) return 0;
  418. }
  419. if (options & UTF8PROC_CASEFOLD) {
  420. if (property->casefold_seqindex != UINT16_MAX) {
  421. return seqindex_write_char_decomposed(property->casefold_seqindex, dst, bufsize, options, last_boundclass);
  422. }
  423. }
  424. if (options & (UTF8PROC_COMPOSE|UTF8PROC_DECOMPOSE)) {
  425. if (property->decomp_seqindex != UINT16_MAX &&
  426. (!property->decomp_type || (options & UTF8PROC_COMPAT))) {
  427. return seqindex_write_char_decomposed(property->decomp_seqindex, dst, bufsize, options, last_boundclass);
  428. }
  429. }
  430. if (options & UTF8PROC_CHARBOUND) {
  431. utf8proc_bool boundary;
  432. int tbc = property->boundclass;
  433. boundary = grapheme_break_extended(*last_boundclass, tbc, last_boundclass);
  434. if (boundary) {
  435. if (bufsize >= 1) dst[0] = 0xFFFF;
  436. if (bufsize >= 2) dst[1] = uc;
  437. return 2;
  438. }
  439. }
  440. if (bufsize >= 1) *dst = uc;
  441. return 1;
  442. }
  443. UTF8PROC_DLLEXPORT utf8proc_ssize_t utf8proc_decompose(
  444. const utf8proc_uint8_t *str, utf8proc_ssize_t strlen,
  445. utf8proc_int32_t *buffer, utf8proc_ssize_t bufsize, utf8proc_option_t options
  446. ) {
  447. return utf8proc_decompose_custom(str, strlen, buffer, bufsize, options, NULL, NULL);
  448. }
  449. UTF8PROC_DLLEXPORT utf8proc_ssize_t utf8proc_decompose_custom(
  450. const utf8proc_uint8_t *str, utf8proc_ssize_t strlen,
  451. utf8proc_int32_t *buffer, utf8proc_ssize_t bufsize, utf8proc_option_t options,
  452. utf8proc_custom_func custom_func, void *custom_data
  453. ) {
  454. /* strlen will be ignored, if UTF8PROC_NULLTERM is set in options */
  455. utf8proc_ssize_t wpos = 0;
  456. if ((options & UTF8PROC_COMPOSE) && (options & UTF8PROC_DECOMPOSE))
  457. return UTF8PROC_ERROR_INVALIDOPTS;
  458. if ((options & UTF8PROC_STRIPMARK) &&
  459. !(options & UTF8PROC_COMPOSE) && !(options & UTF8PROC_DECOMPOSE))
  460. return UTF8PROC_ERROR_INVALIDOPTS;
  461. {
  462. utf8proc_int32_t uc;
  463. utf8proc_ssize_t rpos = 0;
  464. utf8proc_ssize_t decomp_result;
  465. int boundclass = UTF8PROC_BOUNDCLASS_START;
  466. while (1) {
  467. if (options & UTF8PROC_NULLTERM) {
  468. rpos += utf8proc_iterate(str + rpos, -1, &uc);
  469. /* checking of return value is not necessary,
  470. as 'uc' is < 0 in case of error */
  471. if (uc < 0) return UTF8PROC_ERROR_INVALIDUTF8;
  472. if (rpos < 0) return UTF8PROC_ERROR_OVERFLOW;
  473. if (uc == 0) break;
  474. } else {
  475. if (rpos >= strlen) break;
  476. rpos += utf8proc_iterate(str + rpos, strlen - rpos, &uc);
  477. if (uc < 0) return UTF8PROC_ERROR_INVALIDUTF8;
  478. }
  479. if (custom_func != NULL) {
  480. uc = custom_func(uc, custom_data); /* user-specified custom mapping */
  481. }
  482. decomp_result = utf8proc_decompose_char(
  483. uc, buffer + wpos, (bufsize > wpos) ? (bufsize - wpos) : 0, options,
  484. &boundclass
  485. );
  486. if (decomp_result < 0) return decomp_result;
  487. wpos += decomp_result;
  488. /* prohibiting integer overflows due to too long strings: */
  489. if (wpos < 0 ||
  490. wpos > (utf8proc_ssize_t)(SSIZE_MAX/sizeof(utf8proc_int32_t)/2))
  491. return UTF8PROC_ERROR_OVERFLOW;
  492. }
  493. }
  494. if ((options & (UTF8PROC_COMPOSE|UTF8PROC_DECOMPOSE)) && bufsize >= wpos) {
  495. utf8proc_ssize_t pos = 0;
  496. while (pos < wpos-1) {
  497. utf8proc_int32_t uc1, uc2;
  498. const utf8proc_property_t *property1, *property2;
  499. uc1 = buffer[pos];
  500. uc2 = buffer[pos+1];
  501. property1 = unsafe_get_property(uc1);
  502. property2 = unsafe_get_property(uc2);
  503. if (property1->combining_class > property2->combining_class &&
  504. property2->combining_class > 0) {
  505. buffer[pos] = uc2;
  506. buffer[pos+1] = uc1;
  507. if (pos > 0) pos--; else pos++;
  508. } else {
  509. pos++;
  510. }
  511. }
  512. }
  513. return wpos;
  514. }
  515. UTF8PROC_DLLEXPORT utf8proc_ssize_t utf8proc_normalize_utf32(utf8proc_int32_t *buffer, utf8proc_ssize_t length, utf8proc_option_t options) {
  516. /* UTF8PROC_NULLTERM option will be ignored, 'length' is never ignored */
  517. if (options & (UTF8PROC_NLF2LS | UTF8PROC_NLF2PS | UTF8PROC_STRIPCC)) {
  518. utf8proc_ssize_t rpos;
  519. utf8proc_ssize_t wpos = 0;
  520. utf8proc_int32_t uc;
  521. for (rpos = 0; rpos < length; rpos++) {
  522. uc = buffer[rpos];
  523. if (uc == 0x000D && rpos < length-1 && buffer[rpos+1] == 0x000A) rpos++;
  524. if (uc == 0x000A || uc == 0x000D || uc == 0x0085 ||
  525. ((options & UTF8PROC_STRIPCC) && (uc == 0x000B || uc == 0x000C))) {
  526. if (options & UTF8PROC_NLF2LS) {
  527. if (options & UTF8PROC_NLF2PS) {
  528. buffer[wpos++] = 0x000A;
  529. } else {
  530. buffer[wpos++] = 0x2028;
  531. }
  532. } else {
  533. if (options & UTF8PROC_NLF2PS) {
  534. buffer[wpos++] = 0x2029;
  535. } else {
  536. buffer[wpos++] = 0x0020;
  537. }
  538. }
  539. } else if ((options & UTF8PROC_STRIPCC) &&
  540. (uc < 0x0020 || (uc >= 0x007F && uc < 0x00A0))) {
  541. if (uc == 0x0009) buffer[wpos++] = 0x0020;
  542. } else {
  543. buffer[wpos++] = uc;
  544. }
  545. }
  546. length = wpos;
  547. }
  548. if (options & UTF8PROC_COMPOSE) {
  549. utf8proc_int32_t *starter = NULL;
  550. utf8proc_int32_t current_char;
  551. const utf8proc_property_t *starter_property = NULL, *current_property;
  552. utf8proc_propval_t max_combining_class = -1;
  553. utf8proc_ssize_t rpos;
  554. utf8proc_ssize_t wpos = 0;
  555. utf8proc_int32_t composition;
  556. for (rpos = 0; rpos < length; rpos++) {
  557. current_char = buffer[rpos];
  558. current_property = unsafe_get_property(current_char);
  559. if (starter && current_property->combining_class > max_combining_class) {
  560. /* combination perhaps possible */
  561. utf8proc_int32_t hangul_lindex;
  562. utf8proc_int32_t hangul_sindex;
  563. hangul_lindex = *starter - UTF8PROC_HANGUL_LBASE;
  564. if (hangul_lindex >= 0 && hangul_lindex < UTF8PROC_HANGUL_LCOUNT) {
  565. utf8proc_int32_t hangul_vindex;
  566. hangul_vindex = current_char - UTF8PROC_HANGUL_VBASE;
  567. if (hangul_vindex >= 0 && hangul_vindex < UTF8PROC_HANGUL_VCOUNT) {
  568. *starter = UTF8PROC_HANGUL_SBASE +
  569. (hangul_lindex * UTF8PROC_HANGUL_VCOUNT + hangul_vindex) *
  570. UTF8PROC_HANGUL_TCOUNT;
  571. starter_property = NULL;
  572. continue;
  573. }
  574. }
  575. hangul_sindex = *starter - UTF8PROC_HANGUL_SBASE;
  576. if (hangul_sindex >= 0 && hangul_sindex < UTF8PROC_HANGUL_SCOUNT &&
  577. (hangul_sindex % UTF8PROC_HANGUL_TCOUNT) == 0) {
  578. utf8proc_int32_t hangul_tindex;
  579. hangul_tindex = current_char - UTF8PROC_HANGUL_TBASE;
  580. if (hangul_tindex >= 0 && hangul_tindex < UTF8PROC_HANGUL_TCOUNT) {
  581. *starter += hangul_tindex;
  582. starter_property = NULL;
  583. continue;
  584. }
  585. }
  586. if (!starter_property) {
  587. starter_property = unsafe_get_property(*starter);
  588. }
  589. if (starter_property->comb_index < 0x8000 &&
  590. current_property->comb_index != UINT16_MAX &&
  591. current_property->comb_index >= 0x8000) {
  592. int sidx = starter_property->comb_index;
  593. int idx = current_property->comb_index & 0x3FFF;
  594. if (idx >= utf8proc_combinations[sidx] && idx <= utf8proc_combinations[sidx + 1] ) {
  595. idx += sidx + 2 - utf8proc_combinations[sidx];
  596. if (current_property->comb_index & 0x4000) {
  597. composition = (utf8proc_combinations[idx] << 16) | utf8proc_combinations[idx+1];
  598. } else
  599. composition = utf8proc_combinations[idx];
  600. if (composition > 0 && (!(options & UTF8PROC_STABLE) ||
  601. !(unsafe_get_property(composition)->comp_exclusion))) {
  602. *starter = composition;
  603. starter_property = NULL;
  604. continue;
  605. }
  606. }
  607. }
  608. }
  609. buffer[wpos] = current_char;
  610. if (current_property->combining_class) {
  611. if (current_property->combining_class > max_combining_class) {
  612. max_combining_class = current_property->combining_class;
  613. }
  614. } else {
  615. starter = buffer + wpos;
  616. starter_property = NULL;
  617. max_combining_class = -1;
  618. }
  619. wpos++;
  620. }
  621. length = wpos;
  622. }
  623. return length;
  624. }
  625. UTF8PROC_DLLEXPORT utf8proc_ssize_t utf8proc_reencode(utf8proc_int32_t *buffer, utf8proc_ssize_t length, utf8proc_option_t options) {
  626. /* UTF8PROC_NULLTERM option will be ignored, 'length' is never ignored
  627. ASSERT: 'buffer' has one spare byte of free space at the end! */
  628. length = utf8proc_normalize_utf32(buffer, length, options);
  629. if (length < 0) return length;
  630. {
  631. utf8proc_ssize_t rpos, wpos = 0;
  632. utf8proc_int32_t uc;
  633. if (options & UTF8PROC_CHARBOUND) {
  634. for (rpos = 0; rpos < length; rpos++) {
  635. uc = buffer[rpos];
  636. wpos += unsafe_encode_char(uc, ((utf8proc_uint8_t *)buffer) + wpos);
  637. }
  638. } else {
  639. for (rpos = 0; rpos < length; rpos++) {
  640. uc = buffer[rpos];
  641. wpos += utf8proc_encode_char(uc, ((utf8proc_uint8_t *)buffer) + wpos);
  642. }
  643. }
  644. ((utf8proc_uint8_t *)buffer)[wpos] = 0;
  645. return wpos;
  646. }
  647. }
  648. UTF8PROC_DLLEXPORT utf8proc_ssize_t utf8proc_map(
  649. const utf8proc_uint8_t *str, utf8proc_ssize_t strlen, utf8proc_uint8_t **dstptr, utf8proc_option_t options
  650. ) {
  651. return utf8proc_map_custom(str, strlen, dstptr, options, NULL, NULL);
  652. }
  653. UTF8PROC_DLLEXPORT utf8proc_ssize_t utf8proc_map_custom(
  654. const utf8proc_uint8_t *str, utf8proc_ssize_t strlen, utf8proc_uint8_t **dstptr, utf8proc_option_t options,
  655. utf8proc_custom_func custom_func, void *custom_data
  656. ) {
  657. utf8proc_int32_t *buffer;
  658. utf8proc_ssize_t result;
  659. *dstptr = NULL;
  660. result = utf8proc_decompose_custom(str, strlen, NULL, 0, options, custom_func, custom_data);
  661. if (result < 0) return result;
  662. buffer = (utf8proc_int32_t *) malloc(result * sizeof(utf8proc_int32_t) + 1);
  663. if (!buffer) return UTF8PROC_ERROR_NOMEM;
  664. result = utf8proc_decompose_custom(str, strlen, buffer, result, options, custom_func, custom_data);
  665. if (result < 0) {
  666. free(buffer);
  667. return result;
  668. }
  669. result = utf8proc_reencode(buffer, result, options);
  670. if (result < 0) {
  671. free(buffer);
  672. return result;
  673. }
  674. {
  675. utf8proc_int32_t *newptr;
  676. newptr = (utf8proc_int32_t *) realloc(buffer, (size_t)result+1);
  677. if (newptr) buffer = newptr;
  678. }
  679. *dstptr = (utf8proc_uint8_t *)buffer;
  680. return result;
  681. }
  682. UTF8PROC_DLLEXPORT utf8proc_uint8_t *utf8proc_NFD(const utf8proc_uint8_t *str) {
  683. utf8proc_uint8_t *retval;
  684. utf8proc_map(str, 0, &retval, UTF8PROC_NULLTERM | UTF8PROC_STABLE |
  685. UTF8PROC_DECOMPOSE);
  686. return retval;
  687. }
  688. UTF8PROC_DLLEXPORT utf8proc_uint8_t *utf8proc_NFC(const utf8proc_uint8_t *str) {
  689. utf8proc_uint8_t *retval;
  690. utf8proc_map(str, 0, &retval, UTF8PROC_NULLTERM | UTF8PROC_STABLE |
  691. UTF8PROC_COMPOSE);
  692. return retval;
  693. }
  694. UTF8PROC_DLLEXPORT utf8proc_uint8_t *utf8proc_NFKD(const utf8proc_uint8_t *str) {
  695. utf8proc_uint8_t *retval;
  696. utf8proc_map(str, 0, &retval, UTF8PROC_NULLTERM | UTF8PROC_STABLE |
  697. UTF8PROC_DECOMPOSE | UTF8PROC_COMPAT);
  698. return retval;
  699. }
  700. UTF8PROC_DLLEXPORT utf8proc_uint8_t *utf8proc_NFKC(const utf8proc_uint8_t *str) {
  701. utf8proc_uint8_t *retval;
  702. utf8proc_map(str, 0, &retval, UTF8PROC_NULLTERM | UTF8PROC_STABLE |
  703. UTF8PROC_COMPOSE | UTF8PROC_COMPAT);
  704. return retval;
  705. }