runewidth.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481
  1. package runewidth
  2. var (
  3. // EastAsianWidth will be set true if the current locale is CJK
  4. EastAsianWidth = IsEastAsian()
  5. // DefaultCondition is a condition in current locale
  6. DefaultCondition = &Condition{EastAsianWidth}
  7. )
  8. type interval struct {
  9. first rune
  10. last rune
  11. }
  12. var combining = []interval{
  13. {0x0300, 0x036F}, {0x0483, 0x0486}, {0x0488, 0x0489},
  14. {0x0591, 0x05BD}, {0x05BF, 0x05BF}, {0x05C1, 0x05C2},
  15. {0x05C4, 0x05C5}, {0x05C7, 0x05C7}, {0x0600, 0x0603},
  16. {0x0610, 0x0615}, {0x064B, 0x065E}, {0x0670, 0x0670},
  17. {0x06D6, 0x06E4}, {0x06E7, 0x06E8}, {0x06EA, 0x06ED},
  18. {0x070F, 0x070F}, {0x0711, 0x0711}, {0x0730, 0x074A},
  19. {0x07A6, 0x07B0}, {0x07EB, 0x07F3}, {0x0901, 0x0902},
  20. {0x093C, 0x093C}, {0x0941, 0x0948}, {0x094D, 0x094D},
  21. {0x0951, 0x0954}, {0x0962, 0x0963}, {0x0981, 0x0981},
  22. {0x09BC, 0x09BC}, {0x09C1, 0x09C4}, {0x09CD, 0x09CD},
  23. {0x09E2, 0x09E3}, {0x0A01, 0x0A02}, {0x0A3C, 0x0A3C},
  24. {0x0A41, 0x0A42}, {0x0A47, 0x0A48}, {0x0A4B, 0x0A4D},
  25. {0x0A70, 0x0A71}, {0x0A81, 0x0A82}, {0x0ABC, 0x0ABC},
  26. {0x0AC1, 0x0AC5}, {0x0AC7, 0x0AC8}, {0x0ACD, 0x0ACD},
  27. {0x0AE2, 0x0AE3}, {0x0B01, 0x0B01}, {0x0B3C, 0x0B3C},
  28. {0x0B3F, 0x0B3F}, {0x0B41, 0x0B43}, {0x0B4D, 0x0B4D},
  29. {0x0B56, 0x0B56}, {0x0B82, 0x0B82}, {0x0BC0, 0x0BC0},
  30. {0x0BCD, 0x0BCD}, {0x0C3E, 0x0C40}, {0x0C46, 0x0C48},
  31. {0x0C4A, 0x0C4D}, {0x0C55, 0x0C56}, {0x0CBC, 0x0CBC},
  32. {0x0CBF, 0x0CBF}, {0x0CC6, 0x0CC6}, {0x0CCC, 0x0CCD},
  33. {0x0CE2, 0x0CE3}, {0x0D41, 0x0D43}, {0x0D4D, 0x0D4D},
  34. {0x0DCA, 0x0DCA}, {0x0DD2, 0x0DD4}, {0x0DD6, 0x0DD6},
  35. {0x0E31, 0x0E31}, {0x0E34, 0x0E3A}, {0x0E47, 0x0E4E},
  36. {0x0EB1, 0x0EB1}, {0x0EB4, 0x0EB9}, {0x0EBB, 0x0EBC},
  37. {0x0EC8, 0x0ECD}, {0x0F18, 0x0F19}, {0x0F35, 0x0F35},
  38. {0x0F37, 0x0F37}, {0x0F39, 0x0F39}, {0x0F71, 0x0F7E},
  39. {0x0F80, 0x0F84}, {0x0F86, 0x0F87}, {0x0F90, 0x0F97},
  40. {0x0F99, 0x0FBC}, {0x0FC6, 0x0FC6}, {0x102D, 0x1030},
  41. {0x1032, 0x1032}, {0x1036, 0x1037}, {0x1039, 0x1039},
  42. {0x1058, 0x1059}, {0x1160, 0x11FF}, {0x135F, 0x135F},
  43. {0x1712, 0x1714}, {0x1732, 0x1734}, {0x1752, 0x1753},
  44. {0x1772, 0x1773}, {0x17B4, 0x17B5}, {0x17B7, 0x17BD},
  45. {0x17C6, 0x17C6}, {0x17C9, 0x17D3}, {0x17DD, 0x17DD},
  46. {0x180B, 0x180D}, {0x18A9, 0x18A9}, {0x1920, 0x1922},
  47. {0x1927, 0x1928}, {0x1932, 0x1932}, {0x1939, 0x193B},
  48. {0x1A17, 0x1A18}, {0x1B00, 0x1B03}, {0x1B34, 0x1B34},
  49. {0x1B36, 0x1B3A}, {0x1B3C, 0x1B3C}, {0x1B42, 0x1B42},
  50. {0x1B6B, 0x1B73}, {0x1DC0, 0x1DCA}, {0x1DFE, 0x1DFF},
  51. {0x200B, 0x200F}, {0x202A, 0x202E}, {0x2060, 0x2063},
  52. {0x206A, 0x206F}, {0x20D0, 0x20EF}, {0x302A, 0x302F},
  53. {0x3099, 0x309A}, {0xA806, 0xA806}, {0xA80B, 0xA80B},
  54. {0xA825, 0xA826}, {0xFB1E, 0xFB1E}, {0xFE00, 0xFE0F},
  55. {0xFE20, 0xFE23}, {0xFEFF, 0xFEFF}, {0xFFF9, 0xFFFB},
  56. {0x10A01, 0x10A03}, {0x10A05, 0x10A06}, {0x10A0C, 0x10A0F},
  57. {0x10A38, 0x10A3A}, {0x10A3F, 0x10A3F}, {0x1D167, 0x1D169},
  58. {0x1D173, 0x1D182}, {0x1D185, 0x1D18B}, {0x1D1AA, 0x1D1AD},
  59. {0x1D242, 0x1D244}, {0xE0001, 0xE0001}, {0xE0020, 0xE007F},
  60. {0xE0100, 0xE01EF},
  61. }
  62. type ctype int
  63. const (
  64. narrow ctype = iota
  65. ambiguous
  66. wide
  67. halfwidth
  68. fullwidth
  69. neutral
  70. )
  71. type intervalType struct {
  72. first rune
  73. last rune
  74. ctype ctype
  75. }
  76. var ctypes = []intervalType{
  77. {0x0020, 0x007E, narrow},
  78. {0x00A1, 0x00A1, ambiguous},
  79. {0x00A2, 0x00A3, narrow},
  80. {0x00A4, 0x00A4, ambiguous},
  81. {0x00A5, 0x00A6, narrow},
  82. {0x00A7, 0x00A8, ambiguous},
  83. {0x00AA, 0x00AA, ambiguous},
  84. {0x00AC, 0x00AC, narrow},
  85. {0x00AD, 0x00AE, ambiguous},
  86. {0x00AF, 0x00AF, narrow},
  87. {0x00B0, 0x00B4, ambiguous},
  88. {0x00B6, 0x00BA, ambiguous},
  89. {0x00BC, 0x00BF, ambiguous},
  90. {0x00C6, 0x00C6, ambiguous},
  91. {0x00D0, 0x00D0, ambiguous},
  92. {0x00D7, 0x00D8, ambiguous},
  93. {0x00DE, 0x00E1, ambiguous},
  94. {0x00E6, 0x00E6, ambiguous},
  95. {0x00E8, 0x00EA, ambiguous},
  96. {0x00EC, 0x00ED, ambiguous},
  97. {0x00F0, 0x00F0, ambiguous},
  98. {0x00F2, 0x00F3, ambiguous},
  99. {0x00F7, 0x00FA, ambiguous},
  100. {0x00FC, 0x00FC, ambiguous},
  101. {0x00FE, 0x00FE, ambiguous},
  102. {0x0101, 0x0101, ambiguous},
  103. {0x0111, 0x0111, ambiguous},
  104. {0x0113, 0x0113, ambiguous},
  105. {0x011B, 0x011B, ambiguous},
  106. {0x0126, 0x0127, ambiguous},
  107. {0x012B, 0x012B, ambiguous},
  108. {0x0131, 0x0133, ambiguous},
  109. {0x0138, 0x0138, ambiguous},
  110. {0x013F, 0x0142, ambiguous},
  111. {0x0144, 0x0144, ambiguous},
  112. {0x0148, 0x014B, ambiguous},
  113. {0x014D, 0x014D, ambiguous},
  114. {0x0152, 0x0153, ambiguous},
  115. {0x0166, 0x0167, ambiguous},
  116. {0x016B, 0x016B, ambiguous},
  117. {0x01CE, 0x01CE, ambiguous},
  118. {0x01D0, 0x01D0, ambiguous},
  119. {0x01D2, 0x01D2, ambiguous},
  120. {0x01D4, 0x01D4, ambiguous},
  121. {0x01D6, 0x01D6, ambiguous},
  122. {0x01D8, 0x01D8, ambiguous},
  123. {0x01DA, 0x01DA, ambiguous},
  124. {0x01DC, 0x01DC, ambiguous},
  125. {0x0251, 0x0251, ambiguous},
  126. {0x0261, 0x0261, ambiguous},
  127. {0x02C4, 0x02C4, ambiguous},
  128. {0x02C7, 0x02C7, ambiguous},
  129. {0x02C9, 0x02CB, ambiguous},
  130. {0x02CD, 0x02CD, ambiguous},
  131. {0x02D0, 0x02D0, ambiguous},
  132. {0x02D8, 0x02DB, ambiguous},
  133. {0x02DD, 0x02DD, ambiguous},
  134. {0x02DF, 0x02DF, ambiguous},
  135. {0x0300, 0x036F, ambiguous},
  136. {0x0391, 0x03A2, ambiguous},
  137. {0x03A3, 0x03A9, ambiguous},
  138. {0x03B1, 0x03C1, ambiguous},
  139. {0x03C3, 0x03C9, ambiguous},
  140. {0x0401, 0x0401, ambiguous},
  141. {0x0410, 0x044F, ambiguous},
  142. {0x0451, 0x0451, ambiguous},
  143. {0x1100, 0x115F, wide},
  144. {0x2010, 0x2010, ambiguous},
  145. {0x2013, 0x2016, ambiguous},
  146. {0x2018, 0x2019, ambiguous},
  147. {0x201C, 0x201D, ambiguous},
  148. {0x2020, 0x2022, ambiguous},
  149. {0x2024, 0x2027, ambiguous},
  150. {0x2030, 0x2030, ambiguous},
  151. {0x2032, 0x2033, ambiguous},
  152. {0x2035, 0x2035, ambiguous},
  153. {0x203B, 0x203B, ambiguous},
  154. {0x203E, 0x203E, ambiguous},
  155. {0x2074, 0x2074, ambiguous},
  156. {0x207F, 0x207F, ambiguous},
  157. {0x2081, 0x2084, ambiguous},
  158. {0x20A9, 0x20A9, halfwidth},
  159. {0x20AC, 0x20AC, ambiguous},
  160. {0x2103, 0x2103, ambiguous},
  161. {0x2105, 0x2105, ambiguous},
  162. {0x2109, 0x2109, ambiguous},
  163. {0x2113, 0x2113, ambiguous},
  164. {0x2116, 0x2116, ambiguous},
  165. {0x2121, 0x2122, ambiguous},
  166. {0x2126, 0x2126, ambiguous},
  167. {0x212B, 0x212B, ambiguous},
  168. {0x2153, 0x2154, ambiguous},
  169. {0x215B, 0x215E, ambiguous},
  170. {0x2160, 0x216B, ambiguous},
  171. {0x2170, 0x2179, ambiguous},
  172. {0x2189, 0x218A, ambiguous},
  173. {0x2190, 0x2199, ambiguous},
  174. {0x21B8, 0x21B9, ambiguous},
  175. {0x21D2, 0x21D2, ambiguous},
  176. {0x21D4, 0x21D4, ambiguous},
  177. {0x21E7, 0x21E7, ambiguous},
  178. {0x2200, 0x2200, ambiguous},
  179. {0x2202, 0x2203, ambiguous},
  180. {0x2207, 0x2208, ambiguous},
  181. {0x220B, 0x220B, ambiguous},
  182. {0x220F, 0x220F, ambiguous},
  183. {0x2211, 0x2211, ambiguous},
  184. {0x2215, 0x2215, ambiguous},
  185. {0x221A, 0x221A, ambiguous},
  186. {0x221D, 0x2220, ambiguous},
  187. {0x2223, 0x2223, ambiguous},
  188. {0x2225, 0x2225, ambiguous},
  189. {0x2227, 0x222C, ambiguous},
  190. {0x222E, 0x222E, ambiguous},
  191. {0x2234, 0x2237, ambiguous},
  192. {0x223C, 0x223D, ambiguous},
  193. {0x2248, 0x2248, ambiguous},
  194. {0x224C, 0x224C, ambiguous},
  195. {0x2252, 0x2252, ambiguous},
  196. {0x2260, 0x2261, ambiguous},
  197. {0x2264, 0x2267, ambiguous},
  198. {0x226A, 0x226B, ambiguous},
  199. {0x226E, 0x226F, ambiguous},
  200. {0x2282, 0x2283, ambiguous},
  201. {0x2286, 0x2287, ambiguous},
  202. {0x2295, 0x2295, ambiguous},
  203. {0x2299, 0x2299, ambiguous},
  204. {0x22A5, 0x22A5, ambiguous},
  205. {0x22BF, 0x22BF, ambiguous},
  206. {0x2312, 0x2312, ambiguous},
  207. {0x2329, 0x232A, wide},
  208. {0x2460, 0x24E9, ambiguous},
  209. {0x24EB, 0x254B, ambiguous},
  210. {0x2550, 0x2573, ambiguous},
  211. {0x2580, 0x258F, ambiguous},
  212. {0x2592, 0x2595, ambiguous},
  213. {0x25A0, 0x25A1, ambiguous},
  214. {0x25A3, 0x25A9, ambiguous},
  215. {0x25B2, 0x25B3, ambiguous},
  216. {0x25B6, 0x25B7, ambiguous},
  217. {0x25BC, 0x25BD, ambiguous},
  218. {0x25C0, 0x25C1, ambiguous},
  219. {0x25C6, 0x25C8, ambiguous},
  220. {0x25CB, 0x25CB, ambiguous},
  221. {0x25CE, 0x25D1, ambiguous},
  222. {0x25E2, 0x25E5, ambiguous},
  223. {0x25EF, 0x25EF, ambiguous},
  224. {0x2605, 0x2606, ambiguous},
  225. {0x2609, 0x2609, ambiguous},
  226. {0x260E, 0x260F, ambiguous},
  227. {0x2614, 0x2615, ambiguous},
  228. {0x261C, 0x261C, ambiguous},
  229. {0x261E, 0x261E, ambiguous},
  230. {0x2640, 0x2640, ambiguous},
  231. {0x2642, 0x2642, ambiguous},
  232. {0x2660, 0x2661, ambiguous},
  233. {0x2663, 0x2665, ambiguous},
  234. {0x2667, 0x266A, ambiguous},
  235. {0x266C, 0x266D, ambiguous},
  236. {0x266F, 0x266F, ambiguous},
  237. {0x269E, 0x269F, ambiguous},
  238. {0x26BE, 0x26BF, ambiguous},
  239. {0x26C4, 0x26CD, ambiguous},
  240. {0x26CF, 0x26E1, ambiguous},
  241. {0x26E3, 0x26E3, ambiguous},
  242. {0x26E8, 0x26FF, ambiguous},
  243. {0x273D, 0x273D, ambiguous},
  244. {0x2757, 0x2757, ambiguous},
  245. {0x2776, 0x277F, ambiguous},
  246. {0x27E6, 0x27ED, narrow},
  247. {0x2985, 0x2986, narrow},
  248. {0x2B55, 0x2B59, ambiguous},
  249. {0x2E80, 0x2E9A, wide},
  250. {0x2E9B, 0x2EF4, wide},
  251. {0x2F00, 0x2FD6, wide},
  252. {0x2FF0, 0x2FFC, wide},
  253. {0x3000, 0x3000, fullwidth},
  254. {0x3001, 0x303E, wide},
  255. {0x3041, 0x3097, wide},
  256. {0x3099, 0x3100, wide},
  257. {0x3105, 0x312E, wide},
  258. {0x3131, 0x318F, wide},
  259. {0x3190, 0x31BB, wide},
  260. {0x31C0, 0x31E4, wide},
  261. {0x31F0, 0x321F, wide},
  262. {0x3220, 0x3247, wide},
  263. {0x3248, 0x324F, ambiguous},
  264. {0x3250, 0x32FF, wide},
  265. {0x3300, 0x4DBF, wide},
  266. {0x4E00, 0xA48D, wide},
  267. {0xA490, 0xA4C7, wide},
  268. {0xA960, 0xA97D, wide},
  269. {0xAC00, 0xD7A4, wide},
  270. {0xE000, 0xF8FF, ambiguous},
  271. {0xF900, 0xFAFF, wide},
  272. {0xFE00, 0xFE0F, ambiguous},
  273. {0xFE10, 0xFE1A, wide},
  274. {0xFE30, 0xFE53, wide},
  275. {0xFE54, 0xFE67, wide},
  276. {0xFE68, 0xFE6C, wide},
  277. {0xFF01, 0xFF60, fullwidth},
  278. {0xFF61, 0xFFBF, halfwidth},
  279. {0xFFC2, 0xFFC8, halfwidth},
  280. {0xFFCA, 0xFFD0, halfwidth},
  281. {0xFFD2, 0xFFD8, halfwidth},
  282. {0xFFDA, 0xFFDD, halfwidth},
  283. {0xFFE0, 0xFFE7, fullwidth},
  284. {0xFFE8, 0xFFEF, halfwidth},
  285. {0xFFFD, 0xFFFE, ambiguous},
  286. {0x1B000, 0x1B002, wide},
  287. {0x1F100, 0x1F10A, ambiguous},
  288. {0x1F110, 0x1F12D, ambiguous},
  289. {0x1F130, 0x1F169, ambiguous},
  290. {0x1F170, 0x1F19B, ambiguous},
  291. {0x1F200, 0x1F203, wide},
  292. {0x1F210, 0x1F23B, wide},
  293. {0x1F240, 0x1F249, wide},
  294. {0x1F250, 0x1F252, wide},
  295. {0x20000, 0x2FFFE, wide},
  296. {0x30000, 0x3FFFE, wide},
  297. {0xE0100, 0xE01F0, ambiguous},
  298. {0xF0000, 0xFFFFD, ambiguous},
  299. {0x100000, 0x10FFFE, ambiguous},
  300. }
  301. // Condition have flag EastAsianWidth whether the current locale is CJK or not.
  302. type Condition struct {
  303. EastAsianWidth bool
  304. }
  305. // NewCondition return new instance of Condition which is current locale.
  306. func NewCondition() *Condition {
  307. return &Condition{EastAsianWidth}
  308. }
  309. // RuneWidth returns the number of cells in r.
  310. // See http://www.unicode.org/reports/tr11/
  311. func (c *Condition) RuneWidth(r rune) int {
  312. if r == 0 {
  313. return 0
  314. }
  315. if r < 32 || (r >= 0x7f && r < 0xa0) {
  316. return 1
  317. }
  318. for _, iv := range combining {
  319. if iv.first <= r && r <= iv.last {
  320. return 0
  321. }
  322. }
  323. if c.EastAsianWidth && IsAmbiguousWidth(r) {
  324. return 2
  325. }
  326. if r >= 0x1100 &&
  327. (r <= 0x115f || r == 0x2329 || r == 0x232a ||
  328. (r >= 0x2e80 && r <= 0xa4cf && r != 0x303f) ||
  329. (r >= 0xac00 && r <= 0xd7a3) ||
  330. (r >= 0xf900 && r <= 0xfaff) ||
  331. (r >= 0xfe30 && r <= 0xfe6f) ||
  332. (r >= 0xff00 && r <= 0xff60) ||
  333. (r >= 0xffe0 && r <= 0xffe6) ||
  334. (r >= 0x20000 && r <= 0x2fffd) ||
  335. (r >= 0x30000 && r <= 0x3fffd)) {
  336. return 2
  337. }
  338. return 1
  339. }
  340. // StringWidth return width as you can see
  341. func (c *Condition) StringWidth(s string) (width int) {
  342. for _, r := range []rune(s) {
  343. width += c.RuneWidth(r)
  344. }
  345. return width
  346. }
  347. // Truncate return string truncated with w cells
  348. func (c *Condition) Truncate(s string, w int, tail string) string {
  349. if c.StringWidth(s) <= w {
  350. return s
  351. }
  352. r := []rune(s)
  353. tw := c.StringWidth(tail)
  354. w -= tw
  355. width := 0
  356. i := 0
  357. for ; i < len(r); i++ {
  358. cw := c.RuneWidth(r[i])
  359. if width+cw > w {
  360. break
  361. }
  362. width += cw
  363. }
  364. return string(r[0:i]) + tail
  365. }
  366. // Wrap return string wrapped with w cells
  367. func (c *Condition) Wrap(s string, w int) string {
  368. width := 0
  369. out := ""
  370. for _, r := range []rune(s) {
  371. cw := RuneWidth(r)
  372. if r == '\n' {
  373. out += string(r)
  374. width = 0
  375. continue
  376. } else if width+cw > w {
  377. out += "\n"
  378. width = 0
  379. out += string(r)
  380. width += cw
  381. continue
  382. }
  383. out += string(r)
  384. width += cw
  385. }
  386. return out
  387. }
  388. // FillLeft return string filled in left by spaces in w cells
  389. func (c *Condition) FillLeft(s string, w int) string {
  390. width := c.StringWidth(s)
  391. count := w - width
  392. if count > 0 {
  393. b := make([]byte, count)
  394. for i := range b {
  395. b[i] = ' '
  396. }
  397. return string(b) + s
  398. }
  399. return s
  400. }
  401. // FillRight return string filled in left by spaces in w cells
  402. func (c *Condition) FillRight(s string, w int) string {
  403. width := c.StringWidth(s)
  404. count := w - width
  405. if count > 0 {
  406. b := make([]byte, count)
  407. for i := range b {
  408. b[i] = ' '
  409. }
  410. return s + string(b)
  411. }
  412. return s
  413. }
  414. // RuneWidth returns the number of cells in r.
  415. // See http://www.unicode.org/reports/tr11/
  416. func RuneWidth(r rune) int {
  417. return DefaultCondition.RuneWidth(r)
  418. }
  419. func ct(r rune) ctype {
  420. for _, iv := range ctypes {
  421. if iv.first <= r && r <= iv.last {
  422. return iv.ctype
  423. }
  424. }
  425. return neutral
  426. }
  427. // IsAmbiguousWidth returns whether is ambiguous width or not.
  428. func IsAmbiguousWidth(r rune) bool {
  429. return ct(r) == ambiguous
  430. }
  431. // IsNeutralWidth returns whether is neutral width or not.
  432. func IsNeutralWidth(r rune) bool {
  433. return ct(r) == neutral
  434. }
  435. // StringWidth return width as you can see
  436. func StringWidth(s string) (width int) {
  437. return DefaultCondition.StringWidth(s)
  438. }
  439. // Truncate return string truncated with w cells
  440. func Truncate(s string, w int, tail string) string {
  441. return DefaultCondition.Truncate(s, w, tail)
  442. }
  443. // Wrap return string wrapped with w cells
  444. func Wrap(s string, w int) string {
  445. return DefaultCondition.Wrap(s, w)
  446. }
  447. // FillLeft return string filled in left by spaces in w cells
  448. func FillLeft(s string, w int) string {
  449. return DefaultCondition.FillLeft(s, w)
  450. }
  451. // FillRight return string filled in left by spaces in w cells
  452. func FillRight(s string, w int) string {
  453. return DefaultCondition.FillRight(s, w)
  454. }