unicode.c 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186
  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. # if CONFIG_LAST_SUPPORTED_WCHAR >= 0x2e80
  623. || (ucs >= 0x2e80 && ucs <= 0xa4cf && ucs != 0x303f) /* CJK ... Yi */
  624. # endif
  625. # if CONFIG_LAST_SUPPORTED_WCHAR >= 0xac00
  626. || (ucs >= 0xac00 && ucs <= 0xd7a3) /* Hangul Syllables */
  627. # endif
  628. # if CONFIG_LAST_SUPPORTED_WCHAR >= 0xf900
  629. || (ucs >= 0xf900 && ucs <= 0xfaff) /* CJK Compatibility Ideographs */
  630. || (ucs >= 0xfe10 && ucs <= 0xfe19) /* Vertical forms */
  631. || (ucs >= 0xfe30 && ucs <= 0xfe6f) /* CJK Compatibility Forms */
  632. || (ucs >= 0xff00 && ucs <= 0xff60) /* Fullwidth Forms */
  633. || (ucs >= 0xffe0 && ucs <= 0xffe6)
  634. # endif
  635. # if CONFIG_LAST_SUPPORTED_WCHAR >= 0x20000
  636. || ((ucs >> 17) == (2 >> 1)) /* 20000..3ffff: Supplementary and Tertiary Ideographic Planes */
  637. # endif
  638. );
  639. # endif /* >= 0x1100 */
  640. # endif /* >= 0x300 */
  641. }
  642. # if ENABLE_UNICODE_BIDI_SUPPORT
  643. int FAST_FUNC unicode_bidi_isrtl(wint_t wc)
  644. {
  645. /* ranges taken from
  646. * http://www.unicode.org/Public/5.2.0/ucd/extracted/DerivedBidiClass.txt
  647. * Bidi_Class=Left_To_Right | Bidi_Class=Arabic_Letter
  648. */
  649. # define BIG_(a,b) { a, b },
  650. # define PAIR(a,b)
  651. # define ARRAY \
  652. PAIR(0x0590, 0x0590) \
  653. PAIR(0x05BE, 0x05BE) \
  654. PAIR(0x05C0, 0x05C0) \
  655. PAIR(0x05C3, 0x05C3) \
  656. PAIR(0x05C6, 0x05C6) \
  657. BIG_(0x05C8, 0x05FF) \
  658. PAIR(0x0604, 0x0605) \
  659. PAIR(0x0608, 0x0608) \
  660. PAIR(0x060B, 0x060B) \
  661. PAIR(0x060D, 0x060D) \
  662. BIG_(0x061B, 0x064A) \
  663. PAIR(0x065F, 0x065F) \
  664. PAIR(0x066D, 0x066F) \
  665. BIG_(0x0671, 0x06D5) \
  666. PAIR(0x06E5, 0x06E6) \
  667. PAIR(0x06EE, 0x06EF) \
  668. BIG_(0x06FA, 0x070E) \
  669. PAIR(0x0710, 0x0710) \
  670. BIG_(0x0712, 0x072F) \
  671. BIG_(0x074B, 0x07A5) \
  672. BIG_(0x07B1, 0x07EA) \
  673. PAIR(0x07F4, 0x07F5) \
  674. BIG_(0x07FA, 0x0815) \
  675. PAIR(0x081A, 0x081A) \
  676. PAIR(0x0824, 0x0824) \
  677. PAIR(0x0828, 0x0828) \
  678. BIG_(0x082E, 0x08FF) \
  679. PAIR(0x200F, 0x200F) \
  680. PAIR(0x202B, 0x202B) \
  681. PAIR(0x202E, 0x202E) \
  682. BIG_(0xFB1D, 0xFB1D) \
  683. BIG_(0xFB1F, 0xFB28) \
  684. BIG_(0xFB2A, 0xFD3D) \
  685. BIG_(0xFD40, 0xFDCF) \
  686. BIG_(0xFDC8, 0xFDCF) \
  687. BIG_(0xFDF0, 0xFDFC) \
  688. BIG_(0xFDFE, 0xFDFF) \
  689. BIG_(0xFE70, 0xFEFE)
  690. /* Probably not necessary
  691. {0x10800, 0x1091E},
  692. {0x10920, 0x10A00},
  693. {0x10A04, 0x10A04},
  694. {0x10A07, 0x10A0B},
  695. {0x10A10, 0x10A37},
  696. {0x10A3B, 0x10A3E},
  697. {0x10A40, 0x10A7F},
  698. {0x10B36, 0x10B38},
  699. {0x10B40, 0x10E5F},
  700. {0x10E7F, 0x10FFF},
  701. {0x1E800, 0x1EFFF}
  702. */
  703. static const struct interval rtl_b[] = { ARRAY };
  704. # undef BIG_
  705. # undef PAIR
  706. # define BIG_(a,b)
  707. # define PAIR(a,b) (a << 2) | (b-a),
  708. static const uint16_t rtl_p[] = { ARRAY };
  709. # undef BIG_
  710. # undef PAIR
  711. # define BIG_(a,b) char big_##a[b < 0x4000 && b-a <= 3 ? -1 : 1];
  712. # define PAIR(a,b) char pair##a[b >= 0x4000 || b-a > 3 ? -1 : 1];
  713. struct CHECK { ARRAY };
  714. # undef BIG_
  715. # undef PAIR
  716. # undef ARRAY
  717. if (in_interval_table(wc, rtl_b, ARRAY_SIZE(rtl_b) - 1))
  718. return 1;
  719. if (in_uint16_table(wc, rtl_p, ARRAY_SIZE(rtl_p) - 1))
  720. return 1;
  721. return 0;
  722. }
  723. # if ENABLE_UNICODE_NEUTRAL_TABLE
  724. int FAST_FUNC unicode_bidi_is_neutral_wchar(wint_t wc)
  725. {
  726. /* ranges taken from
  727. * http://www.unicode.org/Public/5.2.0/ucd/extracted/DerivedBidiClass.txt
  728. * Bidi_Classes: Paragraph_Separator, Segment_Separator,
  729. * White_Space, Other_Neutral, European_Number, European_Separator,
  730. * European_Terminator, Arabic_Number, Common_Separator
  731. */
  732. # define BIG_(a,b) { a, b },
  733. # define PAIR(a,b)
  734. # define ARRAY \
  735. BIG_(0x0009, 0x000D) \
  736. BIG_(0x001C, 0x0040) \
  737. BIG_(0x005B, 0x0060) \
  738. PAIR(0x007B, 0x007E) \
  739. PAIR(0x0085, 0x0085) \
  740. BIG_(0x00A0, 0x00A9) \
  741. PAIR(0x00AB, 0x00AC) \
  742. BIG_(0x00AE, 0x00B4) \
  743. PAIR(0x00B6, 0x00B9) \
  744. BIG_(0x00BB, 0x00BF) \
  745. PAIR(0x00D7, 0x00D7) \
  746. PAIR(0x00F7, 0x00F7) \
  747. PAIR(0x02B9, 0x02BA) \
  748. BIG_(0x02C2, 0x02CF) \
  749. BIG_(0x02D2, 0x02DF) \
  750. BIG_(0x02E5, 0x02FF) \
  751. PAIR(0x0374, 0x0375) \
  752. PAIR(0x037E, 0x037E) \
  753. PAIR(0x0384, 0x0385) \
  754. PAIR(0x0387, 0x0387) \
  755. PAIR(0x03F6, 0x03F6) \
  756. PAIR(0x058A, 0x058A) \
  757. PAIR(0x0600, 0x0603) \
  758. PAIR(0x0606, 0x0607) \
  759. PAIR(0x0609, 0x060A) \
  760. PAIR(0x060C, 0x060C) \
  761. PAIR(0x060E, 0x060F) \
  762. BIG_(0x0660, 0x066C) \
  763. PAIR(0x06DD, 0x06DD) \
  764. PAIR(0x06E9, 0x06E9) \
  765. BIG_(0x06F0, 0x06F9) \
  766. PAIR(0x07F6, 0x07F9) \
  767. PAIR(0x09F2, 0x09F3) \
  768. PAIR(0x09FB, 0x09FB) \
  769. PAIR(0x0AF1, 0x0AF1) \
  770. BIG_(0x0BF3, 0x0BFA) \
  771. BIG_(0x0C78, 0x0C7E) \
  772. PAIR(0x0CF1, 0x0CF2) \
  773. PAIR(0x0E3F, 0x0E3F) \
  774. PAIR(0x0F3A, 0x0F3D) \
  775. BIG_(0x1390, 0x1400) \
  776. PAIR(0x1680, 0x1680) \
  777. PAIR(0x169B, 0x169C) \
  778. PAIR(0x17DB, 0x17DB) \
  779. BIG_(0x17F0, 0x17F9) \
  780. BIG_(0x1800, 0x180A) \
  781. PAIR(0x180E, 0x180E) \
  782. PAIR(0x1940, 0x1940) \
  783. PAIR(0x1944, 0x1945) \
  784. BIG_(0x19DE, 0x19FF) \
  785. PAIR(0x1FBD, 0x1FBD) \
  786. PAIR(0x1FBF, 0x1FC1) \
  787. PAIR(0x1FCD, 0x1FCF) \
  788. PAIR(0x1FDD, 0x1FDF) \
  789. PAIR(0x1FED, 0x1FEF) \
  790. PAIR(0x1FFD, 0x1FFE) \
  791. BIG_(0x2000, 0x200A) \
  792. BIG_(0x2010, 0x2029) \
  793. BIG_(0x202F, 0x205F) \
  794. PAIR(0x2070, 0x2070) \
  795. BIG_(0x2074, 0x207E) \
  796. BIG_(0x2080, 0x208E) \
  797. BIG_(0x20A0, 0x20B8) \
  798. PAIR(0x2100, 0x2101) \
  799. PAIR(0x2103, 0x2106) \
  800. PAIR(0x2108, 0x2109) \
  801. PAIR(0x2114, 0x2114) \
  802. PAIR(0x2116, 0x2118) \
  803. BIG_(0x211E, 0x2123) \
  804. PAIR(0x2125, 0x2125) \
  805. PAIR(0x2127, 0x2127) \
  806. PAIR(0x2129, 0x2129) \
  807. PAIR(0x212E, 0x212E) \
  808. PAIR(0x213A, 0x213B) \
  809. BIG_(0x2140, 0x2144) \
  810. PAIR(0x214A, 0x214D) \
  811. BIG_(0x2150, 0x215F) \
  812. PAIR(0x2189, 0x2189) \
  813. BIG_(0x2190, 0x2335) \
  814. BIG_(0x237B, 0x2394) \
  815. BIG_(0x2396, 0x23E8) \
  816. BIG_(0x2400, 0x2426) \
  817. BIG_(0x2440, 0x244A) \
  818. BIG_(0x2460, 0x249B) \
  819. BIG_(0x24EA, 0x26AB) \
  820. BIG_(0x26AD, 0x26CD) \
  821. BIG_(0x26CF, 0x26E1) \
  822. PAIR(0x26E3, 0x26E3) \
  823. BIG_(0x26E8, 0x26FF) \
  824. PAIR(0x2701, 0x2704) \
  825. PAIR(0x2706, 0x2709) \
  826. BIG_(0x270C, 0x2727) \
  827. BIG_(0x2729, 0x274B) \
  828. PAIR(0x274D, 0x274D) \
  829. PAIR(0x274F, 0x2752) \
  830. BIG_(0x2756, 0x275E) \
  831. BIG_(0x2761, 0x2794) \
  832. BIG_(0x2798, 0x27AF) \
  833. BIG_(0x27B1, 0x27BE) \
  834. BIG_(0x27C0, 0x27CA) \
  835. PAIR(0x27CC, 0x27CC) \
  836. BIG_(0x27D0, 0x27FF) \
  837. BIG_(0x2900, 0x2B4C) \
  838. BIG_(0x2B50, 0x2B59) \
  839. BIG_(0x2CE5, 0x2CEA) \
  840. BIG_(0x2CF9, 0x2CFF) \
  841. BIG_(0x2E00, 0x2E99) \
  842. BIG_(0x2E9B, 0x2EF3) \
  843. BIG_(0x2F00, 0x2FD5) \
  844. BIG_(0x2FF0, 0x2FFB) \
  845. BIG_(0x3000, 0x3004) \
  846. BIG_(0x3008, 0x3020) \
  847. PAIR(0x3030, 0x3030) \
  848. PAIR(0x3036, 0x3037) \
  849. PAIR(0x303D, 0x303D) \
  850. PAIR(0x303E, 0x303F) \
  851. PAIR(0x309B, 0x309C) \
  852. PAIR(0x30A0, 0x30A0) \
  853. PAIR(0x30FB, 0x30FB) \
  854. BIG_(0x31C0, 0x31E3) \
  855. PAIR(0x321D, 0x321E) \
  856. BIG_(0x3250, 0x325F) \
  857. PAIR(0x327C, 0x327E) \
  858. BIG_(0x32B1, 0x32BF) \
  859. PAIR(0x32CC, 0x32CF) \
  860. PAIR(0x3377, 0x337A) \
  861. PAIR(0x33DE, 0x33DF) \
  862. PAIR(0x33FF, 0x33FF) \
  863. BIG_(0x4DC0, 0x4DFF) \
  864. BIG_(0xA490, 0xA4C6) \
  865. BIG_(0xA60D, 0xA60F) \
  866. BIG_(0xA673, 0xA673) \
  867. BIG_(0xA67E, 0xA67F) \
  868. BIG_(0xA700, 0xA721) \
  869. BIG_(0xA788, 0xA788) \
  870. BIG_(0xA828, 0xA82B) \
  871. BIG_(0xA838, 0xA839) \
  872. BIG_(0xA874, 0xA877) \
  873. BIG_(0xFB29, 0xFB29) \
  874. BIG_(0xFD3E, 0xFD3F) \
  875. BIG_(0xFDFD, 0xFDFD) \
  876. BIG_(0xFE10, 0xFE19) \
  877. BIG_(0xFE30, 0xFE52) \
  878. BIG_(0xFE54, 0xFE66) \
  879. BIG_(0xFE68, 0xFE6B) \
  880. BIG_(0xFF01, 0xFF20) \
  881. BIG_(0xFF3B, 0xFF40) \
  882. BIG_(0xFF5B, 0xFF65) \
  883. BIG_(0xFFE0, 0xFFE6) \
  884. BIG_(0xFFE8, 0xFFEE) \
  885. BIG_(0xFFF9, 0xFFFD)
  886. /*
  887. {0x10101, 0x10101},
  888. {0x10140, 0x1019B},
  889. {0x1091F, 0x1091F},
  890. {0x10B39, 0x10B3F},
  891. {0x10E60, 0x10E7E},
  892. {0x1D200, 0x1D241},
  893. {0x1D245, 0x1D245},
  894. {0x1D300, 0x1D356},
  895. {0x1D6DB, 0x1D6DB},
  896. {0x1D715, 0x1D715},
  897. {0x1D74F, 0x1D74F},
  898. {0x1D789, 0x1D789},
  899. {0x1D7C3, 0x1D7C3},
  900. {0x1D7CE, 0x1D7FF},
  901. {0x1F000, 0x1F02B},
  902. {0x1F030, 0x1F093},
  903. {0x1F100, 0x1F10A}
  904. */
  905. static const struct interval neutral_b[] = { ARRAY };
  906. # undef BIG_
  907. # undef PAIR
  908. # define BIG_(a,b)
  909. # define PAIR(a,b) (a << 2) | (b-a),
  910. static const uint16_t neutral_p[] = { ARRAY };
  911. # undef BIG_
  912. # undef PAIR
  913. # define BIG_(a,b) char big_##a[b < 0x4000 && b-a <= 3 ? -1 : 1];
  914. # define PAIR(a,b) char pair##a[b >= 0x4000 || b-a > 3 ? -1 : 1];
  915. struct CHECK { ARRAY };
  916. # undef BIG_
  917. # undef PAIR
  918. # undef ARRAY
  919. if (in_interval_table(wc, neutral_b, ARRAY_SIZE(neutral_b) - 1))
  920. return 1;
  921. if (in_uint16_table(wc, neutral_p, ARRAY_SIZE(neutral_p) - 1))
  922. return 1;
  923. return 0;
  924. }
  925. # endif
  926. # endif /* UNICODE_BIDI_SUPPORT */
  927. #endif /* Homegrown Unicode support */
  928. /* The rest is mostly same for libc and for "homegrown" support */
  929. size_t FAST_FUNC unicode_strlen(const char *string)
  930. {
  931. size_t width = mbstowcs(NULL, string, INT_MAX);
  932. if (width == (size_t)-1L)
  933. return strlen(string);
  934. return width;
  935. }
  936. size_t FAST_FUNC unicode_strwidth(const char *string)
  937. {
  938. uni_stat_t uni_stat;
  939. printable_string2(&uni_stat, string);
  940. return uni_stat.unicode_width;
  941. }
  942. static char* FAST_FUNC unicode_conv_to_printable2(uni_stat_t *stats, const char *src, unsigned width, int flags)
  943. {
  944. char *dst;
  945. unsigned dst_len;
  946. unsigned uni_count;
  947. unsigned uni_width;
  948. if (unicode_status != UNICODE_ON) {
  949. char *d;
  950. if (flags & UNI_FLAG_PAD) {
  951. d = dst = xmalloc(width + 1);
  952. while ((int)--width >= 0) {
  953. unsigned char c = *src;
  954. if (c == '\0') {
  955. do
  956. *d++ = ' ';
  957. while ((int)--width >= 0);
  958. break;
  959. }
  960. *d++ = (c >= ' ' && c < 0x7f) ? c : '?';
  961. src++;
  962. }
  963. *d = '\0';
  964. } else {
  965. d = dst = xstrndup(src, width);
  966. while (*d) {
  967. unsigned char c = *d;
  968. if (c < ' ' || c >= 0x7f)
  969. *d = '?';
  970. d++;
  971. }
  972. }
  973. if (stats) {
  974. stats->byte_count = (d - dst);
  975. stats->unicode_count = (d - dst);
  976. stats->unicode_width = (d - dst);
  977. }
  978. return dst;
  979. }
  980. dst = NULL;
  981. uni_count = uni_width = 0;
  982. dst_len = 0;
  983. while (1) {
  984. int w;
  985. wchar_t wc;
  986. #if ENABLE_UNICODE_USING_LOCALE
  987. {
  988. mbstate_t mbst = { 0 };
  989. ssize_t rc = mbsrtowcs(&wc, &src, 1, &mbst);
  990. /* If invalid sequence is seen: -1 is returned,
  991. * src points to the invalid sequence, errno = EILSEQ.
  992. * Else number of wchars (excluding terminating L'\0')
  993. * written to dest is returned.
  994. * If len (here: 1) non-L'\0' wchars stored at dest,
  995. * src points to the next char to be converted.
  996. * If string is completely converted: src = NULL.
  997. */
  998. if (rc == 0) /* end-of-string */
  999. break;
  1000. if (rc < 0) { /* error */
  1001. src++;
  1002. goto subst;
  1003. }
  1004. if (!iswprint(wc))
  1005. goto subst;
  1006. }
  1007. #else
  1008. src = mbstowc_internal(&wc, src);
  1009. /* src is advanced to next mb char
  1010. * wc == ERROR_WCHAR: invalid sequence is seen
  1011. * else: wc is set
  1012. */
  1013. if (wc == ERROR_WCHAR) /* error */
  1014. goto subst;
  1015. if (wc == 0) /* end-of-string */
  1016. break;
  1017. #endif
  1018. if (CONFIG_LAST_SUPPORTED_WCHAR && wc > CONFIG_LAST_SUPPORTED_WCHAR)
  1019. goto subst;
  1020. w = wcwidth(wc);
  1021. if ((ENABLE_UNICODE_COMBINING_WCHARS && w < 0) /* non-printable wchar */
  1022. || (!ENABLE_UNICODE_COMBINING_WCHARS && w <= 0)
  1023. || (!ENABLE_UNICODE_WIDE_WCHARS && w > 1)
  1024. ) {
  1025. subst:
  1026. wc = CONFIG_SUBST_WCHAR;
  1027. w = 1;
  1028. }
  1029. width -= w;
  1030. /* Note: if width == 0, we still may add more chars,
  1031. * they may be zero-width or combining ones */
  1032. if ((int)width < 0) {
  1033. /* can't add this wc, string would become longer than width */
  1034. width += w;
  1035. break;
  1036. }
  1037. uni_count++;
  1038. uni_width += w;
  1039. dst = xrealloc(dst, dst_len + MB_CUR_MAX);
  1040. #if ENABLE_UNICODE_USING_LOCALE
  1041. {
  1042. mbstate_t mbst = { 0 };
  1043. dst_len += wcrtomb(&dst[dst_len], wc, &mbst);
  1044. }
  1045. #else
  1046. dst_len += wcrtomb_internal(&dst[dst_len], wc);
  1047. #endif
  1048. }
  1049. /* Pad to remaining width */
  1050. if (flags & UNI_FLAG_PAD) {
  1051. dst = xrealloc(dst, dst_len + width + 1);
  1052. uni_count += width;
  1053. uni_width += width;
  1054. while ((int)--width >= 0) {
  1055. dst[dst_len++] = ' ';
  1056. }
  1057. }
  1058. if (!dst) /* for example, if input was "" */
  1059. dst = xzalloc(1);
  1060. dst[dst_len] = '\0';
  1061. if (stats) {
  1062. stats->byte_count = dst_len;
  1063. stats->unicode_count = uni_count;
  1064. stats->unicode_width = uni_width;
  1065. }
  1066. return dst;
  1067. }
  1068. char* FAST_FUNC unicode_conv_to_printable(uni_stat_t *stats, const char *src)
  1069. {
  1070. return unicode_conv_to_printable2(stats, src, INT_MAX, 0);
  1071. }
  1072. char* FAST_FUNC unicode_conv_to_printable_fixedwidth(/*uni_stat_t *stats,*/ const char *src, unsigned width)
  1073. {
  1074. return unicode_conv_to_printable2(/*stats:*/ NULL, src, width, UNI_FLAG_PAD);
  1075. }
  1076. #ifdef UNUSED
  1077. char* FAST_FUNC unicode_conv_to_printable_maxwidth(uni_stat_t *stats, const char *src, unsigned maxwidth)
  1078. {
  1079. return unicode_conv_to_printable2(stats, src, maxwidth, 0);
  1080. }
  1081. unsigned FAST_FUNC unicode_padding_to_width(unsigned width, const char *src)
  1082. {
  1083. if (unicode_status != UNICODE_ON) {
  1084. return width - strnlen(src, width);
  1085. }
  1086. while (1) {
  1087. int w;
  1088. wchar_t wc;
  1089. #if ENABLE_UNICODE_USING_LOCALE
  1090. {
  1091. mbstate_t mbst = { 0 };
  1092. ssize_t rc = mbsrtowcs(&wc, &src, 1, &mbst);
  1093. if (rc <= 0) /* error, or end-of-string */
  1094. return width;
  1095. }
  1096. #else
  1097. src = mbstowc_internal(&wc, src);
  1098. if (wc == ERROR_WCHAR || wc == 0) /* error, or end-of-string */
  1099. return width;
  1100. #endif
  1101. w = wcwidth(wc);
  1102. if (w < 0) /* non-printable wchar */
  1103. return width;
  1104. width -= w;
  1105. if ((int)width <= 0) /* string is longer than width */
  1106. return 0;
  1107. }
  1108. }
  1109. #endif