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.
18 "golang.org/x/text/encoding"
19 "golang.org/x/text/internal/gen"
22 const ascii = "\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f" +
23 "\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f" +
24 ` !"#$%&'()*+,-./0123456789:;<=>?` +
25 `@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_` +
26 "`abcdefghijklmnopqrstuvwxyz{|}~\u007f"
28 var encodings = []struct {
42 "http://source.icu-project.org/repos/icu/data/trunk/charset/data/ucm/glibc-IBM037-2.1.2.ucm",
50 "http://source.icu-project.org/repos/icu/data/trunk/charset/data/ucm/glibc-IBM437-2.1.2.ucm",
58 "http://source.icu-project.org/repos/icu/data/trunk/charset/data/ucm/glibc-IBM850-2.1.2.ucm",
66 "http://source.icu-project.org/repos/icu/data/trunk/charset/data/ucm/glibc-IBM852-2.1.2.ucm",
74 "http://source.icu-project.org/repos/icu/data/trunk/charset/data/ucm/glibc-IBM855-2.1.2.ucm",
77 "Windows Code Page 858", // PC latin1 with Euro
82 "http://source.icu-project.org/repos/icu/data/trunk/charset/data/ucm/windows-858-2000.ucm",
90 "http://source.icu-project.org/repos/icu/data/trunk/charset/data/ucm/glibc-IBM860-2.1.2.ucm",
98 "http://source.icu-project.org/repos/icu/data/trunk/charset/data/ucm/glibc-IBM862-2.1.2.ucm",
106 "http://source.icu-project.org/repos/icu/data/trunk/charset/data/ucm/glibc-IBM863-2.1.2.ucm",
114 "http://source.icu-project.org/repos/icu/data/trunk/charset/data/ucm/glibc-IBM865-2.1.2.ucm",
122 "http://encoding.spec.whatwg.org/index-ibm866.txt",
125 "IBM Code Page 1047",
130 "http://source.icu-project.org/repos/icu/data/trunk/charset/data/ucm/glibc-IBM1047-2.1.2.ucm",
133 "IBM Code Page 1140",
138 "http://source.icu-project.org/repos/icu/data/trunk/charset/data/ucm/ibm-1140_P100-1997.ucm",
146 "http://source.icu-project.org/repos/icu/data/trunk/charset/data/ucm/iso-8859_1-1998.ucm",
154 "http://encoding.spec.whatwg.org/index-iso-8859-2.txt",
162 "http://encoding.spec.whatwg.org/index-iso-8859-3.txt",
170 "http://encoding.spec.whatwg.org/index-iso-8859-4.txt",
178 "http://encoding.spec.whatwg.org/index-iso-8859-5.txt",
184 "ISO8859_6,ISO8859_6E,ISO8859_6I",
186 "http://encoding.spec.whatwg.org/index-iso-8859-6.txt",
194 "http://encoding.spec.whatwg.org/index-iso-8859-7.txt",
200 "ISO8859_8,ISO8859_8E,ISO8859_8I",
202 "http://encoding.spec.whatwg.org/index-iso-8859-8.txt",
210 "http://source.icu-project.org/repos/icu/data/trunk/charset/data/ucm/iso-8859_9-1999.ucm",
218 "http://encoding.spec.whatwg.org/index-iso-8859-10.txt",
226 "http://encoding.spec.whatwg.org/index-iso-8859-13.txt",
234 "http://encoding.spec.whatwg.org/index-iso-8859-14.txt",
242 "http://encoding.spec.whatwg.org/index-iso-8859-15.txt",
250 "http://encoding.spec.whatwg.org/index-iso-8859-16.txt",
258 "http://encoding.spec.whatwg.org/index-koi8-r.txt",
266 "http://encoding.spec.whatwg.org/index-koi8-u.txt",
274 "http://encoding.spec.whatwg.org/index-macintosh.txt",
277 "Macintosh Cyrillic",
282 "http://encoding.spec.whatwg.org/index-x-mac-cyrillic.txt",
290 "http://encoding.spec.whatwg.org/index-windows-874.txt",
298 "http://encoding.spec.whatwg.org/index-windows-1250.txt",
306 "http://encoding.spec.whatwg.org/index-windows-1251.txt",
314 "http://encoding.spec.whatwg.org/index-windows-1252.txt",
322 "http://encoding.spec.whatwg.org/index-windows-1253.txt",
330 "http://encoding.spec.whatwg.org/index-windows-1254.txt",
338 "http://encoding.spec.whatwg.org/index-windows-1255.txt",
346 "http://encoding.spec.whatwg.org/index-windows-1256.txt",
354 "http://encoding.spec.whatwg.org/index-windows-1257.txt",
362 "http://encoding.spec.whatwg.org/index-windows-1258.txt",
367 "It is defined at http://encoding.spec.whatwg.org/#x-user-defined",
371 "\uf780\uf781\uf782\uf783\uf784\uf785\uf786\uf787" +
372 "\uf788\uf789\uf78a\uf78b\uf78c\uf78d\uf78e\uf78f" +
373 "\uf790\uf791\uf792\uf793\uf794\uf795\uf796\uf797" +
374 "\uf798\uf799\uf79a\uf79b\uf79c\uf79d\uf79e\uf79f" +
375 "\uf7a0\uf7a1\uf7a2\uf7a3\uf7a4\uf7a5\uf7a6\uf7a7" +
376 "\uf7a8\uf7a9\uf7aa\uf7ab\uf7ac\uf7ad\uf7ae\uf7af" +
377 "\uf7b0\uf7b1\uf7b2\uf7b3\uf7b4\uf7b5\uf7b6\uf7b7" +
378 "\uf7b8\uf7b9\uf7ba\uf7bb\uf7bc\uf7bd\uf7be\uf7bf" +
379 "\uf7c0\uf7c1\uf7c2\uf7c3\uf7c4\uf7c5\uf7c6\uf7c7" +
380 "\uf7c8\uf7c9\uf7ca\uf7cb\uf7cc\uf7cd\uf7ce\uf7cf" +
381 "\uf7d0\uf7d1\uf7d2\uf7d3\uf7d4\uf7d5\uf7d6\uf7d7" +
382 "\uf7d8\uf7d9\uf7da\uf7db\uf7dc\uf7dd\uf7de\uf7df" +
383 "\uf7e0\uf7e1\uf7e2\uf7e3\uf7e4\uf7e5\uf7e6\uf7e7" +
384 "\uf7e8\uf7e9\uf7ea\uf7eb\uf7ec\uf7ed\uf7ee\uf7ef" +
385 "\uf7f0\uf7f1\uf7f2\uf7f3\uf7f4\uf7f5\uf7f6\uf7f7" +
386 "\uf7f8\uf7f9\uf7fa\uf7fb\uf7fc\uf7fd\uf7fe\uf7ff",
390 func getWHATWG(url string) string {
391 res, err := http.Get(url)
393 log.Fatalf("%q: Get: %v", url, err)
395 defer res.Body.Close()
397 mapping := make([]rune, 128)
398 for i := range mapping {
399 mapping[i] = '\ufffd'
402 scanner := bufio.NewScanner(res.Body)
404 s := strings.TrimSpace(scanner.Text())
405 if s == "" || s[0] == '#' {
409 if _, err := fmt.Sscanf(s, "%d\t0x%x", &x, &y); err != nil {
410 log.Fatalf("could not parse %q", s)
412 if x < 0 || 128 <= x {
413 log.Fatalf("code %d is out of range", x)
415 if 0x80 <= y && y < 0xa0 {
416 // We diverge from the WHATWG spec by mapping control characters
417 // in the range [0x80, 0xa0) to U+FFFD.
422 return ascii + string(mapping)
425 func getUCM(url string) string {
426 res, err := http.Get(url)
428 log.Fatalf("%q: Get: %v", url, err)
430 defer res.Body.Close()
432 mapping := make([]rune, 256)
433 for i := range mapping {
434 mapping[i] = '\ufffd'
438 scanner := bufio.NewScanner(res.Body)
440 s := strings.TrimSpace(scanner.Text())
441 if s == "" || s[0] == '#' {
446 if _, err := fmt.Sscanf(s, `<U%x> \x%x |0`, &r, &c); err != nil {
453 if charsFound < 200 {
454 log.Fatalf("%q: only %d characters found (wrong page format?)", url, charsFound)
457 return string(mapping)
461 mibs := map[string]bool{}
464 w := gen.NewCodeWriter()
465 defer w.WriteGoFile("tables.go", "charmap")
467 printf := func(s string, a ...interface{}) { fmt.Fprintf(w, s, a...) }
470 printf("\t\"golang.org/x/text/encoding\"\n")
471 printf("\t\"golang.org/x/text/encoding/internal/identifier\"\n")
473 for _, e := range encodings {
474 varNames := strings.Split(e.varName, ",")
475 all = append(all, varNames...)
476 varName := varNames[0]
478 case strings.HasPrefix(e.mapping, "http://encoding.spec.whatwg.org/"):
479 e.mapping = getWHATWG(e.mapping)
480 case strings.HasPrefix(e.mapping, "http://source.icu-project.org/repos/icu/data/trunk/charset/data/ucm/"):
481 e.mapping = getUCM(e.mapping)
484 asciiSuperset, low := strings.HasPrefix(e.mapping, ascii), 0x00
489 if strings.HasPrefix(varName, "ISO") || strings.HasPrefix(varName, "KOI") {
492 lowerVarName := strings.ToLower(varName[:lvn]) + varName[lvn:]
493 printf("// %s is the %s encoding.\n", varName, e.name)
495 printf("//\n// %s\n", e.comment)
497 printf("var %s *Charmap = &%s\n\nvar %s = Charmap{\nname: %q,\n",
498 varName, lowerVarName, lowerVarName, e.name)
500 log.Fatalf("MIB type %q declared multiple times.", e.mib)
502 printf("mib: identifier.%s,\n", e.mib)
503 printf("asciiSuperset: %t,\n", asciiSuperset)
504 printf("low: 0x%02x,\n", low)
505 printf("replacement: 0x%02x,\n", e.replacement)
507 printf("decode: [256]utf8Enc{\n")
508 i, backMapping := 0, map[rune]byte{}
509 for _, c := range e.mapping {
510 if _, ok := backMapping[c]; !ok && c != utf8.RuneError {
511 backMapping[c] = byte(i)
514 n := utf8.EncodeRune(buf[:], c)
516 panic(fmt.Sprintf("rune %q (%U) is too long", c, c))
518 printf("{%d,[3]byte{0x%02x,0x%02x,0x%02x}},", n, buf[0], buf[1], buf[2])
526 printf("encode: [256]uint32{\n")
527 encode := make([]uint32, 0, 256)
528 for c, i := range backMapping {
529 encode = append(encode, uint32(i)<<24|uint32(c))
531 sort.Sort(byRune(encode))
532 for len(encode) < cap(encode) {
533 encode = append(encode, encode[len(encode)-1])
535 for i, enc := range encode {
536 printf("0x%08x,", enc)
543 // Add an estimate of the size of a single Charmap{} struct value, which
544 // includes two 256 elem arrays of 4 bytes and some extra fields, which
545 // align to 3 uint64s on 64-bit architectures.
546 w.Size += 2*4*256 + 3*8
548 // TODO: add proper line breaking.
549 printf("var listAll = []encoding.Encoding{\n%s,\n}\n\n", strings.Join(all, ",\n"))
554 func (b byRune) Len() int { return len(b) }
555 func (b byRune) Less(i, j int) bool { return b[i]&0xffffff < b[j]&0xffffff }
556 func (b byRune) Swap(i, j int) { b[i], b[j] = b[j], b[i] }