-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdecoder.go
More file actions
36 lines (29 loc) · 721 Bytes
/
decoder.go
File metadata and controls
36 lines (29 loc) · 721 Bytes
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
package futf
import (
"bytes"
"golang.org/x/text/encoding"
"golang.org/x/text/transform"
)
type Decoder interface {
Decode(str []byte) []byte
}
type CharmapDecoder struct {
encoding.Encoding
}
func (d *CharmapDecoder) Decode(str []byte) []byte {
var b bytes.Buffer
wr := transform.NewWriter(&b, d.NewDecoder())
_, err := wr.Write(str)
if err != nil {
return str
}
return b.Bytes()
}
// Solves cases when original text in Windows1251 was missinterpreted as Windows1252 and than was encoded to unicode
type MissCodedDecoder struct {
origin encoding.Encoding
misscoded encoding.Encoding
}
func (d *MissCodedDecoder) Decode(str []byte) []byte {
return Dec(Enc(string(str), d.misscoded), d.origin)
}