/
simple.go
185 lines (170 loc) · 4.13 KB
/
simple.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
/*
Copyright 2013 Tamás Gulácsi
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package text
import (
"strconv"
"strings"
"unicode/utf8"
"golang.org/x/text/encoding"
"golang.org/x/text/encoding/charmap"
"golang.org/x/text/transform"
)
// GetEncoding returns the encoding.Encoding for the text name of the encoding
// Returns nil if the encoding is not found.
//
// Knows the ISO8859 family, KOI8, Windows and Mac codepages,
// but misses other Asian codepages
func GetEncoding(name string) encoding.Encoding {
name = strings.ToLower(strings.TrimSpace(name))
var err error
if strings.HasPrefix(name, "iso") {
i := strings.LastIndex(name, "-")
if i < 0 {
if len(name) != len("iso88591") {
return nil
}
i = len(name) - 2
}
if i, err = strconv.Atoi(name[i+1:]); err != nil {
return nil
}
switch i {
case 1:
return ISO8859_1
case 2:
return charmap.ISO8859_2
case 3:
return charmap.ISO8859_3
case 4:
return charmap.ISO8859_4
case 5:
return charmap.ISO8859_5
case 6:
return charmap.ISO8859_6
case 7:
return charmap.ISO8859_7
case 8:
return charmap.ISO8859_8
case 10:
return charmap.ISO8859_10
case 13:
return charmap.ISO8859_13
case 14:
return charmap.ISO8859_14
case 15:
return charmap.ISO8859_15
case 16:
return charmap.ISO8859_16
}
return nil
}
switch strings.Replace(name, "-", "", -1) {
case "utf8":
return encoding.Replacement
case "cp437":
return charmap.CodePage437
case "cp850":
return charmap.CodePage850
case "cp852":
return charmap.CodePage852
case "cp855":
return charmap.CodePage855
case "cp858":
return charmap.CodePage858
case "cp862":
return charmap.CodePage862
case "cp866":
return charmap.CodePage866
case "koi8r":
return charmap.KOI8R
case "koi8u":
return charmap.KOI8U
case "mac", "macintosh":
return charmap.Macintosh
case "macCyrillic", "macintoshCyrillic":
return charmap.MacintoshCyrillic
case "windows1250", "win1250":
return charmap.Windows1250
case "windows1251", "win1251":
return charmap.Windows1251
case "windows1252", "win1252":
return charmap.Windows1252
case "windows1253", "win1253":
return charmap.Windows1253
case "windows1254", "win1254":
return charmap.Windows1254
case "windows1255", "win1255":
return charmap.Windows1255
case "windows1256", "win1256":
return charmap.Windows1256
case "windows1257", "win1257":
return charmap.Windows1257
case "windows1258", "win1258":
return charmap.Windows1258
case "windows874", "win874":
return charmap.Windows874
}
return nil
}
// ISO8859_1 is the Encoding for ISO-8859-1
var ISO8859_1 encoding.Encoding = cmap("ISO8859-1")
type cmap string
func (m cmap) NewDecoder() *encoding.Decoder {
return &encoding.Decoder{Transformer: fromISO8859_1{}}
}
func (m cmap) NewEncoder() *encoding.Encoder {
return &encoding.Encoder{Transformer: toISO8859_1{}}
}
type fromISO8859_1 struct {
transform.NopResetter
}
type toISO8859_1 struct {
transform.NopResetter
}
func (m fromISO8859_1) Transform(dst, src []byte, atEOF bool) (nDst, nSrc int, err error) {
var b [4]byte
for i, c := range src {
n := utf8.EncodeRune(b[:], rune(c))
if nDst+n > len(dst) {
err = transform.ErrShortDst
break
}
nSrc = i + 1
copy(dst[nDst:], b[:n])
nDst += n
}
return nDst, nSrc, err
}
func (m toISO8859_1) Transform(dst, src []byte, atEOF bool) (nDst, nSrc int, err error) {
for {
r, n := utf8.DecodeRune(src[nSrc:])
if n == 0 { //EOF
break
}
if r == utf8.RuneError {
err = encoding.ErrInvalidUTF8
break
}
if r > 0xff {
r = '?'
}
if nDst+1 > len(dst) {
err = transform.ErrShortDst
break
}
nSrc += n
dst[nDst] = byte(r)
nDst++
}
return nDst, nSrc, err
}