unicode.c 31 KB

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