3
0

unicode.c 32 KB

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