maketables.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556
  1. // Copyright 2013 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. // +build ignore
  5. package main
  6. import (
  7. "bufio"
  8. "fmt"
  9. "log"
  10. "net/http"
  11. "sort"
  12. "strings"
  13. "unicode/utf8"
  14. "golang.org/x/text/encoding"
  15. "golang.org/x/text/internal/gen"
  16. )
  17. const ascii = "\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f" +
  18. "\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f" +
  19. ` !"#$%&'()*+,-./0123456789:;<=>?` +
  20. `@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_` +
  21. "`abcdefghijklmnopqrstuvwxyz{|}~\u007f"
  22. var encodings = []struct {
  23. name string
  24. mib string
  25. comment string
  26. varName string
  27. replacement byte
  28. mapping string
  29. }{
  30. {
  31. "IBM Code Page 037",
  32. "IBM037",
  33. "",
  34. "CodePage037",
  35. 0x3f,
  36. "http://source.icu-project.org/repos/icu/data/trunk/charset/data/ucm/glibc-IBM037-2.1.2.ucm",
  37. },
  38. {
  39. "IBM Code Page 437",
  40. "PC8CodePage437",
  41. "",
  42. "CodePage437",
  43. encoding.ASCIISub,
  44. "http://source.icu-project.org/repos/icu/data/trunk/charset/data/ucm/glibc-IBM437-2.1.2.ucm",
  45. },
  46. {
  47. "IBM Code Page 850",
  48. "PC850Multilingual",
  49. "",
  50. "CodePage850",
  51. encoding.ASCIISub,
  52. "http://source.icu-project.org/repos/icu/data/trunk/charset/data/ucm/glibc-IBM850-2.1.2.ucm",
  53. },
  54. {
  55. "IBM Code Page 852",
  56. "PCp852",
  57. "",
  58. "CodePage852",
  59. encoding.ASCIISub,
  60. "http://source.icu-project.org/repos/icu/data/trunk/charset/data/ucm/glibc-IBM852-2.1.2.ucm",
  61. },
  62. {
  63. "IBM Code Page 855",
  64. "IBM855",
  65. "",
  66. "CodePage855",
  67. encoding.ASCIISub,
  68. "http://source.icu-project.org/repos/icu/data/trunk/charset/data/ucm/glibc-IBM855-2.1.2.ucm",
  69. },
  70. {
  71. "Windows Code Page 858", // PC latin1 with Euro
  72. "IBM00858",
  73. "",
  74. "CodePage858",
  75. encoding.ASCIISub,
  76. "http://source.icu-project.org/repos/icu/data/trunk/charset/data/ucm/windows-858-2000.ucm",
  77. },
  78. {
  79. "IBM Code Page 860",
  80. "IBM860",
  81. "",
  82. "CodePage860",
  83. encoding.ASCIISub,
  84. "http://source.icu-project.org/repos/icu/data/trunk/charset/data/ucm/glibc-IBM860-2.1.2.ucm",
  85. },
  86. {
  87. "IBM Code Page 862",
  88. "PC862LatinHebrew",
  89. "",
  90. "CodePage862",
  91. encoding.ASCIISub,
  92. "http://source.icu-project.org/repos/icu/data/trunk/charset/data/ucm/glibc-IBM862-2.1.2.ucm",
  93. },
  94. {
  95. "IBM Code Page 863",
  96. "IBM863",
  97. "",
  98. "CodePage863",
  99. encoding.ASCIISub,
  100. "http://source.icu-project.org/repos/icu/data/trunk/charset/data/ucm/glibc-IBM863-2.1.2.ucm",
  101. },
  102. {
  103. "IBM Code Page 865",
  104. "IBM865",
  105. "",
  106. "CodePage865",
  107. encoding.ASCIISub,
  108. "http://source.icu-project.org/repos/icu/data/trunk/charset/data/ucm/glibc-IBM865-2.1.2.ucm",
  109. },
  110. {
  111. "IBM Code Page 866",
  112. "IBM866",
  113. "",
  114. "CodePage866",
  115. encoding.ASCIISub,
  116. "http://encoding.spec.whatwg.org/index-ibm866.txt",
  117. },
  118. {
  119. "IBM Code Page 1047",
  120. "IBM1047",
  121. "",
  122. "CodePage1047",
  123. 0x3f,
  124. "http://source.icu-project.org/repos/icu/data/trunk/charset/data/ucm/glibc-IBM1047-2.1.2.ucm",
  125. },
  126. {
  127. "IBM Code Page 1140",
  128. "IBM01140",
  129. "",
  130. "CodePage1140",
  131. 0x3f,
  132. "http://source.icu-project.org/repos/icu/data/trunk/charset/data/ucm/ibm-1140_P100-1997.ucm",
  133. },
  134. {
  135. "ISO 8859-1",
  136. "ISOLatin1",
  137. "",
  138. "ISO8859_1",
  139. encoding.ASCIISub,
  140. "http://source.icu-project.org/repos/icu/data/trunk/charset/data/ucm/iso-8859_1-1998.ucm",
  141. },
  142. {
  143. "ISO 8859-2",
  144. "ISOLatin2",
  145. "",
  146. "ISO8859_2",
  147. encoding.ASCIISub,
  148. "http://encoding.spec.whatwg.org/index-iso-8859-2.txt",
  149. },
  150. {
  151. "ISO 8859-3",
  152. "ISOLatin3",
  153. "",
  154. "ISO8859_3",
  155. encoding.ASCIISub,
  156. "http://encoding.spec.whatwg.org/index-iso-8859-3.txt",
  157. },
  158. {
  159. "ISO 8859-4",
  160. "ISOLatin4",
  161. "",
  162. "ISO8859_4",
  163. encoding.ASCIISub,
  164. "http://encoding.spec.whatwg.org/index-iso-8859-4.txt",
  165. },
  166. {
  167. "ISO 8859-5",
  168. "ISOLatinCyrillic",
  169. "",
  170. "ISO8859_5",
  171. encoding.ASCIISub,
  172. "http://encoding.spec.whatwg.org/index-iso-8859-5.txt",
  173. },
  174. {
  175. "ISO 8859-6",
  176. "ISOLatinArabic",
  177. "",
  178. "ISO8859_6,ISO8859_6E,ISO8859_6I",
  179. encoding.ASCIISub,
  180. "http://encoding.spec.whatwg.org/index-iso-8859-6.txt",
  181. },
  182. {
  183. "ISO 8859-7",
  184. "ISOLatinGreek",
  185. "",
  186. "ISO8859_7",
  187. encoding.ASCIISub,
  188. "http://encoding.spec.whatwg.org/index-iso-8859-7.txt",
  189. },
  190. {
  191. "ISO 8859-8",
  192. "ISOLatinHebrew",
  193. "",
  194. "ISO8859_8,ISO8859_8E,ISO8859_8I",
  195. encoding.ASCIISub,
  196. "http://encoding.spec.whatwg.org/index-iso-8859-8.txt",
  197. },
  198. {
  199. "ISO 8859-9",
  200. "ISOLatin5",
  201. "",
  202. "ISO8859_9",
  203. encoding.ASCIISub,
  204. "http://source.icu-project.org/repos/icu/data/trunk/charset/data/ucm/iso-8859_9-1999.ucm",
  205. },
  206. {
  207. "ISO 8859-10",
  208. "ISOLatin6",
  209. "",
  210. "ISO8859_10",
  211. encoding.ASCIISub,
  212. "http://encoding.spec.whatwg.org/index-iso-8859-10.txt",
  213. },
  214. {
  215. "ISO 8859-13",
  216. "ISO885913",
  217. "",
  218. "ISO8859_13",
  219. encoding.ASCIISub,
  220. "http://encoding.spec.whatwg.org/index-iso-8859-13.txt",
  221. },
  222. {
  223. "ISO 8859-14",
  224. "ISO885914",
  225. "",
  226. "ISO8859_14",
  227. encoding.ASCIISub,
  228. "http://encoding.spec.whatwg.org/index-iso-8859-14.txt",
  229. },
  230. {
  231. "ISO 8859-15",
  232. "ISO885915",
  233. "",
  234. "ISO8859_15",
  235. encoding.ASCIISub,
  236. "http://encoding.spec.whatwg.org/index-iso-8859-15.txt",
  237. },
  238. {
  239. "ISO 8859-16",
  240. "ISO885916",
  241. "",
  242. "ISO8859_16",
  243. encoding.ASCIISub,
  244. "http://encoding.spec.whatwg.org/index-iso-8859-16.txt",
  245. },
  246. {
  247. "KOI8-R",
  248. "KOI8R",
  249. "",
  250. "KOI8R",
  251. encoding.ASCIISub,
  252. "http://encoding.spec.whatwg.org/index-koi8-r.txt",
  253. },
  254. {
  255. "KOI8-U",
  256. "KOI8U",
  257. "",
  258. "KOI8U",
  259. encoding.ASCIISub,
  260. "http://encoding.spec.whatwg.org/index-koi8-u.txt",
  261. },
  262. {
  263. "Macintosh",
  264. "Macintosh",
  265. "",
  266. "Macintosh",
  267. encoding.ASCIISub,
  268. "http://encoding.spec.whatwg.org/index-macintosh.txt",
  269. },
  270. {
  271. "Macintosh Cyrillic",
  272. "MacintoshCyrillic",
  273. "",
  274. "MacintoshCyrillic",
  275. encoding.ASCIISub,
  276. "http://encoding.spec.whatwg.org/index-x-mac-cyrillic.txt",
  277. },
  278. {
  279. "Windows 874",
  280. "Windows874",
  281. "",
  282. "Windows874",
  283. encoding.ASCIISub,
  284. "http://encoding.spec.whatwg.org/index-windows-874.txt",
  285. },
  286. {
  287. "Windows 1250",
  288. "Windows1250",
  289. "",
  290. "Windows1250",
  291. encoding.ASCIISub,
  292. "http://encoding.spec.whatwg.org/index-windows-1250.txt",
  293. },
  294. {
  295. "Windows 1251",
  296. "Windows1251",
  297. "",
  298. "Windows1251",
  299. encoding.ASCIISub,
  300. "http://encoding.spec.whatwg.org/index-windows-1251.txt",
  301. },
  302. {
  303. "Windows 1252",
  304. "Windows1252",
  305. "",
  306. "Windows1252",
  307. encoding.ASCIISub,
  308. "http://encoding.spec.whatwg.org/index-windows-1252.txt",
  309. },
  310. {
  311. "Windows 1253",
  312. "Windows1253",
  313. "",
  314. "Windows1253",
  315. encoding.ASCIISub,
  316. "http://encoding.spec.whatwg.org/index-windows-1253.txt",
  317. },
  318. {
  319. "Windows 1254",
  320. "Windows1254",
  321. "",
  322. "Windows1254",
  323. encoding.ASCIISub,
  324. "http://encoding.spec.whatwg.org/index-windows-1254.txt",
  325. },
  326. {
  327. "Windows 1255",
  328. "Windows1255",
  329. "",
  330. "Windows1255",
  331. encoding.ASCIISub,
  332. "http://encoding.spec.whatwg.org/index-windows-1255.txt",
  333. },
  334. {
  335. "Windows 1256",
  336. "Windows1256",
  337. "",
  338. "Windows1256",
  339. encoding.ASCIISub,
  340. "http://encoding.spec.whatwg.org/index-windows-1256.txt",
  341. },
  342. {
  343. "Windows 1257",
  344. "Windows1257",
  345. "",
  346. "Windows1257",
  347. encoding.ASCIISub,
  348. "http://encoding.spec.whatwg.org/index-windows-1257.txt",
  349. },
  350. {
  351. "Windows 1258",
  352. "Windows1258",
  353. "",
  354. "Windows1258",
  355. encoding.ASCIISub,
  356. "http://encoding.spec.whatwg.org/index-windows-1258.txt",
  357. },
  358. {
  359. "X-User-Defined",
  360. "XUserDefined",
  361. "It is defined at http://encoding.spec.whatwg.org/#x-user-defined",
  362. "XUserDefined",
  363. encoding.ASCIISub,
  364. ascii +
  365. "\uf780\uf781\uf782\uf783\uf784\uf785\uf786\uf787" +
  366. "\uf788\uf789\uf78a\uf78b\uf78c\uf78d\uf78e\uf78f" +
  367. "\uf790\uf791\uf792\uf793\uf794\uf795\uf796\uf797" +
  368. "\uf798\uf799\uf79a\uf79b\uf79c\uf79d\uf79e\uf79f" +
  369. "\uf7a0\uf7a1\uf7a2\uf7a3\uf7a4\uf7a5\uf7a6\uf7a7" +
  370. "\uf7a8\uf7a9\uf7aa\uf7ab\uf7ac\uf7ad\uf7ae\uf7af" +
  371. "\uf7b0\uf7b1\uf7b2\uf7b3\uf7b4\uf7b5\uf7b6\uf7b7" +
  372. "\uf7b8\uf7b9\uf7ba\uf7bb\uf7bc\uf7bd\uf7be\uf7bf" +
  373. "\uf7c0\uf7c1\uf7c2\uf7c3\uf7c4\uf7c5\uf7c6\uf7c7" +
  374. "\uf7c8\uf7c9\uf7ca\uf7cb\uf7cc\uf7cd\uf7ce\uf7cf" +
  375. "\uf7d0\uf7d1\uf7d2\uf7d3\uf7d4\uf7d5\uf7d6\uf7d7" +
  376. "\uf7d8\uf7d9\uf7da\uf7db\uf7dc\uf7dd\uf7de\uf7df" +
  377. "\uf7e0\uf7e1\uf7e2\uf7e3\uf7e4\uf7e5\uf7e6\uf7e7" +
  378. "\uf7e8\uf7e9\uf7ea\uf7eb\uf7ec\uf7ed\uf7ee\uf7ef" +
  379. "\uf7f0\uf7f1\uf7f2\uf7f3\uf7f4\uf7f5\uf7f6\uf7f7" +
  380. "\uf7f8\uf7f9\uf7fa\uf7fb\uf7fc\uf7fd\uf7fe\uf7ff",
  381. },
  382. }
  383. func getWHATWG(url string) string {
  384. res, err := http.Get(url)
  385. if err != nil {
  386. log.Fatalf("%q: Get: %v", url, err)
  387. }
  388. defer res.Body.Close()
  389. mapping := make([]rune, 128)
  390. for i := range mapping {
  391. mapping[i] = '\ufffd'
  392. }
  393. scanner := bufio.NewScanner(res.Body)
  394. for scanner.Scan() {
  395. s := strings.TrimSpace(scanner.Text())
  396. if s == "" || s[0] == '#' {
  397. continue
  398. }
  399. x, y := 0, 0
  400. if _, err := fmt.Sscanf(s, "%d\t0x%x", &x, &y); err != nil {
  401. log.Fatalf("could not parse %q", s)
  402. }
  403. if x < 0 || 128 <= x {
  404. log.Fatalf("code %d is out of range", x)
  405. }
  406. if 0x80 <= y && y < 0xa0 {
  407. // We diverge from the WHATWG spec by mapping control characters
  408. // in the range [0x80, 0xa0) to U+FFFD.
  409. continue
  410. }
  411. mapping[x] = rune(y)
  412. }
  413. return ascii + string(mapping)
  414. }
  415. func getUCM(url string) string {
  416. res, err := http.Get(url)
  417. if err != nil {
  418. log.Fatalf("%q: Get: %v", url, err)
  419. }
  420. defer res.Body.Close()
  421. mapping := make([]rune, 256)
  422. for i := range mapping {
  423. mapping[i] = '\ufffd'
  424. }
  425. charsFound := 0
  426. scanner := bufio.NewScanner(res.Body)
  427. for scanner.Scan() {
  428. s := strings.TrimSpace(scanner.Text())
  429. if s == "" || s[0] == '#' {
  430. continue
  431. }
  432. var c byte
  433. var r rune
  434. if _, err := fmt.Sscanf(s, `<U%x> \x%x |0`, &r, &c); err != nil {
  435. continue
  436. }
  437. mapping[c] = r
  438. charsFound++
  439. }
  440. if charsFound < 200 {
  441. log.Fatalf("%q: only %d characters found (wrong page format?)", url, charsFound)
  442. }
  443. return string(mapping)
  444. }
  445. func main() {
  446. mibs := map[string]bool{}
  447. all := []string{}
  448. w := gen.NewCodeWriter()
  449. defer w.WriteGoFile("tables.go", "charmap")
  450. printf := func(s string, a ...interface{}) { fmt.Fprintf(w, s, a...) }
  451. printf("import (\n")
  452. printf("\t\"golang.org/x/text/encoding\"\n")
  453. printf("\t\"golang.org/x/text/encoding/internal/identifier\"\n")
  454. printf(")\n\n")
  455. for _, e := range encodings {
  456. varNames := strings.Split(e.varName, ",")
  457. all = append(all, varNames...)
  458. varName := varNames[0]
  459. switch {
  460. case strings.HasPrefix(e.mapping, "http://encoding.spec.whatwg.org/"):
  461. e.mapping = getWHATWG(e.mapping)
  462. case strings.HasPrefix(e.mapping, "http://source.icu-project.org/repos/icu/data/trunk/charset/data/ucm/"):
  463. e.mapping = getUCM(e.mapping)
  464. }
  465. asciiSuperset, low := strings.HasPrefix(e.mapping, ascii), 0x00
  466. if asciiSuperset {
  467. low = 0x80
  468. }
  469. lvn := 1
  470. if strings.HasPrefix(varName, "ISO") || strings.HasPrefix(varName, "KOI") {
  471. lvn = 3
  472. }
  473. lowerVarName := strings.ToLower(varName[:lvn]) + varName[lvn:]
  474. printf("// %s is the %s encoding.\n", varName, e.name)
  475. if e.comment != "" {
  476. printf("//\n// %s\n", e.comment)
  477. }
  478. printf("var %s *Charmap = &%s\n\nvar %s = Charmap{\nname: %q,\n",
  479. varName, lowerVarName, lowerVarName, e.name)
  480. if mibs[e.mib] {
  481. log.Fatalf("MIB type %q declared multiple times.", e.mib)
  482. }
  483. printf("mib: identifier.%s,\n", e.mib)
  484. printf("asciiSuperset: %t,\n", asciiSuperset)
  485. printf("low: 0x%02x,\n", low)
  486. printf("replacement: 0x%02x,\n", e.replacement)
  487. printf("decode: [256]utf8Enc{\n")
  488. i, backMapping := 0, map[rune]byte{}
  489. for _, c := range e.mapping {
  490. if _, ok := backMapping[c]; !ok && c != utf8.RuneError {
  491. backMapping[c] = byte(i)
  492. }
  493. var buf [8]byte
  494. n := utf8.EncodeRune(buf[:], c)
  495. if n > 3 {
  496. panic(fmt.Sprintf("rune %q (%U) is too long", c, c))
  497. }
  498. printf("{%d,[3]byte{0x%02x,0x%02x,0x%02x}},", n, buf[0], buf[1], buf[2])
  499. if i%2 == 1 {
  500. printf("\n")
  501. }
  502. i++
  503. }
  504. printf("},\n")
  505. printf("encode: [256]uint32{\n")
  506. encode := make([]uint32, 0, 256)
  507. for c, i := range backMapping {
  508. encode = append(encode, uint32(i)<<24|uint32(c))
  509. }
  510. sort.Sort(byRune(encode))
  511. for len(encode) < cap(encode) {
  512. encode = append(encode, encode[len(encode)-1])
  513. }
  514. for i, enc := range encode {
  515. printf("0x%08x,", enc)
  516. if i%8 == 7 {
  517. printf("\n")
  518. }
  519. }
  520. printf("},\n}\n")
  521. // Add an estimate of the size of a single Charmap{} struct value, which
  522. // includes two 256 elem arrays of 4 bytes and some extra fields, which
  523. // align to 3 uint64s on 64-bit architectures.
  524. w.Size += 2*4*256 + 3*8
  525. }
  526. // TODO: add proper line breaking.
  527. printf("var listAll = []encoding.Encoding{\n%s,\n}\n\n", strings.Join(all, ",\n"))
  528. }
  529. type byRune []uint32
  530. func (b byRune) Len() int { return len(b) }
  531. func (b byRune) Less(i, j int) bool { return b[i]&0xffffff < b[j]&0xffffff }
  532. func (b byRune) Swap(i, j int) { b[i], b[j] = b[j], b[i] }