unicode_wcwidth.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473
  1. /*
  2. * This is an implementation of wcwidth() and wcswidth() (defined in
  3. * IEEE Std 1002.1-2001) for Unicode.
  4. *
  5. * http://www.opengroup.org/onlinepubs/007904975/functions/wcwidth.html
  6. * http://www.opengroup.org/onlinepubs/007904975/functions/wcswidth.html
  7. *
  8. * In fixed-width output devices, Latin characters all occupy a single
  9. * "cell" position of equal width, whereas ideographic CJK characters
  10. * occupy two such cells. Interoperability between terminal-line
  11. * applications and (teletype-style) character terminals using the
  12. * UTF-8 encoding requires agreement on which character should advance
  13. * the cursor by how many cell positions. No established formal
  14. * standards exist at present on which Unicode character shall occupy
  15. * how many cell positions on character terminals. These routines are
  16. * a first attempt of defining such behavior based on simple rules
  17. * applied to data provided by the Unicode Consortium.
  18. *
  19. * For some graphical characters, the Unicode standard explicitly
  20. * defines a character-cell width via the definition of the East Asian
  21. * FullWidth (F), Wide (W), Half-width (H), and Narrow (Na) classes.
  22. * In all these cases, there is no ambiguity about which width a
  23. * terminal shall use. For characters in the East Asian Ambiguous (A)
  24. * class, the width choice depends purely on a preference of backward
  25. * compatibility with either historic CJK or Western practice.
  26. * Choosing single-width for these characters is easy to justify as
  27. * the appropriate long-term solution, as the CJK practice of
  28. * displaying these characters as double-width comes from historic
  29. * implementation simplicity (8-bit encoded characters were displayed
  30. * single-width and 16-bit ones double-width, even for Greek,
  31. * Cyrillic, etc.) and not any typographic considerations.
  32. *
  33. * Much less clear is the choice of width for the Not East Asian
  34. * (Neutral) class. Existing practice does not dictate a width for any
  35. * of these characters. It would nevertheless make sense
  36. * typographically to allocate two character cells to characters such
  37. * as for instance EM SPACE or VOLUME INTEGRAL, which cannot be
  38. * represented adequately with a single-width glyph. The following
  39. * routines at present merely assign a single-cell width to all
  40. * neutral characters, in the interest of simplicity. This is not
  41. * entirely satisfactory and should be reconsidered before
  42. * establishing a formal standard in this area. At the moment, the
  43. * decision which Not East Asian (Neutral) characters should be
  44. * represented by double-width glyphs cannot yet be answered by
  45. * applying a simple rule from the Unicode database content. Setting
  46. * up a proper standard for the behavior of UTF-8 character terminals
  47. * will require a careful analysis not only of each Unicode character,
  48. * but also of each presentation form, something the author of these
  49. * routines has avoided to do so far.
  50. *
  51. * http://www.unicode.org/unicode/reports/tr11/
  52. *
  53. * Markus Kuhn -- 2007-05-26 (Unicode 5.0)
  54. *
  55. * Permission to use, copy, modify, and distribute this software
  56. * for any purpose and without fee is hereby granted. The author
  57. * disclaims all warranties with regard to this software.
  58. *
  59. * Latest version: http://www.cl.cam.ac.uk/~mgk25/ucs/wcwidth.c
  60. */
  61. struct interval {
  62. uint16_t first;
  63. uint16_t last;
  64. };
  65. /* auxiliary function for binary search in interval table */
  66. static int in_interval_table(unsigned ucs, const struct interval *table, unsigned max)
  67. {
  68. unsigned min;
  69. unsigned mid;
  70. if (ucs < table[0].first || ucs > table[max].last)
  71. return 0;
  72. min = 0;
  73. while (max >= min) {
  74. mid = (min + max) / 2;
  75. if (ucs > table[mid].last)
  76. min = mid + 1;
  77. else if (ucs < table[mid].first)
  78. max = mid - 1;
  79. else
  80. return 1;
  81. }
  82. return 0;
  83. }
  84. static int in_uint16_table(unsigned ucs, const uint16_t *table, unsigned max)
  85. {
  86. unsigned min;
  87. unsigned mid;
  88. unsigned first, last;
  89. first = table[0] >> 2;
  90. last = first + (table[0] & 3);
  91. if (ucs < first || ucs > last)
  92. return 0;
  93. min = 0;
  94. while (max >= min) {
  95. mid = (min + max) / 2;
  96. first = table[mid] >> 2;
  97. last = first + (table[mid] & 3);
  98. if (ucs > last)
  99. min = mid + 1;
  100. else if (ucs < first)
  101. max = mid - 1;
  102. else
  103. return 1;
  104. }
  105. return 0;
  106. }
  107. /* The following two functions define the column width of an ISO 10646
  108. * character as follows:
  109. *
  110. * - The null character (U+0000) has a column width of 0.
  111. *
  112. * - Other C0/C1 control characters and DEL will lead to a return
  113. * value of -1.
  114. *
  115. * - Non-spacing and enclosing combining characters (general
  116. * category code Mn or Me in the Unicode database) have a
  117. * column width of 0.
  118. *
  119. * - SOFT HYPHEN (U+00AD) has a column width of 1.
  120. *
  121. * - Other format characters (general category code Cf in the Unicode
  122. * database) and ZERO WIDTH SPACE (U+200B) have a column width of 0.
  123. *
  124. * - Hangul Jamo medial vowels and final consonants (U+1160-U+11FF)
  125. * have a column width of 0.
  126. *
  127. * - Spacing characters in the East Asian Wide (W) or East Asian
  128. * Full-width (F) category as defined in Unicode Technical
  129. * Report #11 have a column width of 2.
  130. *
  131. * - All remaining characters (including all printable
  132. * ISO 8859-1 and WGL4 characters, Unicode control characters,
  133. * etc.) have a column width of 1.
  134. *
  135. * This implementation assumes that wchar_t characters are encoded
  136. * in ISO 10646.
  137. */
  138. static int wcwidth(unsigned ucs)
  139. {
  140. /* sorted list of non-overlapping intervals of non-spacing characters */
  141. /* generated by "uniset +cat=Me +cat=Mn +cat=Cf -00AD +1160-11FF +200B c" */
  142. static const struct interval combining[] = {
  143. #define BIG_(a,b) { a, b },
  144. #define PAIR(a,b)
  145. /* PAIR if < 0x4000 and no more than 4 chars big */
  146. BIG_(0x0300, 0x036F)
  147. PAIR(0x0483, 0x0486)
  148. PAIR(0x0488, 0x0489)
  149. BIG_(0x0591, 0x05BD)
  150. PAIR(0x05BF, 0x05BF)
  151. PAIR(0x05C1, 0x05C2)
  152. PAIR(0x05C4, 0x05C5)
  153. PAIR(0x05C7, 0x05C7)
  154. PAIR(0x0600, 0x0603)
  155. BIG_(0x0610, 0x0615)
  156. BIG_(0x064B, 0x065E)
  157. PAIR(0x0670, 0x0670)
  158. BIG_(0x06D6, 0x06E4)
  159. PAIR(0x06E7, 0x06E8)
  160. PAIR(0x06EA, 0x06ED)
  161. PAIR(0x070F, 0x070F)
  162. PAIR(0x0711, 0x0711)
  163. BIG_(0x0730, 0x074A)
  164. BIG_(0x07A6, 0x07B0)
  165. BIG_(0x07EB, 0x07F3)
  166. PAIR(0x0901, 0x0902)
  167. PAIR(0x093C, 0x093C)
  168. BIG_(0x0941, 0x0948)
  169. PAIR(0x094D, 0x094D)
  170. PAIR(0x0951, 0x0954)
  171. PAIR(0x0962, 0x0963)
  172. PAIR(0x0981, 0x0981)
  173. PAIR(0x09BC, 0x09BC)
  174. PAIR(0x09C1, 0x09C4)
  175. PAIR(0x09CD, 0x09CD)
  176. PAIR(0x09E2, 0x09E3)
  177. PAIR(0x0A01, 0x0A02)
  178. PAIR(0x0A3C, 0x0A3C)
  179. PAIR(0x0A41, 0x0A42)
  180. PAIR(0x0A47, 0x0A48)
  181. PAIR(0x0A4B, 0x0A4D)
  182. PAIR(0x0A70, 0x0A71)
  183. PAIR(0x0A81, 0x0A82)
  184. PAIR(0x0ABC, 0x0ABC)
  185. BIG_(0x0AC1, 0x0AC5)
  186. PAIR(0x0AC7, 0x0AC8)
  187. PAIR(0x0ACD, 0x0ACD)
  188. PAIR(0x0AE2, 0x0AE3)
  189. PAIR(0x0B01, 0x0B01)
  190. PAIR(0x0B3C, 0x0B3C)
  191. PAIR(0x0B3F, 0x0B3F)
  192. PAIR(0x0B41, 0x0B43)
  193. PAIR(0x0B4D, 0x0B4D)
  194. PAIR(0x0B56, 0x0B56)
  195. PAIR(0x0B82, 0x0B82)
  196. PAIR(0x0BC0, 0x0BC0)
  197. PAIR(0x0BCD, 0x0BCD)
  198. PAIR(0x0C3E, 0x0C40)
  199. PAIR(0x0C46, 0x0C48)
  200. PAIR(0x0C4A, 0x0C4D)
  201. PAIR(0x0C55, 0x0C56)
  202. PAIR(0x0CBC, 0x0CBC)
  203. PAIR(0x0CBF, 0x0CBF)
  204. PAIR(0x0CC6, 0x0CC6)
  205. PAIR(0x0CCC, 0x0CCD)
  206. PAIR(0x0CE2, 0x0CE3)
  207. PAIR(0x0D41, 0x0D43)
  208. PAIR(0x0D4D, 0x0D4D)
  209. PAIR(0x0DCA, 0x0DCA)
  210. PAIR(0x0DD2, 0x0DD4)
  211. PAIR(0x0DD6, 0x0DD6)
  212. PAIR(0x0E31, 0x0E31)
  213. BIG_(0x0E34, 0x0E3A)
  214. BIG_(0x0E47, 0x0E4E)
  215. PAIR(0x0EB1, 0x0EB1)
  216. BIG_(0x0EB4, 0x0EB9)
  217. PAIR(0x0EBB, 0x0EBC)
  218. BIG_(0x0EC8, 0x0ECD)
  219. PAIR(0x0F18, 0x0F19)
  220. PAIR(0x0F35, 0x0F35)
  221. PAIR(0x0F37, 0x0F37)
  222. PAIR(0x0F39, 0x0F39)
  223. BIG_(0x0F71, 0x0F7E)
  224. BIG_(0x0F80, 0x0F84)
  225. PAIR(0x0F86, 0x0F87)
  226. PAIR(0x0FC6, 0x0FC6)
  227. BIG_(0x0F90, 0x0F97)
  228. BIG_(0x0F99, 0x0FBC)
  229. PAIR(0x102D, 0x1030)
  230. PAIR(0x1032, 0x1032)
  231. PAIR(0x1036, 0x1037)
  232. PAIR(0x1039, 0x1039)
  233. PAIR(0x1058, 0x1059)
  234. BIG_(0x1160, 0x11FF)
  235. PAIR(0x135F, 0x135F)
  236. PAIR(0x1712, 0x1714)
  237. PAIR(0x1732, 0x1734)
  238. PAIR(0x1752, 0x1753)
  239. PAIR(0x1772, 0x1773)
  240. PAIR(0x17B4, 0x17B5)
  241. BIG_(0x17B7, 0x17BD)
  242. PAIR(0x17C6, 0x17C6)
  243. BIG_(0x17C9, 0x17D3)
  244. PAIR(0x17DD, 0x17DD)
  245. PAIR(0x180B, 0x180D)
  246. PAIR(0x18A9, 0x18A9)
  247. PAIR(0x1920, 0x1922)
  248. PAIR(0x1927, 0x1928)
  249. PAIR(0x1932, 0x1932)
  250. PAIR(0x1939, 0x193B)
  251. PAIR(0x1A17, 0x1A18)
  252. PAIR(0x1B00, 0x1B03)
  253. PAIR(0x1B34, 0x1B34)
  254. BIG_(0x1B36, 0x1B3A)
  255. PAIR(0x1B3C, 0x1B3C)
  256. PAIR(0x1B42, 0x1B42)
  257. BIG_(0x1B6B, 0x1B73)
  258. BIG_(0x1DC0, 0x1DCA)
  259. PAIR(0x1DFE, 0x1DFF)
  260. BIG_(0x200B, 0x200F)
  261. BIG_(0x202A, 0x202E)
  262. PAIR(0x2060, 0x2063)
  263. BIG_(0x206A, 0x206F)
  264. BIG_(0x20D0, 0x20EF)
  265. BIG_(0x302A, 0x302F)
  266. PAIR(0x3099, 0x309A)
  267. /* Too big to be packed in PAIRs: */
  268. { 0xA806, 0xA806 },
  269. { 0xA80B, 0xA80B },
  270. { 0xA825, 0xA826 },
  271. { 0xFB1E, 0xFB1E },
  272. { 0xFE00, 0xFE0F },
  273. { 0xFE20, 0xFE23 },
  274. { 0xFEFF, 0xFEFF },
  275. { 0xFFF9, 0xFFFB }
  276. #undef BIG_
  277. #undef PAIR
  278. };
  279. static const uint16_t combining1[] = {
  280. #define BIG_(a,b)
  281. #define PAIR(a,b) (a << 2) | (b-a),
  282. /* Exact copy-n-paste of the above: */
  283. BIG_(0x0300, 0x036F)
  284. PAIR(0x0483, 0x0486)
  285. PAIR(0x0488, 0x0489)
  286. BIG_(0x0591, 0x05BD)
  287. PAIR(0x05BF, 0x05BF)
  288. PAIR(0x05C1, 0x05C2)
  289. PAIR(0x05C4, 0x05C5)
  290. PAIR(0x05C7, 0x05C7)
  291. PAIR(0x0600, 0x0603)
  292. BIG_(0x0610, 0x0615)
  293. BIG_(0x064B, 0x065E)
  294. PAIR(0x0670, 0x0670)
  295. BIG_(0x06D6, 0x06E4)
  296. PAIR(0x06E7, 0x06E8)
  297. PAIR(0x06EA, 0x06ED)
  298. PAIR(0x070F, 0x070F)
  299. PAIR(0x0711, 0x0711)
  300. BIG_(0x0730, 0x074A)
  301. BIG_(0x07A6, 0x07B0)
  302. BIG_(0x07EB, 0x07F3)
  303. PAIR(0x0901, 0x0902)
  304. PAIR(0x093C, 0x093C)
  305. BIG_(0x0941, 0x0948)
  306. PAIR(0x094D, 0x094D)
  307. PAIR(0x0951, 0x0954)
  308. PAIR(0x0962, 0x0963)
  309. PAIR(0x0981, 0x0981)
  310. PAIR(0x09BC, 0x09BC)
  311. PAIR(0x09C1, 0x09C4)
  312. PAIR(0x09CD, 0x09CD)
  313. PAIR(0x09E2, 0x09E3)
  314. PAIR(0x0A01, 0x0A02)
  315. PAIR(0x0A3C, 0x0A3C)
  316. PAIR(0x0A41, 0x0A42)
  317. PAIR(0x0A47, 0x0A48)
  318. PAIR(0x0A4B, 0x0A4D)
  319. PAIR(0x0A70, 0x0A71)
  320. PAIR(0x0A81, 0x0A82)
  321. PAIR(0x0ABC, 0x0ABC)
  322. BIG_(0x0AC1, 0x0AC5)
  323. PAIR(0x0AC7, 0x0AC8)
  324. PAIR(0x0ACD, 0x0ACD)
  325. PAIR(0x0AE2, 0x0AE3)
  326. PAIR(0x0B01, 0x0B01)
  327. PAIR(0x0B3C, 0x0B3C)
  328. PAIR(0x0B3F, 0x0B3F)
  329. PAIR(0x0B41, 0x0B43)
  330. PAIR(0x0B4D, 0x0B4D)
  331. PAIR(0x0B56, 0x0B56)
  332. PAIR(0x0B82, 0x0B82)
  333. PAIR(0x0BC0, 0x0BC0)
  334. PAIR(0x0BCD, 0x0BCD)
  335. PAIR(0x0C3E, 0x0C40)
  336. PAIR(0x0C46, 0x0C48)
  337. PAIR(0x0C4A, 0x0C4D)
  338. PAIR(0x0C55, 0x0C56)
  339. PAIR(0x0CBC, 0x0CBC)
  340. PAIR(0x0CBF, 0x0CBF)
  341. PAIR(0x0CC6, 0x0CC6)
  342. PAIR(0x0CCC, 0x0CCD)
  343. PAIR(0x0CE2, 0x0CE3)
  344. PAIR(0x0D41, 0x0D43)
  345. PAIR(0x0D4D, 0x0D4D)
  346. PAIR(0x0DCA, 0x0DCA)
  347. PAIR(0x0DD2, 0x0DD4)
  348. PAIR(0x0DD6, 0x0DD6)
  349. PAIR(0x0E31, 0x0E31)
  350. BIG_(0x0E34, 0x0E3A)
  351. BIG_(0x0E47, 0x0E4E)
  352. PAIR(0x0EB1, 0x0EB1)
  353. BIG_(0x0EB4, 0x0EB9)
  354. PAIR(0x0EBB, 0x0EBC)
  355. BIG_(0x0EC8, 0x0ECD)
  356. PAIR(0x0F18, 0x0F19)
  357. PAIR(0x0F35, 0x0F35)
  358. PAIR(0x0F37, 0x0F37)
  359. PAIR(0x0F39, 0x0F39)
  360. BIG_(0x0F71, 0x0F7E)
  361. BIG_(0x0F80, 0x0F84)
  362. PAIR(0x0F86, 0x0F87)
  363. PAIR(0x0FC6, 0x0FC6)
  364. BIG_(0x0F90, 0x0F97)
  365. BIG_(0x0F99, 0x0FBC)
  366. PAIR(0x102D, 0x1030)
  367. PAIR(0x1032, 0x1032)
  368. PAIR(0x1036, 0x1037)
  369. PAIR(0x1039, 0x1039)
  370. PAIR(0x1058, 0x1059)
  371. BIG_(0x1160, 0x11FF)
  372. PAIR(0x135F, 0x135F)
  373. PAIR(0x1712, 0x1714)
  374. PAIR(0x1732, 0x1734)
  375. PAIR(0x1752, 0x1753)
  376. PAIR(0x1772, 0x1773)
  377. PAIR(0x17B4, 0x17B5)
  378. BIG_(0x17B7, 0x17BD)
  379. PAIR(0x17C6, 0x17C6)
  380. BIG_(0x17C9, 0x17D3)
  381. PAIR(0x17DD, 0x17DD)
  382. PAIR(0x180B, 0x180D)
  383. PAIR(0x18A9, 0x18A9)
  384. PAIR(0x1920, 0x1922)
  385. PAIR(0x1927, 0x1928)
  386. PAIR(0x1932, 0x1932)
  387. PAIR(0x1939, 0x193B)
  388. PAIR(0x1A17, 0x1A18)
  389. PAIR(0x1B00, 0x1B03)
  390. PAIR(0x1B34, 0x1B34)
  391. BIG_(0x1B36, 0x1B3A)
  392. PAIR(0x1B3C, 0x1B3C)
  393. PAIR(0x1B42, 0x1B42)
  394. BIG_(0x1B6B, 0x1B73)
  395. BIG_(0x1DC0, 0x1DCA)
  396. PAIR(0x1DFE, 0x1DFF)
  397. BIG_(0x200B, 0x200F)
  398. BIG_(0x202A, 0x202E)
  399. PAIR(0x2060, 0x2063)
  400. BIG_(0x206A, 0x206F)
  401. BIG_(0x20D0, 0x20EF)
  402. BIG_(0x302A, 0x302F)
  403. PAIR(0x3099, 0x309A)
  404. #undef BIG_
  405. #undef PAIR
  406. };
  407. struct CHECK {
  408. #define BIG_(a,b) char big##a[b-a <= 3 ? -1 : 1];
  409. #define PAIR(a,b) char pair##a[b-a > 3 ? -1 : 1];
  410. /* Copy-n-paste it here again to verify correctness */
  411. #undef BIG_
  412. #undef PAIR
  413. };
  414. static const struct interval combining0x10000[] = {
  415. { 0x0A01, 0x0A03 }, { 0x0A05, 0x0A06 }, { 0x0A0C, 0x0A0F },
  416. { 0x0A38, 0x0A3A }, { 0x0A3F, 0x0A3F }, { 0xD167, 0xD169 },
  417. { 0xD173, 0xD182 }, { 0xD185, 0xD18B }, { 0xD1AA, 0xD1AD },
  418. { 0xD242, 0xD244 }
  419. };
  420. if (ucs == 0)
  421. return 0;
  422. /* test for 8-bit control characters (00-1f, 80-9f, 7f) */
  423. if ((ucs & ~0x80) < 0x20 || ucs == 0x7f)
  424. return -1;
  425. if (ucs < 0x0300) /* optimization */
  426. return 1;
  427. /* binary search in table of non-spacing characters */
  428. if (in_interval_table(ucs, combining, ARRAY_SIZE(combining) - 1))
  429. return 0;
  430. if (in_uint16_table(ucs, combining1, ARRAY_SIZE(combining1) - 1))
  431. return 0;
  432. if (ucs < 0x1100) /* optimization */
  433. return 1;
  434. /* binary search in table of non-spacing characters, cont. */
  435. if (in_interval_table(ucs ^ 0x10000, combining0x10000, ARRAY_SIZE(combining0x10000) - 1))
  436. return 0;
  437. if (ucs == 0xE0001
  438. || (ucs >= 0xE0020 && ucs <= 0xE007F)
  439. || (ucs >= 0xE0100 && ucs <= 0xE01EF)
  440. ) {
  441. return 0;
  442. }
  443. /* if we arrive here, ucs is not a combining or C0/C1 control character */
  444. return 1 +
  445. ( (/*ucs >= 0x1100 &&*/ ucs <= 0x115f) /* Hangul Jamo init. consonants */
  446. || ucs == 0x2329
  447. || ucs == 0x232a
  448. || (ucs >= 0x2e80 && ucs <= 0xa4cf && ucs != 0x303f) /* CJK ... Yi */
  449. || (ucs >= 0xac00 && ucs <= 0xd7a3) /* Hangul Syllables */
  450. || (ucs >= 0xf900 && ucs <= 0xfaff) /* CJK Compatibility Ideographs */
  451. || (ucs >= 0xfe10 && ucs <= 0xfe19) /* Vertical forms */
  452. || (ucs >= 0xfe30 && ucs <= 0xfe6f) /* CJK Compatibility Forms */
  453. || (ucs >= 0xff00 && ucs <= 0xff60) /* Fullwidth Forms */
  454. || (ucs >= 0xffe0 && ucs <= 0xffe6)
  455. || (ucs >= 0x20000 && ucs <= 0x2fffd)
  456. || (ucs >= 0x30000 && ucs <= 0x3fffd)
  457. );
  458. }