1 // Copyright 2015 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.
5 //go:generate go run gen.go
7 // Package ianaindex maps names to Encodings as specified by the IANA registry.
8 // This includes both the MIME and IANA names.
10 // See http://www.iana.org/assignments/character-sets/character-sets.xhtml for
19 "golang.org/x/text/encoding"
20 "golang.org/x/text/encoding/charmap"
21 "golang.org/x/text/encoding/internal/identifier"
22 "golang.org/x/text/encoding/japanese"
23 "golang.org/x/text/encoding/korean"
24 "golang.org/x/text/encoding/simplifiedchinese"
25 "golang.org/x/text/encoding/traditionalchinese"
26 "golang.org/x/text/encoding/unicode"
29 // TODO: remove the "Status... incomplete" in the package doc comment.
30 // TODO: allow users to specify their own aliases?
31 // TODO: allow users to specify their own indexes?
32 // TODO: allow canonicalizing names
34 // NOTE: only use these top-level variables if we can get the linker to drop
35 // the indexes when they are not used. Make them a function or perhaps only
36 // support MIME otherwise.
39 // MIME is an index to map MIME names.
42 // IANA is an index that supports all names and aliases using IANA names as
43 // the canonical identifier.
46 // MIB is an index that associates the MIB display name with an Encoding.
49 mime = &Index{mimeName, ianaToMIB, ianaAliases, encodings[:]}
50 iana = &Index{ianaName, ianaToMIB, ianaAliases, encodings[:]}
51 mib = &Index{mibName, ianaToMIB, ianaAliases, encodings[:]}
54 // Index maps names registered by IANA to Encodings.
55 // Currently different Indexes only differ in the names they return for
56 // encodings. In the future they may also differ in supported aliases.
58 names func(i int) string
59 toMIB []identifier.MIB // Sorted slice of supported MIBs
61 enc []encoding.Encoding
65 errInvalidName = errors.New("ianaindex: invalid encoding name")
66 errUnknown = errors.New("ianaindex: unknown Encoding")
67 errUnsupported = errors.New("ianaindex: unsupported Encoding")
70 // Encoding returns an Encoding for IANA-registered names. Matching is
72 func (x *Index) Encoding(name string) (encoding.Encoding, error) {
73 name = strings.TrimSpace(name)
74 // First try without lowercasing (possibly creating an allocation).
75 i, ok := x.alias[name]
77 i, ok = x.alias[strings.ToLower(name)]
79 return nil, errInvalidName
85 // Name reports the canonical name of the given Encoding. It will return an
86 // error if the e is not associated with a known encoding scheme.
87 func (x *Index) Name(e encoding.Encoding) (string, error) {
88 id, ok := e.(identifier.Interface)
96 v := findMIB(x.toMIB, mib)
98 return "", errUnsupported
100 return x.names(v), nil
103 // TODO: the coverage of this index is rather spotty. Allowing users to set
104 // encodings would allow:
105 // - users to increase coverage
106 // - allow a partially loaded set of encodings in case the user doesn't need to
108 // - write an OS-specific wrapper for supported encodings and set them.
109 // The exact definition of Set depends a bit on if and how we want to let users
110 // write their own Encoding implementations. Also, it is not possible yet to
111 // only partially load the encodings without doing some refactoring. Until this
112 // is solved, we might as well not support Set.
113 // // Set sets the e to be used for the encoding scheme identified by name. Only
114 // // canonical names may be used. An empty name assigns e to its internally
115 // // associated encoding scheme.
116 // func (x *Index) Set(name string, e encoding.Encoding) error {
117 // panic("TODO: implement")
120 func findMIB(x []identifier.MIB, mib identifier.MIB) int {
121 i := sort.Search(len(x), func(i int) bool { return x[i] >= mib })
122 if i < len(x) && x[i] == mib {
128 const maxMIMENameLen = '0' - 1 // officially 40, but we leave some buffer.
130 func mimeName(x int) string {
132 // See gen.go for a description of the encoding.
133 if n[0] <= maxMIMENameLen {
139 func ianaName(x int) string {
141 // See gen.go for a description of the encoding.
142 if n[0] <= maxMIMENameLen {
148 func mibName(x int) string {
152 var encodings = [numIANA]encoding.Encoding{
153 enc106: unicode.UTF8,
154 enc1015: unicode.UTF16(unicode.BigEndian, unicode.UseBOM),
155 enc1013: unicode.UTF16(unicode.BigEndian, unicode.IgnoreBOM),
156 enc1014: unicode.UTF16(unicode.LittleEndian, unicode.IgnoreBOM),
157 enc2028: charmap.CodePage037,
158 enc2011: charmap.CodePage437,
159 enc2009: charmap.CodePage850,
160 enc2010: charmap.CodePage852,
161 enc2046: charmap.CodePage855,
162 enc2089: charmap.CodePage858,
163 enc2048: charmap.CodePage860,
164 enc2013: charmap.CodePage862,
165 enc2050: charmap.CodePage863,
166 enc2052: charmap.CodePage865,
167 enc2086: charmap.CodePage866,
168 enc2102: charmap.CodePage1047,
169 enc2091: charmap.CodePage1140,
170 enc4: charmap.ISO8859_1,
171 enc5: charmap.ISO8859_2,
172 enc6: charmap.ISO8859_3,
173 enc7: charmap.ISO8859_4,
174 enc8: charmap.ISO8859_5,
175 enc9: charmap.ISO8859_6,
176 enc81: charmap.ISO8859_6E,
177 enc82: charmap.ISO8859_6I,
178 enc10: charmap.ISO8859_7,
179 enc11: charmap.ISO8859_8,
180 enc84: charmap.ISO8859_8E,
181 enc85: charmap.ISO8859_8I,
182 enc12: charmap.ISO8859_9,
183 enc13: charmap.ISO8859_10,
184 enc109: charmap.ISO8859_13,
185 enc110: charmap.ISO8859_14,
186 enc111: charmap.ISO8859_15,
187 enc112: charmap.ISO8859_16,
188 enc2084: charmap.KOI8R,
189 enc2088: charmap.KOI8U,
190 enc2027: charmap.Macintosh,
191 enc2109: charmap.Windows874,
192 enc2250: charmap.Windows1250,
193 enc2251: charmap.Windows1251,
194 enc2252: charmap.Windows1252,
195 enc2253: charmap.Windows1253,
196 enc2254: charmap.Windows1254,
197 enc2255: charmap.Windows1255,
198 enc2256: charmap.Windows1256,
199 enc2257: charmap.Windows1257,
200 enc2258: charmap.Windows1258,
201 enc18: japanese.EUCJP,
202 enc39: japanese.ISO2022JP,
203 enc17: japanese.ShiftJIS,
205 enc114: simplifiedchinese.GB18030,
206 enc113: simplifiedchinese.GBK,
207 enc2085: simplifiedchinese.HZGB2312,
208 enc2026: traditionalchinese.Big5,