gen.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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. // Download https://encoding.spec.whatwg.org/encodings.json and use it to
  7. // generate table.go.
  8. import (
  9. "encoding/json"
  10. "fmt"
  11. "log"
  12. "net/http"
  13. "strings"
  14. )
  15. type enc struct {
  16. Name string
  17. Labels []string
  18. }
  19. type group struct {
  20. Encodings []enc
  21. Heading string
  22. }
  23. const specURL = "https://encoding.spec.whatwg.org/encodings.json"
  24. func main() {
  25. resp, err := http.Get(specURL)
  26. if err != nil {
  27. log.Fatalf("error fetching %s: %s", specURL, err)
  28. }
  29. if resp.StatusCode != 200 {
  30. log.Fatalf("error fetching %s: HTTP status %s", specURL, resp.Status)
  31. }
  32. defer resp.Body.Close()
  33. var groups []group
  34. d := json.NewDecoder(resp.Body)
  35. err = d.Decode(&groups)
  36. if err != nil {
  37. log.Fatalf("error reading encodings.json: %s", err)
  38. }
  39. fmt.Println("// generated by go run gen.go; DO NOT EDIT")
  40. fmt.Println()
  41. fmt.Println("package charset")
  42. fmt.Println()
  43. fmt.Println("import (")
  44. fmt.Println(`"golang.org/x/text/encoding"`)
  45. for _, pkg := range []string{"charmap", "japanese", "korean", "simplifiedchinese", "traditionalchinese", "unicode"} {
  46. fmt.Printf("\"golang.org/x/text/encoding/%s\"\n", pkg)
  47. }
  48. fmt.Println(")")
  49. fmt.Println()
  50. fmt.Println("var encodings = map[string]struct{e encoding.Encoding; name string} {")
  51. for _, g := range groups {
  52. for _, e := range g.Encodings {
  53. goName, ok := miscNames[e.Name]
  54. if !ok {
  55. for k, v := range prefixes {
  56. if strings.HasPrefix(e.Name, k) {
  57. goName = v + e.Name[len(k):]
  58. break
  59. }
  60. }
  61. if goName == "" {
  62. log.Fatalf("unrecognized encoding name: %s", e.Name)
  63. }
  64. }
  65. for _, label := range e.Labels {
  66. fmt.Printf("%q: {%s, %q},\n", label, goName, e.Name)
  67. }
  68. }
  69. }
  70. fmt.Println("}")
  71. }
  72. var prefixes = map[string]string{
  73. "iso-8859-": "charmap.ISO8859_",
  74. "windows-": "charmap.Windows",
  75. }
  76. var miscNames = map[string]string{
  77. "utf-8": "encoding.Nop",
  78. "ibm866": "charmap.CodePage866",
  79. "iso-8859-8-i": "charmap.ISO8859_8",
  80. "koi8-r": "charmap.KOI8R",
  81. "koi8-u": "charmap.KOI8U",
  82. "macintosh": "charmap.Macintosh",
  83. "x-mac-cyrillic": "charmap.MacintoshCyrillic",
  84. "gbk": "simplifiedchinese.GBK",
  85. "gb18030": "simplifiedchinese.GB18030",
  86. "hz-gb-2312": "simplifiedchinese.HZGB2312",
  87. "big5": "traditionalchinese.Big5",
  88. "euc-jp": "japanese.EUCJP",
  89. "iso-2022-jp": "japanese.ISO2022JP",
  90. "shift_jis": "japanese.ShiftJIS",
  91. "euc-kr": "korean.EUCKR",
  92. "replacement": "encoding.Replacement",
  93. "utf-16be": "unicode.UTF16(unicode.BigEndian, unicode.IgnoreBOM)",
  94. "utf-16le": "unicode.UTF16(unicode.LittleEndian, unicode.IgnoreBOM)",
  95. "x-user-defined": "charmap.XUserDefined",
  96. }