-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathtransform.go
More file actions
401 lines (355 loc) · 10.6 KB
/
transform.go
File metadata and controls
401 lines (355 loc) · 10.6 KB
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
package text
import (
"bytes"
"errors"
"io"
"io/ioutil"
"golang.org/x/text/encoding"
"golang.org/x/text/encoding/japanese"
"golang.org/x/text/encoding/unicode"
"golang.org/x/text/transform"
)
const (
UTF8BOM = "\xef\xbb\xbf"
UTF16BEBOM = "\xfe\xff"
UTF16LEBOM = "\xff\xfe"
)
const (
SurrogatePairHighBegin = "\xd8\x00"
SurrogatePairHighEnd = "\xdb\xff"
SurrogatePairLowBegin = "\xdc\x00"
SurrogatePairLowEnd = "\xdf\xff"
)
func IsHighSurrogate(r rune) bool {
return rune(SurrogatePairHighBegin[0])<<8|rune(SurrogatePairHighBegin[1]) <= r && r <= rune(SurrogatePairHighEnd[0])<<8|rune(SurrogatePairHighEnd[1])
}
func IsLowSurrogate(r rune) bool {
return rune(SurrogatePairLowBegin[0])<<8|rune(SurrogatePairLowBegin[1]) <= r && r <= rune(SurrogatePairLowEnd[0])<<8|rune(SurrogatePairLowEnd[1])
}
var ErrUnknownEncoding = errors.New("cannot detect character encoding")
var ErrInvalidEncoding = errors.New("invalid character encoding")
// DetectEncoding Detects character encoding
func DetectEncoding(r io.ReadSeeker) (detected Encoding, err error) {
return DetectInSpecifiedEncoding(r, AUTO)
}
func DetectInSpecifiedEncoding(r io.ReadSeeker, enc Encoding) (detected Encoding, err error) {
switch enc {
case UTF8M, UTF16BEM, UTF16LEM, UTF16BE, UTF16LE, SJIS:
return enc, nil
}
defer func() {
if _, e := r.Seek(0, io.SeekStart); e != nil {
err = e
}
}()
if _, err := r.Seek(0, io.SeekStart); err != nil {
return detected, err
}
lead := make([]byte, 3)
n, err := r.Read(lead)
if n < 1 && err == io.EOF {
return UTF8, nil
}
if enc == AUTO || enc == UTF8 {
if n == 3 && lead[0] == UTF8BOM[0] && lead[1] == UTF8BOM[1] && lead[2] == UTF8BOM[2] {
return UTF8M, nil
}
}
if (enc == AUTO || enc == UTF16) && 1 < n {
if lead[0] == UTF16BEBOM[0] && lead[1] == UTF16BEBOM[1] {
return UTF16BEM, nil
} else if lead[0] == UTF16LEBOM[0] && lead[1] == UTF16LEBOM[1] {
return UTF16LEM, nil
}
}
if enc == UTF8 {
return UTF8, nil
} else if enc == UTF16 {
return UTF16BE, nil
}
if _, err := r.Seek(0, io.SeekStart); err != nil {
return detected, err
}
lead = make([]byte, 1024)
n, err = r.Read(lead)
return InferEncoding(lead[:n], err == io.EOF)
}
func InferEncoding(b []byte, eof bool) (Encoding, error) {
if len(b) < 1 {
return UTF8, nil
}
var isInRange = func(pos int) bool {
return pos < len(b)
}
var between = func(b byte, low byte, high byte) bool {
return low <= b && b <= high
}
utf8Idx := 0
utf16Idx := 1
sjisIdx := 2
nextPos := []int{0, 0, 0}
validEnc := make([]int, 0, 3)
latinBeCnt := 0
latinLeCnt := 0
pos := 0
for {
validEnc = validEnc[:0]
for i := range nextPos {
if -1 < nextPos[i] {
validEnc = append(validEnc, i)
}
}
if !isInRange(pos) {
break
}
if (len(validEnc)) < 2 && !(len(validEnc) == 1 && -1 < nextPos[utf16Idx]) {
break
}
if nextPos[sjisIdx] == pos {
switch {
case between(b[pos], 0x00, 0x7f) || between(b[pos], 0xa0, 0xdf):
nextPos[sjisIdx] = nextPos[sjisIdx] + 1
case between(b[pos], 0x81, 0x9f) || between(b[pos], 0xe0, 0xef):
if (eof && !isInRange(pos+1)) ||
(isInRange(pos+1) && !between(b[pos+1], 0x40, 0x7e) && !between(b[pos+1], 0x80, 0xfc)) {
nextPos[sjisIdx] = -1
} else {
nextPos[sjisIdx] = nextPos[sjisIdx] + 2
}
default:
nextPos[sjisIdx] = -1
}
}
if nextPos[utf8Idx] == pos {
switch {
case between(b[pos], 0x00, 0x7f):
nextPos[utf8Idx] = nextPos[utf8Idx] + 1
case between(b[pos], 0xc2, 0xdf):
if (eof && !isInRange(pos+1)) ||
(isInRange(pos+1) && !between(b[pos+1], 0x80, 0xbf)) {
nextPos[utf8Idx] = -1
} else {
nextPos[utf8Idx] = nextPos[utf8Idx] + 2
}
case between(b[pos], 0xe0, 0xef):
if (eof && !isInRange(pos+2)) ||
(isInRange(pos+2) && !(between(b[pos+1], 0x80, 0xbf) && between(b[pos+2], 0x80, 0xbf))) {
nextPos[utf8Idx] = -1
} else {
nextPos[utf8Idx] = nextPos[utf8Idx] + 3
}
case between(b[pos], 0xf0, 0xf7):
if (eof && !isInRange(pos+3)) ||
(isInRange(pos+3) && !(between(b[pos+1], 0x80, 0xbf) && between(b[pos+2], 0x80, 0xbf) && between(b[pos+3], 0x80, 0xbf))) {
nextPos[utf8Idx] = -1
} else {
nextPos[utf8Idx] = nextPos[utf8Idx] + 4
}
default:
nextPos[utf8Idx] = -1
}
}
if nextPos[utf16Idx] == pos {
if eof && !isInRange(pos+1) {
nextPos[utf16Idx] = -1
} else if isInRange(pos + 1) {
if b[pos] == 0x00 && between(b[pos+1], 0x01, 0xff) {
latinBeCnt++
} else if b[pos+1] == 0x00 && between(b[pos], 0x00, 0xff) {
latinLeCnt++
}
if 5 < latinBeCnt || 5 < latinLeCnt {
goto InferredAsUTF16
}
if between(b[pos], 0xdc, 0xdf) {
if IsLowSurrogate(rune(b[pos])<<8 | rune(b[pos+1])) {
nextPos[utf16Idx] = -1
}
}
if -1 < nextPos[utf16Idx] && between(b[pos+1], 0xdc, 0xdf) {
if IsLowSurrogate(rune(b[pos+1])<<8 | rune(b[pos])) {
nextPos[utf16Idx] = -1
}
}
}
if isInRange(pos + 3) {
if -1 < nextPos[utf16Idx] && between(b[pos], 0xd8, 0xdb) {
if IsHighSurrogate(rune(b[pos])<<8 | rune(b[pos+1])) {
if IsLowSurrogate(rune(b[pos+2])<<8 | rune(b[pos+3])) {
return UTF16BE, nil
}
nextPos[utf16Idx] = -1
}
}
if -1 < nextPos[utf16Idx] && between(b[pos+1], 0xd8, 0xdb) {
if IsHighSurrogate(rune(b[pos+1])<<8 | rune(b[pos])) {
if IsLowSurrogate(rune(b[pos+3])<<8 | rune(b[pos+2])) {
return UTF16LE, nil
}
nextPos[utf16Idx] = -1
}
}
}
if -1 < nextPos[utf16Idx] {
nextPos[utf16Idx] = nextPos[utf16Idx] + 2
}
}
pos++
}
if 0 < latinBeCnt || 0 < latinLeCnt {
if -1 < nextPos[utf16Idx] {
goto InferredAsUTF16
} else {
return UTF8, ErrUnknownEncoding
}
}
switch len(validEnc) {
case 0:
return UTF8, ErrUnknownEncoding
case 1:
switch validEnc[0] {
case utf8Idx:
return UTF8, nil
case sjisIdx:
return SJIS, nil
default:
goto InferredAsUTF16
}
default:
if -1 < nextPos[utf8Idx] {
return UTF8, nil
} else if -1 < nextPos[sjisIdx] {
return SJIS, nil
} else {
goto InferredAsUTF16
}
}
InferredAsUTF16:
if latinBeCnt < latinLeCnt {
return UTF16LE, nil
}
return UTF16BE, nil
}
// GetTransformEncoder gets a reader to transform character encoding from UTF-8 to another encoding.
func GetTransformEncoder(r io.Reader, enc Encoding) (io.Reader, error) {
switch enc {
case UTF8:
return transform.NewReader(r, unicode.UTF8.NewEncoder()), nil
case UTF8M:
return transform.NewReader(r, NewUTF8MEncoder()), nil
case UTF16:
return transform.NewReader(r, unicode.UTF16(unicode.BigEndian, unicode.IgnoreBOM).NewEncoder()), nil
case UTF16BE:
return transform.NewReader(r, unicode.UTF16(unicode.BigEndian, unicode.IgnoreBOM).NewEncoder()), nil
case UTF16LE:
return transform.NewReader(r, unicode.UTF16(unicode.LittleEndian, unicode.IgnoreBOM).NewEncoder()), nil
case UTF16BEM:
return transform.NewReader(r, unicode.UTF16(unicode.BigEndian, unicode.ExpectBOM).NewEncoder()), nil
case UTF16LEM:
return transform.NewReader(r, unicode.UTF16(unicode.LittleEndian, unicode.ExpectBOM).NewEncoder()), nil
case SJIS:
return transform.NewReader(r, japanese.ShiftJIS.NewEncoder()), nil
default:
return nil, ErrInvalidEncoding
}
}
// GetTransformDecoder gets a reader to transform character encoding from any encoding to UTF-8.
func GetTransformDecoder(r io.Reader, enc Encoding) (io.Reader, error) {
switch enc {
case UTF8:
return transform.NewReader(r, unicode.UTF8.NewDecoder()), nil
case UTF8M:
return transform.NewReader(r, unicode.BOMOverride(unicode.UTF8.NewDecoder())), nil
case UTF16:
return transform.NewReader(r, unicode.UTF16(unicode.BigEndian, unicode.UseBOM).NewDecoder()), nil
case UTF16BE:
return transform.NewReader(r, unicode.UTF16(unicode.BigEndian, unicode.IgnoreBOM).NewDecoder()), nil
case UTF16LE:
return transform.NewReader(r, unicode.UTF16(unicode.LittleEndian, unicode.IgnoreBOM).NewDecoder()), nil
case UTF16BEM:
return transform.NewReader(r, unicode.UTF16(unicode.BigEndian, unicode.ExpectBOM).NewDecoder()), nil
case UTF16LEM:
return transform.NewReader(r, unicode.UTF16(unicode.LittleEndian, unicode.ExpectBOM).NewDecoder()), nil
case SJIS:
return transform.NewReader(r, japanese.ShiftJIS.NewDecoder()), nil
default:
return nil, ErrInvalidEncoding
}
}
// GetTransformWriter gets a writer to transform character encoding from UTF-8 to another encoding.
func GetTransformWriter(w io.Writer, enc Encoding) (io.Writer, error) {
switch enc {
case UTF8:
return transform.NewWriter(w, unicode.UTF8.NewEncoder()), nil
case UTF8M:
return transform.NewWriter(w, NewUTF8MEncoder()), nil
case UTF16:
return transform.NewWriter(w, unicode.UTF16(unicode.BigEndian, unicode.IgnoreBOM).NewEncoder()), nil
case UTF16BE:
return transform.NewWriter(w, unicode.UTF16(unicode.BigEndian, unicode.IgnoreBOM).NewEncoder()), nil
case UTF16LE:
return transform.NewWriter(w, unicode.UTF16(unicode.LittleEndian, unicode.IgnoreBOM).NewEncoder()), nil
case UTF16BEM:
return transform.NewWriter(w, unicode.UTF16(unicode.BigEndian, unicode.ExpectBOM).NewEncoder()), nil
case UTF16LEM:
return transform.NewWriter(w, unicode.UTF16(unicode.LittleEndian, unicode.ExpectBOM).NewEncoder()), nil
case SJIS:
return transform.NewWriter(w, japanese.ShiftJIS.NewEncoder()), nil
default:
return nil, ErrInvalidEncoding
}
}
// Encode a string from UTF-8 to another encoding.
func Encode(src []byte, enc Encoding) ([]byte, error) {
r, err := GetTransformEncoder(bytes.NewReader(src), enc)
if err != nil {
return nil, err
}
return ioutil.ReadAll(r)
}
// Decode a string from any encoding to UTF-8.
func Decode(src []byte, enc Encoding) ([]byte, error) {
r, err := GetTransformDecoder(bytes.NewReader(src), enc)
if err != nil {
return nil, err
}
return ioutil.ReadAll(r)
}
type bomPolicy uint8
const (
writeBOM bomPolicy = 0x01
ignoreBOM bomPolicy = 0
)
type UTF8MEncoder struct {
initialBOMPolicy bomPolicy
currentBOMPolicy bomPolicy
}
func NewUTF8MEncoder() *encoding.Encoder {
return &encoding.Encoder{Transformer: &UTF8MEncoder{
initialBOMPolicy: writeBOM,
currentBOMPolicy: writeBOM,
}}
}
func (u *UTF8MEncoder) Reset() {
u.currentBOMPolicy = u.initialBOMPolicy
}
func (u *UTF8MEncoder) Transform(dst, src []byte, atEOF bool) (nDst, nSrc int, err error) {
if u.currentBOMPolicy&writeBOM != 0 {
if len(dst) < 3 {
return 0, 0, transform.ErrShortDst
}
bom := []byte(UTF8BOM)
dst[0], dst[1], dst[2] = bom[0], bom[1], bom[2]
u.currentBOMPolicy = ignoreBOM
nDst = 3
}
for i := range src {
if nDst+1 > len(dst) {
return nDst, nSrc, transform.ErrShortDst
}
dst[nDst] = src[i]
nDst++
nSrc++
}
return nDst, nSrc, err
}