1 // Copyright 2017 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/internal/identifier"
19 "golang.org/x/text/internal/gen"
22 type registry struct {
23 XMLName xml.Name `xml:"registry"`
24 Updated string `xml:"updated"`
26 ID string `xml:"id,attr"`
28 Name string `xml:"name"`
30 Type string `xml:"type,attr"`
31 Data string `xml:"data,attr"`
34 Data string `xml:",innerxml"`
35 } `xml:"description,"`
36 MIB string `xml:"value"`
37 Alias []string `xml:"alias"`
38 MIME string `xml:"preferred_alias"`
44 r := gen.OpenIANAFile("assignments/character-sets/character-sets.xml")
46 if err := xml.NewDecoder(r).Decode(®); err != nil && err != io.EOF {
47 log.Fatalf("Error decoding charset registry: %v", err)
49 if len(reg.Registry) == 0 || reg.Registry[0].ID != "character-sets-1" {
50 log.Fatalf("Unexpected ID %s", reg.Registry[0].ID)
55 for _, rec := range reg.Registry[0].Record {
56 mib := identifier.MIB(parseInt(rec.MIB))
57 x.addEntry(mib, rec.Name)
58 for _, a := range rec.Alias {
59 a = strings.Split(a, " ")[0] // strip comments.
61 // MIB name aliases are prefixed with a "cs" (character set) in the
62 // registry to identify them as display names and to ensure that
63 // the name starts with a lowercase letter in case it is used as
64 // an identifier. We remove it to be left with a nice clean name.
65 if strings.HasPrefix(a, "cs") {
70 x.addAlias(rec.MIME, mib)
71 x.setName(1, rec.MIME)
75 w := gen.NewCodeWriter()
77 fmt.Fprintln(w, `import "golang.org/x/text/encoding/internal/identifier"`)
81 w.WriteGoFile("tables.go", "ianaindex")
89 type indexInfo struct {
90 // compacted index from code to MIB
91 codeToMIB []identifier.MIB
96 func (ii *indexInfo) Len() int {
97 return len(ii.codeToMIB)
100 func (ii *indexInfo) Less(a, b int) bool {
101 return ii.codeToMIB[a] < ii.codeToMIB[b]
104 func (ii *indexInfo) Swap(a, b int) {
105 ii.codeToMIB[a], ii.codeToMIB[b] = ii.codeToMIB[b], ii.codeToMIB[a]
106 // Co-sort the names.
107 ii.names[a], ii.names[b] = ii.names[b], ii.names[a]
110 func (ii *indexInfo) setName(i int, name string) {
111 ii.names[len(ii.names)-1][i] = name
114 func (ii *indexInfo) addEntry(mib identifier.MIB, name string) {
115 ii.names = append(ii.names, [3]string{name, name, name})
116 ii.addAlias(name, mib)
117 ii.codeToMIB = append(ii.codeToMIB, mib)
120 func (ii *indexInfo) addAlias(name string, mib identifier.MIB) {
121 // Don't add duplicates for the same mib. Adding duplicate aliases for
122 // different MIBs will cause the compiler to barf on an invalid map: great!.
123 for i := len(ii.alias) - 1; i >= 0 && ii.alias[i].mib == mib; i-- {
124 if ii.alias[i].name == name {
128 ii.alias = append(ii.alias, alias{name, mib})
129 lower := strings.ToLower(name)
131 ii.addAlias(lower, mib)
135 const maxMIMENameLen = '0' - 1 // officially 40, but we leave some buffer.
137 func writeIndex(w *gen.CodeWriter, x *indexInfo) {
141 fmt.Fprintln(w, "const (")
142 for i, m := range x.codeToMIB {
144 fmt.Fprintf(w, "enc%d = iota\n", m)
146 fmt.Fprintf(w, "enc%d\n", m)
149 fmt.Fprintln(w, "numIANA")
152 w.WriteVar("ianaToMIB", x.codeToMIB)
154 var ianaNames, mibNames []string
155 for _, names := range x.names {
157 if names[0] != names[1] {
158 // MIME names are mostly identical to IANA names. We share the
159 // tables by setting the first byte of the string to an index into
160 // the string itself (< maxMIMENameLen) to the IANA name. The MIME
161 // name immediately follows the index.
162 x := len(names[1]) + 1
163 if x > maxMIMENameLen {
164 log.Fatalf("MIME name length (%d) > %d", x, maxMIMENameLen)
166 n = string(x) + names[1] + names[0]
168 ianaNames = append(ianaNames, n)
169 mibNames = append(mibNames, names[2])
172 w.WriteVar("ianaNames", ianaNames)
173 w.WriteVar("mibNames", mibNames)
176 TODO: Instead of using a map, we could use binary search strings doing
177 on-the fly lower-casing per character. This allows to always avoid
178 allocation and will be considerably more compact.`)
179 fmt.Fprintln(w, "var ianaAliases = map[string]int{")
180 for _, a := range x.alias {
181 fmt.Fprintf(w, "%q: enc%d,\n", a.name, a.mib)
186 func parseInt(s string) int {
187 x, err := strconv.ParseInt(s, 10, 64)
189 log.Fatalf("Could not parse integer: %v", err)