unicode.c 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * Unicode support routines.
  4. *
  5. * Copyright (C) 2009 Denys Vlasenko
  6. *
  7. * Licensed under GPLv2, see file LICENSE in this source tree.
  8. */
  9. #include "libbb.h"
  10. #include "unicode.h"
  11. /* If it's not #defined as a constant in unicode.h... */
  12. #ifndef unicode_status
  13. uint8_t unicode_status;
  14. #endif
  15. /* This file is compiled only if UNICODE_SUPPORT is on.
  16. * We check other options and decide whether to use libc support
  17. * via locale, or use our own logic:
  18. */
  19. #if ENABLE_UNICODE_USING_LOCALE
  20. /* Unicode support using libc locale support. */
  21. void FAST_FUNC reinit_unicode(const char *LANG)
  22. {
  23. static const char unicode_0x394[] = { 0xce, 0x94, 0 };
  24. size_t width;
  25. //TODO: avoid repeated calls by caching last string?
  26. setlocale(LC_ALL, (LANG && LANG[0]) ? LANG : "C");
  27. /* In unicode, this is a one character string */
  28. // can use unicode_strlen(string) too, but otherwise unicode_strlen() is unused
  29. width = mbstowcs(NULL, unicode_0x394, INT_MAX);
  30. unicode_status = (width == 1 ? UNICODE_ON : UNICODE_OFF);
  31. }
  32. void FAST_FUNC init_unicode(void)
  33. {
  34. if (unicode_status == UNICODE_UNKNOWN)
  35. reinit_unicode(getenv("LANG"));
  36. }
  37. #else
  38. /* Homegrown Unicode support. It knows only C and Unicode locales. */
  39. # if ENABLE_FEATURE_CHECK_UNICODE_IN_ENV
  40. void FAST_FUNC reinit_unicode(const char *LANG)
  41. {
  42. unicode_status = UNICODE_OFF;
  43. if (!LANG || !(strstr(LANG, ".utf") || strstr(LANG, ".UTF")))
  44. return;
  45. unicode_status = UNICODE_ON;
  46. }
  47. void FAST_FUNC init_unicode(void)
  48. {
  49. if (unicode_status == UNICODE_UNKNOWN)
  50. reinit_unicode(getenv("LANG"));
  51. }
  52. # endif
  53. static size_t wcrtomb_internal(char *s, wchar_t wc)
  54. {
  55. int n, i;
  56. uint32_t v = wc;
  57. if (v <= 0x7f) {
  58. *s = v;
  59. return 1;
  60. }
  61. /* RFC 3629 says that Unicode ends at 10FFFF,
  62. * but we cover entire 32 bits */
  63. /* 4000000-FFFFFFFF -> 111111tt 10tttttt 10zzzzzz 10zzyyyy 10yyyyxx 10xxxxxx */
  64. /* 200000-3FFFFFF -> 111110tt 10zzzzzz 10zzyyyy 10yyyyxx 10xxxxxx */
  65. /* 10000-1FFFFF -> 11110zzz 10zzyyyy 10yyyyxx 10xxxxxx */
  66. /* 800-FFFF -> 1110yyyy 10yyyyxx 10xxxxxx */
  67. /* 80-7FF -> 110yyyxx 10xxxxxx */
  68. /* How many bytes do we need? */
  69. n = 2;
  70. /* (0x80000000+ would result in n = 7, limiting n to 6) */
  71. while (v >= 0x800 && n < 6) {
  72. v >>= 5;
  73. n++;
  74. }
  75. /* Fill bytes n-1..1 */
  76. i = n;
  77. while (--i) {
  78. s[i] = (wc & 0x3f) | 0x80;
  79. wc >>= 6;
  80. }
  81. /* Fill byte 0 */
  82. s[0] = wc | (uint8_t)(0x3f00 >> n);
  83. return n;
  84. }
  85. size_t FAST_FUNC wcrtomb(char *s, wchar_t wc, mbstate_t *ps UNUSED_PARAM)
  86. {
  87. if (unicode_status != UNICODE_ON) {
  88. *s = wc;
  89. return 1;
  90. }
  91. return wcrtomb_internal(s, wc);
  92. }
  93. size_t FAST_FUNC wcstombs(char *dest, const wchar_t *src, size_t n)
  94. {
  95. size_t org_n = n;
  96. if (unicode_status != UNICODE_ON) {
  97. while (n) {
  98. wchar_t c = *src++;
  99. *dest++ = c;
  100. if (c == 0)
  101. break;
  102. n--;
  103. }
  104. return org_n - n;
  105. }
  106. while (n >= MB_CUR_MAX) {
  107. wchar_t wc = *src++;
  108. size_t len = wcrtomb_internal(dest, wc);
  109. if (wc == L'\0')
  110. return org_n - n;
  111. dest += len;
  112. n -= len;
  113. }
  114. while (n) {
  115. char tbuf[MB_CUR_MAX];
  116. wchar_t wc = *src++;
  117. size_t len = wcrtomb_internal(tbuf, wc);
  118. if (len > n)
  119. break;
  120. memcpy(dest, tbuf, len);
  121. if (wc == L'\0')
  122. return org_n - n;
  123. dest += len;
  124. n -= len;
  125. }
  126. return org_n - n;
  127. }
  128. # define ERROR_WCHAR (~(wchar_t)0)
  129. static const char *mbstowc_internal(wchar_t *res, const char *src)
  130. {
  131. int bytes;
  132. unsigned c = (unsigned char) *src++;
  133. if (c <= 0x7f) {
  134. *res = c;
  135. return src;
  136. }
  137. /* 80-7FF -> 110yyyxx 10xxxxxx */
  138. /* 800-FFFF -> 1110yyyy 10yyyyxx 10xxxxxx */
  139. /* 10000-1FFFFF -> 11110zzz 10zzyyyy 10yyyyxx 10xxxxxx */
  140. /* 200000-3FFFFFF -> 111110tt 10zzzzzz 10zzyyyy 10yyyyxx 10xxxxxx */
  141. /* 4000000-FFFFFFFF -> 111111tt 10tttttt 10zzzzzz 10zzyyyy 10yyyyxx 10xxxxxx */
  142. bytes = 0;
  143. do {
  144. c <<= 1;
  145. bytes++;
  146. } while ((c & 0x80) && bytes < 6);
  147. if (bytes == 1) {
  148. /* A bare "continuation" byte. Say, 80 */
  149. *res = ERROR_WCHAR;
  150. return src;
  151. }
  152. c = (uint8_t)(c) >> bytes;
  153. while (--bytes) {
  154. unsigned ch = (unsigned char) *src;
  155. if ((ch & 0xc0) != 0x80) {
  156. /* Missing "continuation" byte. Example: e0 80 */
  157. *res = ERROR_WCHAR;
  158. return src;
  159. }
  160. c = (c << 6) + (ch & 0x3f);
  161. src++;
  162. }
  163. /* TODO */
  164. /* Need to check that c isn't produced by overlong encoding */
  165. /* Example: 11000000 10000000 converts to NUL */
  166. /* 11110000 10000000 10000100 10000000 converts to 0x100 */
  167. /* correct encoding: 11000100 10000000 */
  168. if (c <= 0x7f) { /* crude check */
  169. *res = ERROR_WCHAR;
  170. return src;
  171. }
  172. *res = c;
  173. return src;
  174. }
  175. size_t FAST_FUNC mbstowcs(wchar_t *dest, const char *src, size_t n)
  176. {
  177. size_t org_n = n;
  178. if (unicode_status != UNICODE_ON) {
  179. while (n) {
  180. unsigned char c = *src++;
  181. if (dest)
  182. *dest++ = c;
  183. if (c == 0)
  184. break;
  185. n--;
  186. }
  187. return org_n - n;
  188. }
  189. while (n) {
  190. wchar_t wc;
  191. src = mbstowc_internal(&wc, src);
  192. if (wc == ERROR_WCHAR) /* error */
  193. return (size_t) -1L;
  194. if (dest)
  195. *dest++ = wc;
  196. if (wc == 0) /* end-of-string */
  197. break;
  198. n--;
  199. }
  200. return org_n - n;
  201. }
  202. int FAST_FUNC iswspace(wint_t wc)
  203. {
  204. return (unsigned)wc <= 0x7f && isspace(wc);
  205. }
  206. int FAST_FUNC iswalnum(wint_t wc)
  207. {
  208. return (unsigned)wc <= 0x7f && isalnum(wc);
  209. }
  210. int FAST_FUNC iswpunct(wint_t wc)
  211. {
  212. return (unsigned)wc <= 0x7f && ispunct(wc);
  213. }
  214. # if CONFIG_LAST_SUPPORTED_WCHAR >= 0x300
  215. struct interval {
  216. uint16_t first;
  217. uint16_t last;
  218. };
  219. /* auxiliary function for binary search in interval table */
  220. static int in_interval_table(unsigned ucs, const struct interval *table, unsigned max)
  221. {
  222. unsigned min;
  223. unsigned mid;
  224. if (ucs < table[0].first || ucs > table[max].last)
  225. return 0;
  226. min = 0;
  227. while (max >= min) {
  228. mid = (min + max) / 2;
  229. if (ucs > table[mid].last)
  230. min = mid + 1;
  231. else if (ucs < table[mid].first)
  232. max = mid - 1;
  233. else
  234. return 1;
  235. }
  236. return 0;
  237. }
  238. static int in_uint16_table(unsigned ucs, const uint16_t *table, unsigned max)
  239. {
  240. unsigned min;
  241. unsigned mid;
  242. unsigned first, last;
  243. first = table[0] >> 2;
  244. last = first + (table[0] & 3);
  245. if (ucs < first || ucs > last)
  246. return 0;
  247. min = 0;
  248. while (max >= min) {
  249. mid = (min + max) / 2;
  250. first = table[mid] >> 2;
  251. last = first + (table[mid] & 3);
  252. if (ucs > last)
  253. min = mid + 1;
  254. else if (ucs < first)
  255. max = mid - 1;
  256. else
  257. return 1;
  258. }
  259. return 0;
  260. }
  261. # endif
  262. /*
  263. * This is an implementation of wcwidth() and wcswidth() (defined in
  264. * IEEE Std 1002.1-2001) for Unicode.
  265. *
  266. * http://www.opengroup.org/onlinepubs/007904975/functions/wcwidth.html
  267. * http://www.opengroup.org/onlinepubs/007904975/functions/wcswidth.html
  268. *
  269. * In fixed-width output devices, Latin characters all occupy a single
  270. * "cell" position of equal width, whereas ideographic CJK characters
  271. * occupy two such cells. Interoperability between terminal-line
  272. * applications and (teletype-style) character terminals using the
  273. * UTF-8 encoding requires agreement on which character should advance
  274. * the cursor by how many cell positions. No established formal
  275. * standards exist at present on which Unicode character shall occupy
  276. * how many cell positions on character terminals. These routines are
  277. * a first attempt of defining such behavior based on simple rules
  278. * applied to data provided by the Unicode Consortium.
  279. *
  280. * For some graphical characters, the Unicode standard explicitly
  281. * defines a character-cell width via the definition of the East Asian
  282. * FullWidth (F), Wide (W), Half-width (H), and Narrow (Na) classes.
  283. * In all these cases, there is no ambiguity about which width a
  284. * terminal shall use. For characters in the East Asian Ambiguous (A)
  285. * class, the width choice depends purely on a preference of backward
  286. * compatibility with either historic CJK or Western practice.
  287. * Choosing single-width for these characters is easy to justify as
  288. * the appropriate long-term solution, as the CJK practice of
  289. * displaying these characters as double-width comes from historic
  290. * implementation simplicity (8-bit encoded characters were displayed
  291. * single-width and 16-bit ones double-width, even for Greek,
  292. * Cyrillic, etc.) and not any typographic considerations.
  293. *
  294. * Much less clear is the choice of width for the Not East Asian
  295. * (Neutral) class. Existing practice does not dictate a width for any
  296. * of these characters. It would nevertheless make sense
  297. * typographically to allocate two character cells to characters such
  298. * as for instance EM SPACE or VOLUME INTEGRAL, which cannot be
  299. * represented adequately with a single-width glyph. The following
  300. * routines at present merely assign a single-cell width to all
  301. * neutral characters, in the interest of simplicity. This is not
  302. * entirely satisfactory and should be reconsidered before
  303. * establishing a formal standard in this area. At the moment, the
  304. * decision which Not East Asian (Neutral) characters should be
  305. * represented by double-width glyphs cannot yet be answered by
  306. * applying a simple rule from the Unicode database content. Setting
  307. * up a proper standard for the behavior of UTF-8 character terminals
  308. * will require a careful analysis not only of each Unicode character,
  309. * but also of each presentation form, something the author of these
  310. * routines has avoided to do so far.
  311. *
  312. * http://www.unicode.org/unicode/reports/tr11/
  313. *
  314. * Markus Kuhn -- 2007-05-26 (Unicode 5.0)
  315. *
  316. * Permission to use, copy, modify, and distribute this software
  317. * for any purpose and without fee is hereby granted. The author
  318. * disclaims all warranties with regard to this software.
  319. *
  320. * Latest version: http://www.cl.cam.ac.uk/~mgk25/ucs/wcwidth.c
  321. */
  322. /* Assigned Unicode character ranges:
  323. * Plane Range
  324. * 0 0000–FFFF Basic Multilingual Plane
  325. * 1 10000–1FFFF Supplementary Multilingual Plane
  326. * 2 20000–2FFFF Supplementary Ideographic Plane
  327. * 3 30000-3FFFF Tertiary Ideographic Plane (no chars assigned yet)
  328. * 4-13 40000–DFFFF currently unassigned
  329. * 14 E0000–EFFFF Supplementary Special-purpose Plane
  330. * 15 F0000–FFFFF Supplementary Private Use Area-A
  331. * 16 100000–10FFFF Supplementary Private Use Area-B
  332. *
  333. * "Supplementary Special-purpose Plane currently contains non-graphical
  334. * characters in two blocks of 128 and 240 characters. The first block
  335. * is for language tag characters for use when language cannot be indicated
  336. * through other protocols (such as the xml:lang attribute in XML).
  337. * The other block contains glyph variation selectors to indicate
  338. * an alternate glyph for a character that cannot be determined by context."
  339. *
  340. * In simpler terms: it is a tool to fix the "Han unification" mess
  341. * created by Unicode committee, to select Chinese/Japanese/Korean/Taiwan
  342. * version of a character. (They forgot that the whole purpose of the Unicode
  343. * was to be able to write all chars in one charset without such tricks).
  344. * Until East Asian users say it is actually necessary to support these
  345. * code points in console applications like busybox
  346. * (i.e. do these chars ever appear in filenames, hostnames, text files
  347. * and such?), we are treating these code points as invalid.
  348. *
  349. * Tertiary Ideographic Plane is also ignored for now,
  350. * until Unicode committee assigns something there.
  351. */
  352. /* The following two functions define the column width of an ISO 10646
  353. * character as follows:
  354. *
  355. * - The null character (U+0000) has a column width of 0.
  356. *
  357. * - Other C0/C1 control characters and DEL will lead to a return
  358. * value of -1.
  359. *
  360. * - Non-spacing and enclosing combining characters (general
  361. * category code Mn or Me in the Unicode database) have a
  362. * column width of 0.
  363. *
  364. * - SOFT HYPHEN (U+00AD) has a column width of 1.
  365. *
  366. * - Other format characters (general category code Cf in the Unicode
  367. * database) and ZERO WIDTH SPACE (U+200B) have a column width of 0.
  368. *
  369. * - Hangul Jamo medial vowels and final consonants (U+1160-U+11FF)
  370. * have a column width of 0.
  371. *
  372. * - Spacing characters in the East Asian Wide (W) or East Asian
  373. * Full-width (F) category as defined in Unicode Technical
  374. * Report #11 have a column width of 2.
  375. *
  376. * - All remaining characters (including all printable
  377. * ISO 8859-1 and WGL4 characters, Unicode control characters,
  378. * etc.) have a column width of 1.
  379. *
  380. * This implementation assumes that wchar_t characters are encoded
  381. * in ISO 10646.
  382. */
  383. int FAST_FUNC wcwidth(unsigned ucs)
  384. {
  385. # if CONFIG_LAST_SUPPORTED_WCHAR >= 0x300
  386. /* sorted list of non-overlapping intervals of non-spacing characters */
  387. /* generated by "uniset +cat=Me +cat=Mn +cat=Cf -00AD +1160-11FF +200B c" */
  388. # define BIG_(a,b) { a, b },
  389. # define PAIR(a,b)
  390. # define ARRAY /* PAIR if < 0x4000 and no more than 4 chars big */ \
  391. BIG_(0x0300, 0x036F) \
  392. PAIR(0x0483, 0x0486) \
  393. PAIR(0x0488, 0x0489) \
  394. BIG_(0x0591, 0x05BD) \
  395. PAIR(0x05BF, 0x05BF) \
  396. PAIR(0x05C1, 0x05C2) \
  397. PAIR(0x05C4, 0x05C5) \
  398. PAIR(0x05C7, 0x05C7) \
  399. PAIR(0x0600, 0x0603) \
  400. BIG_(0x0610, 0x0615) \
  401. BIG_(0x064B, 0x065E) \
  402. PAIR(0x0670, 0x0670) \
  403. BIG_(0x06D6, 0x06E4) \
  404. PAIR(0x06E7, 0x06E8) \
  405. PAIR(0x06EA, 0x06ED) \
  406. PAIR(0x070F, 0x070F) \
  407. PAIR(0x0711, 0x0711) \
  408. BIG_(0x0730, 0x074A) \
  409. BIG_(0x07A6, 0x07B0) \
  410. BIG_(0x07EB, 0x07F3) \
  411. PAIR(0x0901, 0x0902) \
  412. PAIR(0x093C, 0x093C) \
  413. BIG_(0x0941, 0x0948) \
  414. PAIR(0x094D, 0x094D) \
  415. PAIR(0x0951, 0x0954) \
  416. PAIR(0x0962, 0x0963) \
  417. PAIR(0x0981, 0x0981) \
  418. PAIR(0x09BC, 0x09BC) \
  419. PAIR(0x09C1, 0x09C4) \
  420. PAIR(0x09CD, 0x09CD) \
  421. PAIR(0x09E2, 0x09E3) \
  422. PAIR(0x0A01, 0x0A02) \
  423. PAIR(0x0A3C, 0x0A3C) \
  424. PAIR(0x0A41, 0x0A42) \
  425. PAIR(0x0A47, 0x0A48) \
  426. PAIR(0x0A4B, 0x0A4D) \
  427. PAIR(0x0A70, 0x0A71) \
  428. PAIR(0x0A81, 0x0A82) \
  429. PAIR(0x0ABC, 0x0ABC) \
  430. BIG_(0x0AC1, 0x0AC5) \
  431. PAIR(0x0AC7, 0x0AC8) \
  432. PAIR(0x0ACD, 0x0ACD) \
  433. PAIR(0x0AE2, 0x0AE3) \
  434. PAIR(0x0B01, 0x0B01) \
  435. PAIR(0x0B3C, 0x0B3C) \
  436. PAIR(0x0B3F, 0x0B3F) \
  437. PAIR(0x0B41, 0x0B43) \
  438. PAIR(0x0B4D, 0x0B4D) \
  439. PAIR(0x0B56, 0x0B56) \
  440. PAIR(0x0B82, 0x0B82) \
  441. PAIR(0x0BC0, 0x0BC0) \
  442. PAIR(0x0BCD, 0x0BCD) \
  443. PAIR(0x0C3E, 0x0C40) \
  444. PAIR(0x0C46, 0x0C48) \
  445. PAIR(0x0C4A, 0x0C4D) \
  446. PAIR(0x0C55, 0x0C56) \
  447. PAIR(0x0CBC, 0x0CBC) \
  448. PAIR(0x0CBF, 0x0CBF) \
  449. PAIR(0x0CC6, 0x0CC6) \
  450. PAIR(0x0CCC, 0x0CCD) \
  451. PAIR(0x0CE2, 0x0CE3) \
  452. PAIR(0x0D41, 0x0D43) \
  453. PAIR(0x0D4D, 0x0D4D) \
  454. PAIR(0x0DCA, 0x0DCA) \
  455. PAIR(0x0DD2, 0x0DD4) \
  456. PAIR(0x0DD6, 0x0DD6) \
  457. PAIR(0x0E31, 0x0E31) \
  458. BIG_(0x0E34, 0x0E3A) \
  459. BIG_(0x0E47, 0x0E4E) \
  460. PAIR(0x0EB1, 0x0EB1) \
  461. BIG_(0x0EB4, 0x0EB9) \
  462. PAIR(0x0EBB, 0x0EBC) \
  463. BIG_(0x0EC8, 0x0ECD) \
  464. PAIR(0x0F18, 0x0F19) \
  465. PAIR(0x0F35, 0x0F35) \
  466. PAIR(0x0F37, 0x0F37) \
  467. PAIR(0x0F39, 0x0F39) \
  468. BIG_(0x0F71, 0x0F7E) \
  469. BIG_(0x0F80, 0x0F84) \
  470. PAIR(0x0F86, 0x0F87) \
  471. PAIR(0x0FC6, 0x0FC6) \
  472. BIG_(0x0F90, 0x0F97) \
  473. BIG_(0x0F99, 0x0FBC) \
  474. PAIR(0x102D, 0x1030) \
  475. PAIR(0x1032, 0x1032) \
  476. PAIR(0x1036, 0x1037) \
  477. PAIR(0x1039, 0x1039) \
  478. PAIR(0x1058, 0x1059) \
  479. BIG_(0x1160, 0x11FF) \
  480. PAIR(0x135F, 0x135F) \
  481. PAIR(0x1712, 0x1714) \
  482. PAIR(0x1732, 0x1734) \
  483. PAIR(0x1752, 0x1753) \
  484. PAIR(0x1772, 0x1773) \
  485. PAIR(0x17B4, 0x17B5) \
  486. BIG_(0x17B7, 0x17BD) \
  487. PAIR(0x17C6, 0x17C6) \
  488. BIG_(0x17C9, 0x17D3) \
  489. PAIR(0x17DD, 0x17DD) \
  490. PAIR(0x180B, 0x180D) \
  491. PAIR(0x18A9, 0x18A9) \
  492. PAIR(0x1920, 0x1922) \
  493. PAIR(0x1927, 0x1928) \
  494. PAIR(0x1932, 0x1932) \
  495. PAIR(0x1939, 0x193B) \
  496. PAIR(0x1A17, 0x1A18) \
  497. PAIR(0x1B00, 0x1B03) \
  498. PAIR(0x1B34, 0x1B34) \
  499. BIG_(0x1B36, 0x1B3A) \
  500. PAIR(0x1B3C, 0x1B3C) \
  501. PAIR(0x1B42, 0x1B42) \
  502. BIG_(0x1B6B, 0x1B73) \
  503. BIG_(0x1DC0, 0x1DCA) \
  504. PAIR(0x1DFE, 0x1DFF) \
  505. BIG_(0x200B, 0x200F) \
  506. BIG_(0x202A, 0x202E) \
  507. PAIR(0x2060, 0x2063) \
  508. BIG_(0x206A, 0x206F) \
  509. BIG_(0x20D0, 0x20EF) \
  510. BIG_(0x302A, 0x302F) \
  511. PAIR(0x3099, 0x309A) \
  512. /* Too big to be packed in PAIRs: */ \
  513. BIG_(0xA806, 0xA806) \
  514. BIG_(0xA80B, 0xA80B) \
  515. BIG_(0xA825, 0xA826) \
  516. BIG_(0xFB1E, 0xFB1E) \
  517. BIG_(0xFE00, 0xFE0F) \
  518. BIG_(0xFE20, 0xFE23) \
  519. BIG_(0xFEFF, 0xFEFF) \
  520. BIG_(0xFFF9, 0xFFFB)
  521. static const struct interval combining[] = { ARRAY };
  522. # undef BIG_
  523. # undef PAIR
  524. # define BIG_(a,b)
  525. # define PAIR(a,b) (a << 2) | (b-a),
  526. static const uint16_t combining1[] = { ARRAY };
  527. # undef BIG_
  528. # undef PAIR
  529. # define BIG_(a,b) char big_##a[b < 0x4000 && b-a <= 3 ? -1 : 1];
  530. # define PAIR(a,b) char pair##a[b >= 0x4000 || b-a > 3 ? -1 : 1];
  531. struct CHECK { ARRAY };
  532. # undef BIG_
  533. # undef PAIR
  534. # undef ARRAY
  535. # endif
  536. if (ucs == 0)
  537. return 0;
  538. /* Test for 8-bit control characters (00-1f, 80-9f, 7f) */
  539. if ((ucs & ~0x80) < 0x20 || ucs == 0x7f)
  540. return -1;
  541. /* Quick abort if it is an obviously invalid char */
  542. if (ucs > CONFIG_LAST_SUPPORTED_WCHAR)
  543. return -1;
  544. /* Optimization: no combining chars below 0x300 */
  545. if (CONFIG_LAST_SUPPORTED_WCHAR < 0x300 || ucs < 0x300)
  546. return 1;
  547. # if CONFIG_LAST_SUPPORTED_WCHAR >= 0x300
  548. /* Binary search in table of non-spacing characters */
  549. if (in_interval_table(ucs, combining, ARRAY_SIZE(combining) - 1))
  550. return 0;
  551. if (in_uint16_table(ucs, combining1, ARRAY_SIZE(combining1) - 1))
  552. return 0;
  553. /* Optimization: all chars below 0x1100 are not double-width */
  554. if (CONFIG_LAST_SUPPORTED_WCHAR < 0x1100 || ucs < 0x1100)
  555. return 1;
  556. # if CONFIG_LAST_SUPPORTED_WCHAR >= 0x1100
  557. /* Invalid code points: */
  558. /* High (d800..dbff) and low (dc00..dfff) surrogates (valid only in UTF16) */
  559. /* Private Use Area (e000..f8ff) */
  560. /* Noncharacters fdd0..fdef */
  561. if ((CONFIG_LAST_SUPPORTED_WCHAR >= 0xd800 && ucs >= 0xd800 && ucs <= 0xf8ff)
  562. || (CONFIG_LAST_SUPPORTED_WCHAR >= 0xfdd0 && ucs >= 0xfdd0 && ucs <= 0xfdef)
  563. ) {
  564. return -1;
  565. }
  566. /* 0xfffe and 0xffff in every plane are invalid */
  567. if (CONFIG_LAST_SUPPORTED_WCHAR >= 0xfffe && ((ucs & 0xfffe) == 0xfffe)) {
  568. return -1;
  569. }
  570. # if CONFIG_LAST_SUPPORTED_WCHAR >= 0x10000
  571. if (ucs >= 0x10000) {
  572. /* Combining chars in Supplementary Multilingual Plane 0x1xxxx */
  573. static const struct interval combining0x10000[] = {
  574. { 0x0A01, 0x0A03 }, { 0x0A05, 0x0A06 }, { 0x0A0C, 0x0A0F },
  575. { 0x0A38, 0x0A3A }, { 0x0A3F, 0x0A3F }, { 0xD167, 0xD169 },
  576. { 0xD173, 0xD182 }, { 0xD185, 0xD18B }, { 0xD1AA, 0xD1AD },
  577. { 0xD242, 0xD244 }
  578. };
  579. /* Binary search in table of non-spacing characters in Supplementary Multilingual Plane */
  580. if (in_interval_table(ucs ^ 0x10000, combining0x10000, ARRAY_SIZE(combining0x10000) - 1))
  581. return 0;
  582. /* Check a few non-spacing chars in Supplementary Special-purpose Plane 0xExxxx */
  583. if (CONFIG_LAST_SUPPORTED_WCHAR >= 0xE0001
  584. && ( ucs == 0xE0001
  585. || (ucs >= 0xE0020 && ucs <= 0xE007F)
  586. || (ucs >= 0xE0100 && ucs <= 0xE01EF)
  587. )
  588. ) {
  589. return 0;
  590. }
  591. }
  592. # endif
  593. /* If we arrive here, ucs is not a combining or C0/C1 control character.
  594. * Check whether it's 1 char or 2-shar wide.
  595. */
  596. return 1 +
  597. ( (/*ucs >= 0x1100 &&*/ ucs <= 0x115f) /* Hangul Jamo init. consonants */
  598. || ucs == 0x2329 /* left-pointing angle bracket; also CJK punct. char */
  599. || ucs == 0x232a /* right-pointing angle bracket; also CJK punct. char */
  600. || (ucs >= 0x2e80 && ucs <= 0xa4cf && ucs != 0x303f) /* CJK ... Yi */
  601. # if CONFIG_LAST_SUPPORTED_WCHAR >= 0xac00
  602. || (ucs >= 0xac00 && ucs <= 0xd7a3) /* Hangul Syllables */
  603. || (ucs >= 0xf900 && ucs <= 0xfaff) /* CJK Compatibility Ideographs */
  604. || (ucs >= 0xfe10 && ucs <= 0xfe19) /* Vertical forms */
  605. || (ucs >= 0xfe30 && ucs <= 0xfe6f) /* CJK Compatibility Forms */
  606. || (ucs >= 0xff00 && ucs <= 0xff60) /* Fullwidth Forms */
  607. || (ucs >= 0xffe0 && ucs <= 0xffe6)
  608. || ((ucs >> 17) == (2 >> 1)) /* 20000..3ffff: Supplementary and Tertiary Ideographic Planes */
  609. # endif
  610. );
  611. # endif /* >= 0x1100 */
  612. # endif /* >= 0x300 */
  613. }
  614. # if ENABLE_UNICODE_BIDI_SUPPORT
  615. int FAST_FUNC unicode_bidi_isrtl(wint_t wc)
  616. {
  617. /* ranges taken from
  618. * http://www.unicode.org/Public/5.2.0/ucd/extracted/DerivedBidiClass.txt
  619. * Bidi_Class=Left_To_Right | Bidi_Class=Arabic_Letter
  620. */
  621. # define BIG_(a,b) { a, b },
  622. # define PAIR(a,b)
  623. # define ARRAY \
  624. PAIR(0x0590, 0x0590) \
  625. PAIR(0x05BE, 0x05BE) \
  626. PAIR(0x05C0, 0x05C0) \
  627. PAIR(0x05C3, 0x05C3) \
  628. PAIR(0x05C6, 0x05C6) \
  629. BIG_(0x05C8, 0x05FF) \
  630. PAIR(0x0604, 0x0605) \
  631. PAIR(0x0608, 0x0608) \
  632. PAIR(0x060B, 0x060B) \
  633. PAIR(0x060D, 0x060D) \
  634. BIG_(0x061B, 0x064A) \
  635. PAIR(0x065F, 0x065F) \
  636. PAIR(0x066D, 0x066F) \
  637. BIG_(0x0671, 0x06D5) \
  638. PAIR(0x06E5, 0x06E6) \
  639. PAIR(0x06EE, 0x06EF) \
  640. BIG_(0x06FA, 0x070E) \
  641. PAIR(0x0710, 0x0710) \
  642. BIG_(0x0712, 0x072F) \
  643. BIG_(0x074B, 0x07A5) \
  644. BIG_(0x07B1, 0x07EA) \
  645. PAIR(0x07F4, 0x07F5) \
  646. BIG_(0x07FA, 0x0815) \
  647. PAIR(0x081A, 0x081A) \
  648. PAIR(0x0824, 0x0824) \
  649. PAIR(0x0828, 0x0828) \
  650. BIG_(0x082E, 0x08FF) \
  651. PAIR(0x200F, 0x200F) \
  652. PAIR(0x202B, 0x202B) \
  653. PAIR(0x202E, 0x202E) \
  654. BIG_(0xFB1D, 0xFB1D) \
  655. BIG_(0xFB1F, 0xFB28) \
  656. BIG_(0xFB2A, 0xFD3D) \
  657. BIG_(0xFD40, 0xFDCF) \
  658. BIG_(0xFDC8, 0xFDCF) \
  659. BIG_(0xFDF0, 0xFDFC) \
  660. BIG_(0xFDFE, 0xFDFF) \
  661. BIG_(0xFE70, 0xFEFE)
  662. /* Probably not necessary
  663. {0x10800, 0x1091E},
  664. {0x10920, 0x10A00},
  665. {0x10A04, 0x10A04},
  666. {0x10A07, 0x10A0B},
  667. {0x10A10, 0x10A37},
  668. {0x10A3B, 0x10A3E},
  669. {0x10A40, 0x10A7F},
  670. {0x10B36, 0x10B38},
  671. {0x10B40, 0x10E5F},
  672. {0x10E7F, 0x10FFF},
  673. {0x1E800, 0x1EFFF}
  674. */
  675. static const struct interval rtl_b[] = { ARRAY };
  676. # undef BIG_
  677. # undef PAIR
  678. # define BIG_(a,b)
  679. # define PAIR(a,b) (a << 2) | (b-a),
  680. static const uint16_t rtl_p[] = { ARRAY };
  681. # undef BIG_
  682. # undef PAIR
  683. # define BIG_(a,b) char big_##a[b < 0x4000 && b-a <= 3 ? -1 : 1];
  684. # define PAIR(a,b) char pair##a[b >= 0x4000 || b-a > 3 ? -1 : 1];
  685. struct CHECK { ARRAY };
  686. # undef BIG_
  687. # undef PAIR
  688. # undef ARRAY
  689. if (in_interval_table(wc, rtl_b, ARRAY_SIZE(rtl_b) - 1))
  690. return 1;
  691. if (in_uint16_table(wc, rtl_p, ARRAY_SIZE(rtl_p) - 1))
  692. return 1;
  693. return 0;
  694. }
  695. # if ENABLE_UNICODE_NEUTRAL_TABLE
  696. int FAST_FUNC unicode_bidi_is_neutral_wchar(wint_t wc)
  697. {
  698. /* ranges taken from
  699. * http://www.unicode.org/Public/5.2.0/ucd/extracted/DerivedBidiClass.txt
  700. * Bidi_Classes: Paragraph_Separator, Segment_Separator,
  701. * White_Space, Other_Neutral, European_Number, European_Separator,
  702. * European_Terminator, Arabic_Number, Common_Separator
  703. */
  704. # define BIG_(a,b) { a, b },
  705. # define PAIR(a,b)
  706. # define ARRAY \
  707. BIG_(0x0009, 0x000D) \
  708. BIG_(0x001C, 0x0040) \
  709. BIG_(0x005B, 0x0060) \
  710. PAIR(0x007B, 0x007E) \
  711. PAIR(0x0085, 0x0085) \
  712. BIG_(0x00A0, 0x00A9) \
  713. PAIR(0x00AB, 0x00AC) \
  714. BIG_(0x00AE, 0x00B4) \
  715. PAIR(0x00B6, 0x00B9) \
  716. BIG_(0x00BB, 0x00BF) \
  717. PAIR(0x00D7, 0x00D7) \
  718. PAIR(0x00F7, 0x00F7) \
  719. PAIR(0x02B9, 0x02BA) \
  720. BIG_(0x02C2, 0x02CF) \
  721. BIG_(0x02D2, 0x02DF) \
  722. BIG_(0x02E5, 0x02FF) \
  723. PAIR(0x0374, 0x0375) \
  724. PAIR(0x037E, 0x037E) \
  725. PAIR(0x0384, 0x0385) \
  726. PAIR(0x0387, 0x0387) \
  727. PAIR(0x03F6, 0x03F6) \
  728. PAIR(0x058A, 0x058A) \
  729. PAIR(0x0600, 0x0603) \
  730. PAIR(0x0606, 0x0607) \
  731. PAIR(0x0609, 0x060A) \
  732. PAIR(0x060C, 0x060C) \
  733. PAIR(0x060E, 0x060F) \
  734. BIG_(0x0660, 0x066C) \
  735. PAIR(0x06DD, 0x06DD) \
  736. PAIR(0x06E9, 0x06E9) \
  737. BIG_(0x06F0, 0x06F9) \
  738. PAIR(0x07F6, 0x07F9) \
  739. PAIR(0x09F2, 0x09F3) \
  740. PAIR(0x09FB, 0x09FB) \
  741. PAIR(0x0AF1, 0x0AF1) \
  742. BIG_(0x0BF3, 0x0BFA) \
  743. BIG_(0x0C78, 0x0C7E) \
  744. PAIR(0x0CF1, 0x0CF2) \
  745. PAIR(0x0E3F, 0x0E3F) \
  746. PAIR(0x0F3A, 0x0F3D) \
  747. BIG_(0x1390, 0x1400) \
  748. PAIR(0x1680, 0x1680) \
  749. PAIR(0x169B, 0x169C) \
  750. PAIR(0x17DB, 0x17DB) \
  751. BIG_(0x17F0, 0x17F9) \
  752. BIG_(0x1800, 0x180A) \
  753. PAIR(0x180E, 0x180E) \
  754. PAIR(0x1940, 0x1940) \
  755. PAIR(0x1944, 0x1945) \
  756. BIG_(0x19DE, 0x19FF) \
  757. PAIR(0x1FBD, 0x1FBD) \
  758. PAIR(0x1FBF, 0x1FC1) \
  759. PAIR(0x1FCD, 0x1FCF) \
  760. PAIR(0x1FDD, 0x1FDF) \
  761. PAIR(0x1FED, 0x1FEF) \
  762. PAIR(0x1FFD, 0x1FFE) \
  763. BIG_(0x2000, 0x200A) \
  764. BIG_(0x2010, 0x2029) \
  765. BIG_(0x202F, 0x205F) \
  766. PAIR(0x2070, 0x2070) \
  767. BIG_(0x2074, 0x207E) \
  768. BIG_(0x2080, 0x208E) \
  769. BIG_(0x20A0, 0x20B8) \
  770. PAIR(0x2100, 0x2101) \
  771. PAIR(0x2103, 0x2106) \
  772. PAIR(0x2108, 0x2109) \
  773. PAIR(0x2114, 0x2114) \
  774. PAIR(0x2116, 0x2118) \
  775. BIG_(0x211E, 0x2123) \
  776. PAIR(0x2125, 0x2125) \
  777. PAIR(0x2127, 0x2127) \
  778. PAIR(0x2129, 0x2129) \
  779. PAIR(0x212E, 0x212E) \
  780. PAIR(0x213A, 0x213B) \
  781. BIG_(0x2140, 0x2144) \
  782. PAIR(0x214A, 0x214D) \
  783. BIG_(0x2150, 0x215F) \
  784. PAIR(0x2189, 0x2189) \
  785. BIG_(0x2190, 0x2335) \
  786. BIG_(0x237B, 0x2394) \
  787. BIG_(0x2396, 0x23E8) \
  788. BIG_(0x2400, 0x2426) \
  789. BIG_(0x2440, 0x244A) \
  790. BIG_(0x2460, 0x249B) \
  791. BIG_(0x24EA, 0x26AB) \
  792. BIG_(0x26AD, 0x26CD) \
  793. BIG_(0x26CF, 0x26E1) \
  794. PAIR(0x26E3, 0x26E3) \
  795. BIG_(0x26E8, 0x26FF) \
  796. PAIR(0x2701, 0x2704) \
  797. PAIR(0x2706, 0x2709) \
  798. BIG_(0x270C, 0x2727) \
  799. BIG_(0x2729, 0x274B) \
  800. PAIR(0x274D, 0x274D) \
  801. PAIR(0x274F, 0x2752) \
  802. BIG_(0x2756, 0x275E) \
  803. BIG_(0x2761, 0x2794) \
  804. BIG_(0x2798, 0x27AF) \
  805. BIG_(0x27B1, 0x27BE) \
  806. BIG_(0x27C0, 0x27CA) \
  807. PAIR(0x27CC, 0x27CC) \
  808. BIG_(0x27D0, 0x27FF) \
  809. BIG_(0x2900, 0x2B4C) \
  810. BIG_(0x2B50, 0x2B59) \
  811. BIG_(0x2CE5, 0x2CEA) \
  812. BIG_(0x2CF9, 0x2CFF) \
  813. BIG_(0x2E00, 0x2E99) \
  814. BIG_(0x2E9B, 0x2EF3) \
  815. BIG_(0x2F00, 0x2FD5) \
  816. BIG_(0x2FF0, 0x2FFB) \
  817. BIG_(0x3000, 0x3004) \
  818. BIG_(0x3008, 0x3020) \
  819. PAIR(0x3030, 0x3030) \
  820. PAIR(0x3036, 0x3037) \
  821. PAIR(0x303D, 0x303D) \
  822. PAIR(0x303E, 0x303F) \
  823. PAIR(0x309B, 0x309C) \
  824. PAIR(0x30A0, 0x30A0) \
  825. PAIR(0x30FB, 0x30FB) \
  826. BIG_(0x31C0, 0x31E3) \
  827. PAIR(0x321D, 0x321E) \
  828. BIG_(0x3250, 0x325F) \
  829. PAIR(0x327C, 0x327E) \
  830. BIG_(0x32B1, 0x32BF) \
  831. PAIR(0x32CC, 0x32CF) \
  832. PAIR(0x3377, 0x337A) \
  833. PAIR(0x33DE, 0x33DF) \
  834. PAIR(0x33FF, 0x33FF) \
  835. BIG_(0x4DC0, 0x4DFF) \
  836. BIG_(0xA490, 0xA4C6) \
  837. BIG_(0xA60D, 0xA60F) \
  838. BIG_(0xA673, 0xA673) \
  839. BIG_(0xA67E, 0xA67F) \
  840. BIG_(0xA700, 0xA721) \
  841. BIG_(0xA788, 0xA788) \
  842. BIG_(0xA828, 0xA82B) \
  843. BIG_(0xA838, 0xA839) \
  844. BIG_(0xA874, 0xA877) \
  845. BIG_(0xFB29, 0xFB29) \
  846. BIG_(0xFD3E, 0xFD3F) \
  847. BIG_(0xFDFD, 0xFDFD) \
  848. BIG_(0xFE10, 0xFE19) \
  849. BIG_(0xFE30, 0xFE52) \
  850. BIG_(0xFE54, 0xFE66) \
  851. BIG_(0xFE68, 0xFE6B) \
  852. BIG_(0xFF01, 0xFF20) \
  853. BIG_(0xFF3B, 0xFF40) \
  854. BIG_(0xFF5B, 0xFF65) \
  855. BIG_(0xFFE0, 0xFFE6) \
  856. BIG_(0xFFE8, 0xFFEE) \
  857. BIG_(0xFFF9, 0xFFFD)
  858. /*
  859. {0x10101, 0x10101},
  860. {0x10140, 0x1019B},
  861. {0x1091F, 0x1091F},
  862. {0x10B39, 0x10B3F},
  863. {0x10E60, 0x10E7E},
  864. {0x1D200, 0x1D241},
  865. {0x1D245, 0x1D245},
  866. {0x1D300, 0x1D356},
  867. {0x1D6DB, 0x1D6DB},
  868. {0x1D715, 0x1D715},
  869. {0x1D74F, 0x1D74F},
  870. {0x1D789, 0x1D789},
  871. {0x1D7C3, 0x1D7C3},
  872. {0x1D7CE, 0x1D7FF},
  873. {0x1F000, 0x1F02B},
  874. {0x1F030, 0x1F093},
  875. {0x1F100, 0x1F10A}
  876. */
  877. static const struct interval neutral_b[] = { ARRAY };
  878. # undef BIG_
  879. # undef PAIR
  880. # define BIG_(a,b)
  881. # define PAIR(a,b) (a << 2) | (b-a),
  882. static const uint16_t neutral_p[] = { ARRAY };
  883. # undef BIG_
  884. # undef PAIR
  885. # define BIG_(a,b) char big_##a[b < 0x4000 && b-a <= 3 ? -1 : 1];
  886. # define PAIR(a,b) char pair##a[b >= 0x4000 || b-a > 3 ? -1 : 1];
  887. struct CHECK { ARRAY };
  888. # undef BIG_
  889. # undef PAIR
  890. # undef ARRAY
  891. if (in_interval_table(wc, neutral_b, ARRAY_SIZE(neutral_b) - 1))
  892. return 1;
  893. if (in_uint16_table(wc, neutral_p, ARRAY_SIZE(neutral_p) - 1))
  894. return 1;
  895. return 0;
  896. }
  897. # endif
  898. # endif /* UNICODE_BIDI_SUPPORT */
  899. #endif /* Homegrown Unicode support */
  900. /* The rest is mostly same for libc and for "homegrown" support */
  901. #if 0 // UNUSED
  902. size_t FAST_FUNC unicode_strlen(const char *string)
  903. {
  904. size_t width = mbstowcs(NULL, string, INT_MAX);
  905. if (width == (size_t)-1L)
  906. return strlen(string);
  907. return width;
  908. }
  909. #endif
  910. size_t FAST_FUNC unicode_strwidth(const char *string)
  911. {
  912. uni_stat_t uni_stat;
  913. printable_string(&uni_stat, string);
  914. return uni_stat.unicode_width;
  915. }
  916. static char* FAST_FUNC unicode_conv_to_printable2(uni_stat_t *stats, const char *src, unsigned width, int flags)
  917. {
  918. char *dst;
  919. unsigned dst_len;
  920. unsigned uni_count;
  921. unsigned uni_width;
  922. if (unicode_status != UNICODE_ON) {
  923. char *d;
  924. if (flags & UNI_FLAG_PAD) {
  925. d = dst = xmalloc(width + 1);
  926. while ((int)--width >= 0) {
  927. unsigned char c = *src;
  928. if (c == '\0') {
  929. do
  930. *d++ = ' ';
  931. while ((int)--width >= 0);
  932. break;
  933. }
  934. *d++ = (c >= ' ' && c < 0x7f) ? c : '?';
  935. src++;
  936. }
  937. *d = '\0';
  938. } else {
  939. d = dst = xstrndup(src, width);
  940. while (*d) {
  941. unsigned char c = *d;
  942. if (c < ' ' || c >= 0x7f)
  943. *d = '?';
  944. d++;
  945. }
  946. }
  947. if (stats) {
  948. stats->byte_count = (d - dst);
  949. stats->unicode_count = (d - dst);
  950. stats->unicode_width = (d - dst);
  951. }
  952. return dst;
  953. }
  954. dst = NULL;
  955. uni_count = uni_width = 0;
  956. dst_len = 0;
  957. while (1) {
  958. int w;
  959. wchar_t wc;
  960. #if ENABLE_UNICODE_USING_LOCALE
  961. {
  962. mbstate_t mbst = { 0 };
  963. ssize_t rc = mbsrtowcs(&wc, &src, 1, &mbst);
  964. /* If invalid sequence is seen: -1 is returned,
  965. * src points to the invalid sequence, errno = EILSEQ.
  966. * Else number of wchars (excluding terminating L'\0')
  967. * written to dest is returned.
  968. * If len (here: 1) non-L'\0' wchars stored at dest,
  969. * src points to the next char to be converted.
  970. * If string is completely converted: src = NULL.
  971. */
  972. if (rc == 0) /* end-of-string */
  973. break;
  974. if (rc < 0) { /* error */
  975. src++;
  976. goto subst;
  977. }
  978. if (!iswprint(wc))
  979. goto subst;
  980. }
  981. #else
  982. src = mbstowc_internal(&wc, src);
  983. /* src is advanced to next mb char
  984. * wc == ERROR_WCHAR: invalid sequence is seen
  985. * else: wc is set
  986. */
  987. if (wc == ERROR_WCHAR) /* error */
  988. goto subst;
  989. if (wc == 0) /* end-of-string */
  990. break;
  991. #endif
  992. if (CONFIG_LAST_SUPPORTED_WCHAR && wc > CONFIG_LAST_SUPPORTED_WCHAR)
  993. goto subst;
  994. w = wcwidth(wc);
  995. if ((ENABLE_UNICODE_COMBINING_WCHARS && w < 0) /* non-printable wchar */
  996. || (!ENABLE_UNICODE_COMBINING_WCHARS && w <= 0)
  997. || (!ENABLE_UNICODE_WIDE_WCHARS && w > 1)
  998. ) {
  999. subst:
  1000. wc = CONFIG_SUBST_WCHAR;
  1001. w = 1;
  1002. }
  1003. width -= w;
  1004. /* Note: if width == 0, we still may add more chars,
  1005. * they may be zero-width or combining ones */
  1006. if ((int)width < 0) {
  1007. /* can't add this wc, string would become longer than width */
  1008. width += w;
  1009. break;
  1010. }
  1011. uni_count++;
  1012. uni_width += w;
  1013. dst = xrealloc(dst, dst_len + MB_CUR_MAX);
  1014. #if ENABLE_UNICODE_USING_LOCALE
  1015. {
  1016. mbstate_t mbst = { 0 };
  1017. dst_len += wcrtomb(&dst[dst_len], wc, &mbst);
  1018. }
  1019. #else
  1020. dst_len += wcrtomb_internal(&dst[dst_len], wc);
  1021. #endif
  1022. }
  1023. /* Pad to remaining width */
  1024. if (flags & UNI_FLAG_PAD) {
  1025. dst = xrealloc(dst, dst_len + width + 1);
  1026. uni_count += width;
  1027. uni_width += width;
  1028. while ((int)--width >= 0) {
  1029. dst[dst_len++] = ' ';
  1030. }
  1031. }
  1032. dst[dst_len] = '\0';
  1033. if (stats) {
  1034. stats->byte_count = dst_len;
  1035. stats->unicode_count = uni_count;
  1036. stats->unicode_width = uni_width;
  1037. }
  1038. return dst;
  1039. }
  1040. char* FAST_FUNC unicode_conv_to_printable(uni_stat_t *stats, const char *src)
  1041. {
  1042. return unicode_conv_to_printable2(stats, src, INT_MAX, 0);
  1043. }
  1044. char* FAST_FUNC unicode_conv_to_printable_fixedwidth(/*uni_stat_t *stats,*/ const char *src, unsigned width)
  1045. {
  1046. return unicode_conv_to_printable2(/*stats:*/ NULL, src, width, UNI_FLAG_PAD);
  1047. }
  1048. #ifdef UNUSED
  1049. char* FAST_FUNC unicode_conv_to_printable_maxwidth(uni_stat_t *stats, const char *src, unsigned maxwidth)
  1050. {
  1051. return unicode_conv_to_printable2(stats, src, maxwidth, 0);
  1052. }
  1053. unsigned FAST_FUNC unicode_padding_to_width(unsigned width, const char *src)
  1054. {
  1055. if (unicode_status != UNICODE_ON) {
  1056. return width - strnlen(src, width);
  1057. }
  1058. while (1) {
  1059. int w;
  1060. wchar_t wc;
  1061. #if ENABLE_UNICODE_USING_LOCALE
  1062. {
  1063. mbstate_t mbst = { 0 };
  1064. ssize_t rc = mbsrtowcs(&wc, &src, 1, &mbst);
  1065. if (rc <= 0) /* error, or end-of-string */
  1066. return width;
  1067. }
  1068. #else
  1069. src = mbstowc_internal(&wc, src);
  1070. if (wc == ERROR_WCHAR || wc == 0) /* error, or end-of-string */
  1071. return width;
  1072. #endif
  1073. w = wcwidth(wc);
  1074. if (w < 0) /* non-printable wchar */
  1075. return width;
  1076. width -= w;
  1077. if ((int)width <= 0) /* string is longer than width */
  1078. return 0;
  1079. }
  1080. }
  1081. #endif