utf8proc.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792
  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 "15.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, utf8proc_int32_t *state)
  267. {
  268. if (state) {
  269. int lbc_override;
  270. if (*state == UTF8PROC_BOUNDCLASS_START)
  271. *state = lbc_override = lbc;
  272. else
  273. lbc_override = *state;
  274. utf8proc_bool break_permitted = grapheme_break_simple(lbc_override, tbc);
  275. // Special support for GB 12/13 made possible by GB999. After two RI
  276. // class codepoints we want to force a break. Do this by resetting the
  277. // second RI's bound class to UTF8PROC_BOUNDCLASS_OTHER, to force a break
  278. // after that character according to GB999 (unless of course such a break is
  279. // forbidden by a different rule such as GB9).
  280. if (*state == tbc && tbc == UTF8PROC_BOUNDCLASS_REGIONAL_INDICATOR)
  281. *state = UTF8PROC_BOUNDCLASS_OTHER;
  282. // Special support for GB11 (emoji extend* zwj / emoji)
  283. else if (*state == UTF8PROC_BOUNDCLASS_EXTENDED_PICTOGRAPHIC) {
  284. if (tbc == UTF8PROC_BOUNDCLASS_EXTEND) // fold EXTEND codepoints into emoji
  285. *state = UTF8PROC_BOUNDCLASS_EXTENDED_PICTOGRAPHIC;
  286. else if (tbc == UTF8PROC_BOUNDCLASS_ZWJ)
  287. *state = UTF8PROC_BOUNDCLASS_E_ZWG; // state to record emoji+zwg combo
  288. else
  289. *state = tbc;
  290. }
  291. else
  292. *state = tbc;
  293. return break_permitted;
  294. }
  295. else
  296. return grapheme_break_simple(lbc, tbc);
  297. }
  298. UTF8PROC_DLLEXPORT utf8proc_bool utf8proc_grapheme_break_stateful(
  299. utf8proc_int32_t c1, utf8proc_int32_t c2, utf8proc_int32_t *state) {
  300. return grapheme_break_extended(utf8proc_get_property(c1)->boundclass,
  301. utf8proc_get_property(c2)->boundclass,
  302. state);
  303. }
  304. UTF8PROC_DLLEXPORT utf8proc_bool utf8proc_grapheme_break(
  305. utf8proc_int32_t c1, utf8proc_int32_t c2) {
  306. return utf8proc_grapheme_break_stateful(c1, c2, NULL);
  307. }
  308. static utf8proc_int32_t seqindex_decode_entry(const utf8proc_uint16_t **entry)
  309. {
  310. utf8proc_int32_t entry_cp = **entry;
  311. if ((entry_cp & 0xF800) == 0xD800) {
  312. *entry = *entry + 1;
  313. entry_cp = ((entry_cp & 0x03FF) << 10) | (**entry & 0x03FF);
  314. entry_cp += 0x10000;
  315. }
  316. return entry_cp;
  317. }
  318. static utf8proc_int32_t seqindex_decode_index(const utf8proc_uint32_t seqindex)
  319. {
  320. const utf8proc_uint16_t *entry = &utf8proc_sequences[seqindex];
  321. return seqindex_decode_entry(&entry);
  322. }
  323. 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) {
  324. utf8proc_ssize_t written = 0;
  325. const utf8proc_uint16_t *entry = &utf8proc_sequences[seqindex & 0x3FFF];
  326. int len = seqindex >> 14;
  327. if (len >= 3) {
  328. len = *entry;
  329. entry++;
  330. }
  331. for (; len >= 0; entry++, len--) {
  332. utf8proc_int32_t entry_cp = seqindex_decode_entry(&entry);
  333. written += utf8proc_decompose_char(entry_cp, dst+written,
  334. (bufsize > written) ? (bufsize - written) : 0, options,
  335. last_boundclass);
  336. if (written < 0) return UTF8PROC_ERROR_OVERFLOW;
  337. }
  338. return written;
  339. }
  340. UTF8PROC_DLLEXPORT utf8proc_int32_t utf8proc_tolower(utf8proc_int32_t c)
  341. {
  342. utf8proc_int32_t cl = utf8proc_get_property(c)->lowercase_seqindex;
  343. return cl != UINT16_MAX ? seqindex_decode_index((utf8proc_uint32_t)cl) : c;
  344. }
  345. UTF8PROC_DLLEXPORT utf8proc_int32_t utf8proc_toupper(utf8proc_int32_t c)
  346. {
  347. utf8proc_int32_t cu = utf8proc_get_property(c)->uppercase_seqindex;
  348. return cu != UINT16_MAX ? seqindex_decode_index((utf8proc_uint32_t)cu) : c;
  349. }
  350. UTF8PROC_DLLEXPORT utf8proc_int32_t utf8proc_totitle(utf8proc_int32_t c)
  351. {
  352. utf8proc_int32_t cu = utf8proc_get_property(c)->titlecase_seqindex;
  353. return cu != UINT16_MAX ? seqindex_decode_index((utf8proc_uint32_t)cu) : c;
  354. }
  355. UTF8PROC_DLLEXPORT int utf8proc_islower(utf8proc_int32_t c)
  356. {
  357. const utf8proc_property_t *p = utf8proc_get_property(c);
  358. return p->lowercase_seqindex != p->uppercase_seqindex && p->lowercase_seqindex == UINT16_MAX;
  359. }
  360. UTF8PROC_DLLEXPORT int utf8proc_isupper(utf8proc_int32_t c)
  361. {
  362. const utf8proc_property_t *p = utf8proc_get_property(c);
  363. return p->lowercase_seqindex != p->uppercase_seqindex && p->uppercase_seqindex == UINT16_MAX && p->category != UTF8PROC_CATEGORY_LT;
  364. }
  365. /* return a character width analogous to wcwidth (except portable and
  366. hopefully less buggy than most system wcwidth functions). */
  367. UTF8PROC_DLLEXPORT int utf8proc_charwidth(utf8proc_int32_t c) {
  368. return utf8proc_get_property(c)->charwidth;
  369. }
  370. UTF8PROC_DLLEXPORT utf8proc_category_t utf8proc_category(utf8proc_int32_t c) {
  371. return (utf8proc_category_t) utf8proc_get_property(c)->category;
  372. }
  373. UTF8PROC_DLLEXPORT const char *utf8proc_category_string(utf8proc_int32_t c) {
  374. 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"};
  375. return s[utf8proc_category(c)];
  376. }
  377. #define utf8proc_decompose_lump(replacement_uc) \
  378. return utf8proc_decompose_char((replacement_uc), dst, bufsize, \
  379. options & ~(unsigned int)UTF8PROC_LUMP, last_boundclass)
  380. 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) {
  381. const utf8proc_property_t *property;
  382. utf8proc_propval_t category;
  383. utf8proc_int32_t hangul_sindex;
  384. if (uc < 0 || uc >= 0x110000) return UTF8PROC_ERROR_NOTASSIGNED;
  385. property = unsafe_get_property(uc);
  386. category = property->category;
  387. hangul_sindex = uc - UTF8PROC_HANGUL_SBASE;
  388. if (options & (UTF8PROC_COMPOSE|UTF8PROC_DECOMPOSE)) {
  389. if (hangul_sindex >= 0 && hangul_sindex < UTF8PROC_HANGUL_SCOUNT) {
  390. utf8proc_int32_t hangul_tindex;
  391. if (bufsize >= 1) {
  392. dst[0] = UTF8PROC_HANGUL_LBASE +
  393. hangul_sindex / UTF8PROC_HANGUL_NCOUNT;
  394. if (bufsize >= 2) dst[1] = UTF8PROC_HANGUL_VBASE +
  395. (hangul_sindex % UTF8PROC_HANGUL_NCOUNT) / UTF8PROC_HANGUL_TCOUNT;
  396. }
  397. hangul_tindex = hangul_sindex % UTF8PROC_HANGUL_TCOUNT;
  398. if (!hangul_tindex) return 2;
  399. if (bufsize >= 3) dst[2] = UTF8PROC_HANGUL_TBASE + hangul_tindex;
  400. return 3;
  401. }
  402. }
  403. if (options & UTF8PROC_REJECTNA) {
  404. if (!category) return UTF8PROC_ERROR_NOTASSIGNED;
  405. }
  406. if (options & UTF8PROC_IGNORE) {
  407. if (property->ignorable) return 0;
  408. }
  409. if (options & UTF8PROC_STRIPNA) {
  410. if (!category) return 0;
  411. }
  412. if (options & UTF8PROC_LUMP) {
  413. if (category == UTF8PROC_CATEGORY_ZS) utf8proc_decompose_lump(0x0020);
  414. if (uc == 0x2018 || uc == 0x2019 || uc == 0x02BC || uc == 0x02C8)
  415. utf8proc_decompose_lump(0x0027);
  416. if (category == UTF8PROC_CATEGORY_PD || uc == 0x2212)
  417. utf8proc_decompose_lump(0x002D);
  418. if (uc == 0x2044 || uc == 0x2215) utf8proc_decompose_lump(0x002F);
  419. if (uc == 0x2236) utf8proc_decompose_lump(0x003A);
  420. if (uc == 0x2039 || uc == 0x2329 || uc == 0x3008)
  421. utf8proc_decompose_lump(0x003C);
  422. if (uc == 0x203A || uc == 0x232A || uc == 0x3009)
  423. utf8proc_decompose_lump(0x003E);
  424. if (uc == 0x2216) utf8proc_decompose_lump(0x005C);
  425. if (uc == 0x02C4 || uc == 0x02C6 || uc == 0x2038 || uc == 0x2303)
  426. utf8proc_decompose_lump(0x005E);
  427. if (category == UTF8PROC_CATEGORY_PC || uc == 0x02CD)
  428. utf8proc_decompose_lump(0x005F);
  429. if (uc == 0x02CB) utf8proc_decompose_lump(0x0060);
  430. if (uc == 0x2223) utf8proc_decompose_lump(0x007C);
  431. if (uc == 0x223C) utf8proc_decompose_lump(0x007E);
  432. if ((options & UTF8PROC_NLF2LS) && (options & UTF8PROC_NLF2PS)) {
  433. if (category == UTF8PROC_CATEGORY_ZL ||
  434. category == UTF8PROC_CATEGORY_ZP)
  435. utf8proc_decompose_lump(0x000A);
  436. }
  437. }
  438. if (options & UTF8PROC_STRIPMARK) {
  439. if (category == UTF8PROC_CATEGORY_MN ||
  440. category == UTF8PROC_CATEGORY_MC ||
  441. category == UTF8PROC_CATEGORY_ME) return 0;
  442. }
  443. if (options & UTF8PROC_CASEFOLD) {
  444. if (property->casefold_seqindex != UINT16_MAX) {
  445. return seqindex_write_char_decomposed(property->casefold_seqindex, dst, bufsize, options, last_boundclass);
  446. }
  447. }
  448. if (options & (UTF8PROC_COMPOSE|UTF8PROC_DECOMPOSE)) {
  449. if (property->decomp_seqindex != UINT16_MAX &&
  450. (!property->decomp_type || (options & UTF8PROC_COMPAT))) {
  451. return seqindex_write_char_decomposed(property->decomp_seqindex, dst, bufsize, options, last_boundclass);
  452. }
  453. }
  454. if (options & UTF8PROC_CHARBOUND) {
  455. utf8proc_bool boundary;
  456. int tbc = property->boundclass;
  457. boundary = grapheme_break_extended(*last_boundclass, tbc, last_boundclass);
  458. if (boundary) {
  459. if (bufsize >= 1) dst[0] = -1; /* sentinel value for grapheme break */
  460. if (bufsize >= 2) dst[1] = uc;
  461. return 2;
  462. }
  463. }
  464. if (bufsize >= 1) *dst = uc;
  465. return 1;
  466. }
  467. UTF8PROC_DLLEXPORT utf8proc_ssize_t utf8proc_decompose(
  468. const utf8proc_uint8_t *str, utf8proc_ssize_t strlen,
  469. utf8proc_int32_t *buffer, utf8proc_ssize_t bufsize, utf8proc_option_t options
  470. ) {
  471. return utf8proc_decompose_custom(str, strlen, buffer, bufsize, options, NULL, NULL);
  472. }
  473. UTF8PROC_DLLEXPORT utf8proc_ssize_t utf8proc_decompose_custom(
  474. const utf8proc_uint8_t *str, utf8proc_ssize_t strlen,
  475. utf8proc_int32_t *buffer, utf8proc_ssize_t bufsize, utf8proc_option_t options,
  476. utf8proc_custom_func custom_func, void *custom_data
  477. ) {
  478. /* strlen will be ignored, if UTF8PROC_NULLTERM is set in options */
  479. utf8proc_ssize_t wpos = 0;
  480. if ((options & UTF8PROC_COMPOSE) && (options & UTF8PROC_DECOMPOSE))
  481. return UTF8PROC_ERROR_INVALIDOPTS;
  482. if ((options & UTF8PROC_STRIPMARK) &&
  483. !(options & UTF8PROC_COMPOSE) && !(options & UTF8PROC_DECOMPOSE))
  484. return UTF8PROC_ERROR_INVALIDOPTS;
  485. {
  486. utf8proc_int32_t uc;
  487. utf8proc_ssize_t rpos = 0;
  488. utf8proc_ssize_t decomp_result;
  489. int boundclass = UTF8PROC_BOUNDCLASS_START;
  490. while (1) {
  491. if (options & UTF8PROC_NULLTERM) {
  492. rpos += utf8proc_iterate(str + rpos, -1, &uc);
  493. /* checking of return value is not necessary,
  494. as 'uc' is < 0 in case of error */
  495. if (uc < 0) return UTF8PROC_ERROR_INVALIDUTF8;
  496. if (rpos < 0) return UTF8PROC_ERROR_OVERFLOW;
  497. if (uc == 0) break;
  498. } else {
  499. if (rpos >= strlen) break;
  500. rpos += utf8proc_iterate(str + rpos, strlen - rpos, &uc);
  501. if (uc < 0) return UTF8PROC_ERROR_INVALIDUTF8;
  502. }
  503. if (custom_func != NULL) {
  504. uc = custom_func(uc, custom_data); /* user-specified custom mapping */
  505. }
  506. decomp_result = utf8proc_decompose_char(
  507. uc, buffer + wpos, (bufsize > wpos) ? (bufsize - wpos) : 0, options,
  508. &boundclass
  509. );
  510. if (decomp_result < 0) return decomp_result;
  511. wpos += decomp_result;
  512. /* prohibiting integer overflows due to too long strings: */
  513. if (wpos < 0 ||
  514. wpos > (utf8proc_ssize_t)(SSIZE_MAX/sizeof(utf8proc_int32_t)/2))
  515. return UTF8PROC_ERROR_OVERFLOW;
  516. }
  517. }
  518. if ((options & (UTF8PROC_COMPOSE|UTF8PROC_DECOMPOSE)) && bufsize >= wpos) {
  519. utf8proc_ssize_t pos = 0;
  520. while (pos < wpos-1) {
  521. utf8proc_int32_t uc1, uc2;
  522. const utf8proc_property_t *property1, *property2;
  523. uc1 = buffer[pos];
  524. uc2 = buffer[pos+1];
  525. property1 = unsafe_get_property(uc1);
  526. property2 = unsafe_get_property(uc2);
  527. if (property1->combining_class > property2->combining_class &&
  528. property2->combining_class > 0) {
  529. buffer[pos] = uc2;
  530. buffer[pos+1] = uc1;
  531. if (pos > 0) pos--; else pos++;
  532. } else {
  533. pos++;
  534. }
  535. }
  536. }
  537. return wpos;
  538. }
  539. UTF8PROC_DLLEXPORT utf8proc_ssize_t utf8proc_normalize_utf32(utf8proc_int32_t *buffer, utf8proc_ssize_t length, utf8proc_option_t options) {
  540. /* UTF8PROC_NULLTERM option will be ignored, 'length' is never ignored */
  541. if (options & (UTF8PROC_NLF2LS | UTF8PROC_NLF2PS | UTF8PROC_STRIPCC)) {
  542. utf8proc_ssize_t rpos;
  543. utf8proc_ssize_t wpos = 0;
  544. utf8proc_int32_t uc;
  545. for (rpos = 0; rpos < length; rpos++) {
  546. uc = buffer[rpos];
  547. if (uc == 0x000D && rpos < length-1 && buffer[rpos+1] == 0x000A) rpos++;
  548. if (uc == 0x000A || uc == 0x000D || uc == 0x0085 ||
  549. ((options & UTF8PROC_STRIPCC) && (uc == 0x000B || uc == 0x000C))) {
  550. if (options & UTF8PROC_NLF2LS) {
  551. if (options & UTF8PROC_NLF2PS) {
  552. buffer[wpos++] = 0x000A;
  553. } else {
  554. buffer[wpos++] = 0x2028;
  555. }
  556. } else {
  557. if (options & UTF8PROC_NLF2PS) {
  558. buffer[wpos++] = 0x2029;
  559. } else {
  560. buffer[wpos++] = 0x0020;
  561. }
  562. }
  563. } else if ((options & UTF8PROC_STRIPCC) &&
  564. (uc < 0x0020 || (uc >= 0x007F && uc < 0x00A0))) {
  565. if (uc == 0x0009) buffer[wpos++] = 0x0020;
  566. } else {
  567. buffer[wpos++] = uc;
  568. }
  569. }
  570. length = wpos;
  571. }
  572. if (options & UTF8PROC_COMPOSE) {
  573. utf8proc_int32_t *starter = NULL;
  574. utf8proc_int32_t current_char;
  575. const utf8proc_property_t *starter_property = NULL, *current_property;
  576. utf8proc_propval_t max_combining_class = -1;
  577. utf8proc_ssize_t rpos;
  578. utf8proc_ssize_t wpos = 0;
  579. utf8proc_int32_t composition;
  580. for (rpos = 0; rpos < length; rpos++) {
  581. current_char = buffer[rpos];
  582. current_property = unsafe_get_property(current_char);
  583. if (starter && current_property->combining_class > max_combining_class) {
  584. /* combination perhaps possible */
  585. utf8proc_int32_t hangul_lindex;
  586. utf8proc_int32_t hangul_sindex;
  587. hangul_lindex = *starter - UTF8PROC_HANGUL_LBASE;
  588. if (hangul_lindex >= 0 && hangul_lindex < UTF8PROC_HANGUL_LCOUNT) {
  589. utf8proc_int32_t hangul_vindex;
  590. hangul_vindex = current_char - UTF8PROC_HANGUL_VBASE;
  591. if (hangul_vindex >= 0 && hangul_vindex < UTF8PROC_HANGUL_VCOUNT) {
  592. *starter = UTF8PROC_HANGUL_SBASE +
  593. (hangul_lindex * UTF8PROC_HANGUL_VCOUNT + hangul_vindex) *
  594. UTF8PROC_HANGUL_TCOUNT;
  595. starter_property = NULL;
  596. continue;
  597. }
  598. }
  599. hangul_sindex = *starter - UTF8PROC_HANGUL_SBASE;
  600. if (hangul_sindex >= 0 && hangul_sindex < UTF8PROC_HANGUL_SCOUNT &&
  601. (hangul_sindex % UTF8PROC_HANGUL_TCOUNT) == 0) {
  602. utf8proc_int32_t hangul_tindex;
  603. hangul_tindex = current_char - UTF8PROC_HANGUL_TBASE;
  604. if (hangul_tindex >= 0 && hangul_tindex < UTF8PROC_HANGUL_TCOUNT) {
  605. *starter += hangul_tindex;
  606. starter_property = NULL;
  607. continue;
  608. }
  609. }
  610. if (!starter_property) {
  611. starter_property = unsafe_get_property(*starter);
  612. }
  613. if (starter_property->comb_index < 0x8000 &&
  614. current_property->comb_index != UINT16_MAX &&
  615. current_property->comb_index >= 0x8000) {
  616. int sidx = starter_property->comb_index;
  617. int idx = current_property->comb_index & 0x3FFF;
  618. if (idx >= utf8proc_combinations[sidx] && idx <= utf8proc_combinations[sidx + 1] ) {
  619. idx += sidx + 2 - utf8proc_combinations[sidx];
  620. if (current_property->comb_index & 0x4000) {
  621. composition = (utf8proc_combinations[idx] << 16) | utf8proc_combinations[idx+1];
  622. } else
  623. composition = utf8proc_combinations[idx];
  624. if (composition > 0 && (!(options & UTF8PROC_STABLE) ||
  625. !(unsafe_get_property(composition)->comp_exclusion))) {
  626. *starter = composition;
  627. starter_property = NULL;
  628. continue;
  629. }
  630. }
  631. }
  632. }
  633. buffer[wpos] = current_char;
  634. if (current_property->combining_class) {
  635. if (current_property->combining_class > max_combining_class) {
  636. max_combining_class = current_property->combining_class;
  637. }
  638. } else {
  639. starter = buffer + wpos;
  640. starter_property = NULL;
  641. max_combining_class = -1;
  642. }
  643. wpos++;
  644. }
  645. length = wpos;
  646. }
  647. return length;
  648. }
  649. UTF8PROC_DLLEXPORT utf8proc_ssize_t utf8proc_reencode(utf8proc_int32_t *buffer, utf8proc_ssize_t length, utf8proc_option_t options) {
  650. /* UTF8PROC_NULLTERM option will be ignored, 'length' is never ignored
  651. ASSERT: 'buffer' has one spare byte of free space at the end! */
  652. length = utf8proc_normalize_utf32(buffer, length, options);
  653. if (length < 0) return length;
  654. {
  655. utf8proc_ssize_t rpos, wpos = 0;
  656. utf8proc_int32_t uc;
  657. if (options & UTF8PROC_CHARBOUND) {
  658. for (rpos = 0; rpos < length; rpos++) {
  659. uc = buffer[rpos];
  660. wpos += charbound_encode_char(uc, ((utf8proc_uint8_t *)buffer) + wpos);
  661. }
  662. } else {
  663. for (rpos = 0; rpos < length; rpos++) {
  664. uc = buffer[rpos];
  665. wpos += utf8proc_encode_char(uc, ((utf8proc_uint8_t *)buffer) + wpos);
  666. }
  667. }
  668. ((utf8proc_uint8_t *)buffer)[wpos] = 0;
  669. return wpos;
  670. }
  671. }
  672. UTF8PROC_DLLEXPORT utf8proc_ssize_t utf8proc_map(
  673. const utf8proc_uint8_t *str, utf8proc_ssize_t strlen, utf8proc_uint8_t **dstptr, utf8proc_option_t options
  674. ) {
  675. return utf8proc_map_custom(str, strlen, dstptr, options, NULL, NULL);
  676. }
  677. UTF8PROC_DLLEXPORT utf8proc_ssize_t utf8proc_map_custom(
  678. const utf8proc_uint8_t *str, utf8proc_ssize_t strlen, utf8proc_uint8_t **dstptr, utf8proc_option_t options,
  679. utf8proc_custom_func custom_func, void *custom_data
  680. ) {
  681. utf8proc_int32_t *buffer;
  682. utf8proc_ssize_t result;
  683. *dstptr = NULL;
  684. result = utf8proc_decompose_custom(str, strlen, NULL, 0, options, custom_func, custom_data);
  685. if (result < 0) return result;
  686. buffer = (utf8proc_int32_t *) malloc(((utf8proc_size_t)result) * sizeof(utf8proc_int32_t) + 1);
  687. if (!buffer) return UTF8PROC_ERROR_NOMEM;
  688. result = utf8proc_decompose_custom(str, strlen, buffer, result, options, custom_func, custom_data);
  689. if (result < 0) {
  690. free(buffer);
  691. return result;
  692. }
  693. result = utf8proc_reencode(buffer, result, options);
  694. if (result < 0) {
  695. free(buffer);
  696. return result;
  697. }
  698. {
  699. utf8proc_int32_t *newptr;
  700. newptr = (utf8proc_int32_t *) realloc(buffer, (size_t)result+1);
  701. if (newptr) buffer = newptr;
  702. }
  703. *dstptr = (utf8proc_uint8_t *)buffer;
  704. return result;
  705. }
  706. UTF8PROC_DLLEXPORT utf8proc_uint8_t *utf8proc_NFD(const utf8proc_uint8_t *str) {
  707. utf8proc_uint8_t *retval;
  708. utf8proc_map(str, 0, &retval, UTF8PROC_NULLTERM | UTF8PROC_STABLE |
  709. UTF8PROC_DECOMPOSE);
  710. return retval;
  711. }
  712. UTF8PROC_DLLEXPORT utf8proc_uint8_t *utf8proc_NFC(const utf8proc_uint8_t *str) {
  713. utf8proc_uint8_t *retval;
  714. utf8proc_map(str, 0, &retval, UTF8PROC_NULLTERM | UTF8PROC_STABLE |
  715. UTF8PROC_COMPOSE);
  716. return retval;
  717. }
  718. UTF8PROC_DLLEXPORT utf8proc_uint8_t *utf8proc_NFKD(const utf8proc_uint8_t *str) {
  719. utf8proc_uint8_t *retval;
  720. utf8proc_map(str, 0, &retval, UTF8PROC_NULLTERM | UTF8PROC_STABLE |
  721. UTF8PROC_DECOMPOSE | UTF8PROC_COMPAT);
  722. return retval;
  723. }
  724. UTF8PROC_DLLEXPORT utf8proc_uint8_t *utf8proc_NFKC(const utf8proc_uint8_t *str) {
  725. utf8proc_uint8_t *retval;
  726. utf8proc_map(str, 0, &retval, UTF8PROC_NULLTERM | UTF8PROC_STABLE |
  727. UTF8PROC_COMPOSE | UTF8PROC_COMPAT);
  728. return retval;
  729. }
  730. UTF8PROC_DLLEXPORT utf8proc_uint8_t *utf8proc_NFKC_Casefold(const utf8proc_uint8_t *str) {
  731. utf8proc_uint8_t *retval;
  732. utf8proc_map(str, 0, &retval, UTF8PROC_NULLTERM | UTF8PROC_STABLE |
  733. UTF8PROC_COMPOSE | UTF8PROC_COMPAT | UTF8PROC_CASEFOLD | UTF8PROC_IGNORE);
  734. return retval;
  735. }