1
2
3
4
5
6
7
8
9
10
11
12 package ianaindex
13
14 import (
15 "errors"
16 "sort"
17 "strings"
18
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"
27 )
28
29
30
31
32
33
34
35
36
37
38 var (
39
40 MIME *Index = mime
41
42
43
44 IANA *Index = iana
45
46
47 MIB *Index = mib
48
49 mime = &Index{mimeName, ianaToMIB, ianaAliases, encodings[:]}
50 iana = &Index{ianaName, ianaToMIB, ianaAliases, encodings[:]}
51 mib = &Index{mibName, ianaToMIB, ianaAliases, encodings[:]}
52 )
53
54
55
56
57 type Index struct {
58 names func(i int) string
59 toMIB []identifier.MIB
60 alias map[string]int
61 enc []encoding.Encoding
62 }
63
64 var (
65 errInvalidName = errors.New("ianaindex: invalid encoding name")
66 errUnknown = errors.New("ianaindex: unknown Encoding")
67 errUnsupported = errors.New("ianaindex: unsupported Encoding")
68 )
69
70
71
72
73
74
75
76 func (x *Index) Encoding(name string) (encoding.Encoding, error) {
77 name = strings.TrimSpace(name)
78
79 i, ok := x.alias[name]
80 if !ok {
81 i, ok = x.alias[strings.ToLower(name)]
82 if !ok {
83 return nil, errInvalidName
84 }
85 }
86 return x.enc[i], nil
87 }
88
89
90
91 func (x *Index) Name(e encoding.Encoding) (string, error) {
92 id, ok := e.(identifier.Interface)
93 if !ok {
94 return "", errUnknown
95 }
96 mib, _ := id.ID()
97 if mib == 0 {
98 return "", errUnknown
99 }
100 v := findMIB(x.toMIB, mib)
101 if v == -1 {
102 return "", errUnsupported
103 }
104 return x.names(v), nil
105 }
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124 func findMIB(x []identifier.MIB, mib identifier.MIB) int {
125 i := sort.Search(len(x), func(i int) bool { return x[i] >= mib })
126 if i < len(x) && x[i] == mib {
127 return i
128 }
129 return -1
130 }
131
132 const maxMIMENameLen = '0' - 1
133
134 func mimeName(x int) string {
135 n := ianaNames[x]
136
137 if n[0] <= maxMIMENameLen {
138 return n[1:n[0]]
139 }
140 return n
141 }
142
143 func ianaName(x int) string {
144 n := ianaNames[x]
145
146 if n[0] <= maxMIMENameLen {
147 return n[n[0]:]
148 }
149 return n
150 }
151
152 func mibName(x int) string {
153 return mibNames[x]
154 }
155
156 var encodings = [numIANA]encoding.Encoding{
157 enc3: asciiEnc,
158 enc106: unicode.UTF8,
159 enc1015: unicode.UTF16(unicode.BigEndian, unicode.UseBOM),
160 enc1013: unicode.UTF16(unicode.BigEndian, unicode.IgnoreBOM),
161 enc1014: unicode.UTF16(unicode.LittleEndian, unicode.IgnoreBOM),
162 enc2028: charmap.CodePage037,
163 enc2011: charmap.CodePage437,
164 enc2009: charmap.CodePage850,
165 enc2010: charmap.CodePage852,
166 enc2046: charmap.CodePage855,
167 enc2089: charmap.CodePage858,
168 enc2048: charmap.CodePage860,
169 enc2013: charmap.CodePage862,
170 enc2050: charmap.CodePage863,
171 enc2052: charmap.CodePage865,
172 enc2086: charmap.CodePage866,
173 enc2102: charmap.CodePage1047,
174 enc2091: charmap.CodePage1140,
175 enc4: charmap.ISO8859_1,
176 enc5: charmap.ISO8859_2,
177 enc6: charmap.ISO8859_3,
178 enc7: charmap.ISO8859_4,
179 enc8: charmap.ISO8859_5,
180 enc9: charmap.ISO8859_6,
181 enc81: charmap.ISO8859_6E,
182 enc82: charmap.ISO8859_6I,
183 enc10: charmap.ISO8859_7,
184 enc11: charmap.ISO8859_8,
185 enc84: charmap.ISO8859_8E,
186 enc85: charmap.ISO8859_8I,
187 enc12: charmap.ISO8859_9,
188 enc13: charmap.ISO8859_10,
189 enc109: charmap.ISO8859_13,
190 enc110: charmap.ISO8859_14,
191 enc111: charmap.ISO8859_15,
192 enc112: charmap.ISO8859_16,
193 enc2084: charmap.KOI8R,
194 enc2088: charmap.KOI8U,
195 enc2027: charmap.Macintosh,
196 enc2109: charmap.Windows874,
197 enc2250: charmap.Windows1250,
198 enc2251: charmap.Windows1251,
199 enc2252: charmap.Windows1252,
200 enc2253: charmap.Windows1253,
201 enc2254: charmap.Windows1254,
202 enc2255: charmap.Windows1255,
203 enc2256: charmap.Windows1256,
204 enc2257: charmap.Windows1257,
205 enc2258: charmap.Windows1258,
206 enc18: japanese.EUCJP,
207 enc39: japanese.ISO2022JP,
208 enc17: japanese.ShiftJIS,
209 enc38: korean.EUCKR,
210 enc114: simplifiedchinese.GB18030,
211 enc113: simplifiedchinese.GBK,
212 enc2085: simplifiedchinese.HZGB2312,
213 enc2026: traditionalchinese.Big5,
214 }
215
View as plain text