convert.go 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  1. package door
  2. import "strings"
  3. // I used iconv in door++, but it was horrible. It doesn't translate
  4. // control codes to Unicode.
  5. // This converts CP437 to Unicode for rendering in Unicode terminals
  6. // like the linux telnet client.
  7. // This does everything but: \x07, \x08, \x0a and \x0d.
  8. // 0x07, 0x08, 0x09, 0x0a, 0x0c, 0x0d, 0x1b (ANSI spec)
  9. func CP437_to_Unicode(cp437 string) string {
  10. // var result string
  11. var result strings.Builder
  12. // make initial guess on capacity
  13. result.Grow(len(cp437))
  14. for _, char := range []byte(cp437) {
  15. // for _, char := range []rune(cp437) {
  16. switch int(char) {
  17. case 0x01:
  18. result.WriteRune('\u263A')
  19. case 0x02:
  20. result.WriteRune('\u263B')
  21. case 0x03:
  22. result.WriteRune('\u2665')
  23. case 0x04:
  24. result.WriteRune('\u2666')
  25. case 0x05:
  26. result.WriteRune('\u2663')
  27. case 0x06:
  28. result.WriteRune('\u2660')
  29. case 0x09:
  30. result.WriteRune('\u25CB')
  31. case 0x0b:
  32. result.WriteRune('\u2642')
  33. case 0x0c:
  34. result.WriteRune('\u2640')
  35. case 0x0e:
  36. result.WriteRune('\u266B')
  37. case 0x0f:
  38. result.WriteRune('\u263C')
  39. case 0x10:
  40. result.WriteRune('\u25BA')
  41. case 0x11:
  42. result.WriteRune('\u25C4')
  43. case 0x12:
  44. result.WriteRune('\u2195')
  45. case 0x13:
  46. result.WriteRune('\u203C')
  47. case 0x14:
  48. result.Write([]byte{0xc2, 0xb6})
  49. case 0x15:
  50. result.Write([]byte{0xc2, 0xa7})
  51. case 0x16:
  52. result.WriteRune('\u25AC')
  53. case 0x17:
  54. result.WriteRune('\u21A8')
  55. case 0x18:
  56. result.WriteRune('\u2191')
  57. case 0x19:
  58. result.WriteRune('\u2193')
  59. case 0x1a:
  60. result.WriteRune('\u2192')
  61. case 0x1c:
  62. result.WriteRune('\u221F')
  63. case 0x1d:
  64. result.WriteRune('\u2194')
  65. case 0x1e:
  66. result.WriteRune('\u25B2')
  67. case 0x1f:
  68. result.WriteRune('\u25BC')
  69. case 0x7f:
  70. result.WriteRune('\u2302')
  71. case 0x80:
  72. result.Write([]byte{0xc3, 0x87})
  73. case 0x81:
  74. result.Write([]byte{0xc3, 0xbc})
  75. case 0x82:
  76. result.Write([]byte{0xc3, 0xa9})
  77. case 0x83:
  78. result.Write([]byte{0xc3, 0xa2})
  79. case 0x84:
  80. result.Write([]byte{0xc3, 0xa4})
  81. case 0x85:
  82. result.Write([]byte{0xc3, 0xa0})
  83. case 0x86:
  84. result.Write([]byte{0xc3, 0xa5})
  85. case 0x87:
  86. result.Write([]byte{0xc3, 0xa7})
  87. case 0x88:
  88. result.Write([]byte{0xc3, 0xaa})
  89. case 0x89:
  90. result.Write([]byte{0xc3, 0xab})
  91. case 0x8a:
  92. result.Write([]byte{0xc3, 0xa8})
  93. case 0x8b:
  94. result.Write([]byte{0xc3, 0xaf})
  95. case 0x8c:
  96. result.Write([]byte{0xc3, 0xae})
  97. case 0x8d:
  98. result.Write([]byte{0xc3, 0xac})
  99. case 0x8e:
  100. result.Write([]byte{0xc3, 0x84})
  101. case 0x8f:
  102. result.Write([]byte{0xc3, 0x85})
  103. case 0x90:
  104. result.Write([]byte{0xc3, 0x89})
  105. case 0x91:
  106. result.Write([]byte{0xc3, 0xa6})
  107. case 0x92:
  108. result.Write([]byte{0xc3, 0x86})
  109. case 0x93:
  110. result.Write([]byte{0xc3, 0xb4})
  111. case 0x94:
  112. result.Write([]byte{0xc3, 0xb6})
  113. case 0x95:
  114. result.Write([]byte{0xc3, 0xb2})
  115. case 0x96:
  116. result.Write([]byte{0xc3, 0xbb})
  117. case 0x97:
  118. result.Write([]byte{0xc3, 0xb9})
  119. case 0x98:
  120. result.Write([]byte{0xc3, 0xbf})
  121. case 0x99:
  122. result.Write([]byte{0xc3, 0x96})
  123. case 0x9a:
  124. result.Write([]byte{0xc3, 0x9c})
  125. case 0x9b:
  126. result.Write([]byte{0xc2, 0xa2})
  127. case 0x9c:
  128. result.Write([]byte{0xc2, 0xa3})
  129. case 0x9d:
  130. result.Write([]byte{0xc2, 0xa5})
  131. case 0x9e:
  132. result.WriteRune('\u20A7')
  133. case 0x9f:
  134. result.WriteRune('\u0192')
  135. case 0xa0:
  136. result.Write([]byte{0xc3, 0xa1})
  137. case 0xa1:
  138. result.Write([]byte{0xc3, 0xad})
  139. case 0xa2:
  140. result.Write([]byte{0xc3, 0xb3})
  141. case 0xa3:
  142. result.Write([]byte{0xc3, 0xba})
  143. case 0xa4:
  144. result.Write([]byte{0xc3, 0xb1})
  145. case 0xa5:
  146. result.Write([]byte{0xc3, 0x91})
  147. case 0xa6:
  148. result.Write([]byte{0xc2, 0xaa})
  149. case 0xa7:
  150. result.Write([]byte{0xc2, 0xba})
  151. case 0xa8:
  152. result.Write([]byte{0xc2, 0xbf})
  153. case 0xa9:
  154. result.WriteRune('\u2310')
  155. case 0xaa:
  156. result.Write([]byte{0xc2, 0xac})
  157. case 0xab:
  158. result.Write([]byte{0xc2, 0xbd})
  159. case 0xac:
  160. result.Write([]byte{0xc2, 0xbc})
  161. case 0xad:
  162. result.Write([]byte{0xc2, 0xa1})
  163. case 0xae:
  164. result.Write([]byte{0xc2, 0xab})
  165. case 0xaf:
  166. result.Write([]byte{0xc2, 0xbb})
  167. case 0xb0:
  168. result.WriteRune('\u2591')
  169. case 0xb1:
  170. result.WriteRune('\u2592')
  171. case 0xb2:
  172. result.WriteRune('\u2593')
  173. case 0xb3:
  174. result.WriteRune('\u2502')
  175. case 0xb4:
  176. result.WriteRune('\u2524')
  177. case 0xb5:
  178. result.WriteRune('\u2561')
  179. case 0xb6:
  180. result.WriteRune('\u2562')
  181. case 0xb7:
  182. result.WriteRune('\u2556')
  183. case 0xb8:
  184. result.WriteRune('\u2555')
  185. case 0xb9:
  186. result.WriteRune('\u2563')
  187. case 0xba:
  188. result.WriteRune('\u2551')
  189. case 0xbb:
  190. result.WriteRune('\u2557')
  191. case 0xbc:
  192. result.WriteRune('\u255D')
  193. case 0xbd:
  194. result.WriteRune('\u255C')
  195. case 0xbe:
  196. result.WriteRune('\u255B')
  197. case 0xbf:
  198. result.WriteRune('\u2510')
  199. case 0xc0:
  200. result.WriteRune('\u2514')
  201. case 0xc1:
  202. result.WriteRune('\u2534')
  203. case 0xc2:
  204. result.WriteRune('\u252C')
  205. case 0xc3:
  206. result.WriteRune('\u251C')
  207. case 0xc4:
  208. result.WriteRune('\u2500')
  209. case 0xc5:
  210. result.WriteRune('\u253C')
  211. case 0xc6:
  212. result.WriteRune('\u255E')
  213. case 0xc7:
  214. result.WriteRune('\u255F')
  215. case 0xc8:
  216. result.WriteRune('\u255A')
  217. case 0xc9:
  218. result.WriteRune('\u2554')
  219. case 0xca:
  220. result.WriteRune('\u2569')
  221. case 0xcb:
  222. result.WriteRune('\u2566')
  223. case 0xcc:
  224. result.WriteRune('\u2560')
  225. case 0xcd:
  226. result.WriteRune('\u2550')
  227. case 0xce:
  228. result.WriteRune('\u256C')
  229. case 0xcf:
  230. result.WriteRune('\u2567')
  231. case 0xd0:
  232. result.WriteRune('\u2568')
  233. case 0xd1:
  234. result.WriteRune('\u2564')
  235. case 0xd2:
  236. result.WriteRune('\u2565')
  237. case 0xd3:
  238. result.WriteRune('\u2559')
  239. case 0xd4:
  240. result.WriteRune('\u2558')
  241. case 0xd5:
  242. result.WriteRune('\u2552')
  243. case 0xd6:
  244. result.WriteRune('\u2553')
  245. case 0xd7:
  246. result.WriteRune('\u256B')
  247. case 0xd8:
  248. result.WriteRune('\u256A')
  249. case 0xd9:
  250. result.WriteRune('\u2518')
  251. case 0xda:
  252. result.WriteRune('\u250C')
  253. case 0xdb:
  254. result.WriteRune('\u2588')
  255. case 0xdc:
  256. result.WriteRune('\u2584')
  257. case 0xdd:
  258. result.WriteRune('\u258C')
  259. case 0xde:
  260. result.WriteRune('\u2590')
  261. case 0xdf:
  262. result.WriteRune('\u2580')
  263. case 0xe0:
  264. result.WriteRune('\u03B1')
  265. case 0xe1:
  266. result.Write([]byte{0xc3, 0x9f})
  267. case 0xe2:
  268. result.WriteRune('\u0393')
  269. case 0xe3:
  270. result.WriteRune('\u03C0')
  271. case 0xe4:
  272. result.WriteRune('\u03A3')
  273. case 0xe5:
  274. result.WriteRune('\u03C3')
  275. case 0xe6:
  276. result.Write([]byte{0xc2, 0xb5})
  277. case 0xe7:
  278. result.WriteRune('\u03C4')
  279. case 0xe8:
  280. result.WriteRune('\u03A6')
  281. case 0xe9:
  282. result.WriteRune('\u0398')
  283. case 0xea:
  284. result.WriteRune('\u03A9')
  285. case 0xeb:
  286. result.WriteRune('\u03B4')
  287. case 0xec:
  288. result.WriteRune('\u221E')
  289. case 0xed:
  290. result.WriteRune('\u03C6')
  291. case 0xee:
  292. result.WriteRune('\u03B5')
  293. case 0xef:
  294. result.WriteRune('\u2229')
  295. case 0xf0:
  296. result.WriteRune('\u2261')
  297. case 0xf1:
  298. result.Write([]byte{0xc2, 0xb1})
  299. case 0xf2:
  300. result.WriteRune('\u2265')
  301. case 0xf3:
  302. result.WriteRune('\u2264')
  303. case 0xf4:
  304. result.WriteRune('\u2320')
  305. case 0xf5:
  306. result.WriteRune('\u2321')
  307. case 0xf6:
  308. result.Write([]byte{0xc3, 0xb7})
  309. case 0xf7:
  310. result.WriteRune('\u2248')
  311. case 0xf8:
  312. result.Write([]byte{0xc2, 0xb0})
  313. case 0xf9:
  314. result.WriteRune('\u2219')
  315. case 0xfa:
  316. result.Write([]byte{0xc2, 0xb7})
  317. case 0xfb:
  318. result.WriteRune('\u221A')
  319. case 0xfc:
  320. result.WriteRune('\u207F')
  321. case 0xfd:
  322. result.Write([]byte{0xc2, 0xb2})
  323. case 0xfe:
  324. result.WriteRune('\u25A0')
  325. case 0xff:
  326. result.Write([]byte{0xc2, 0xa0})
  327. default:
  328. result.WriteByte(byte(char))
  329. }
  330. }
  331. return result.String()
  332. }