3
0

unicode.c 32 KB

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