-
Notifications
You must be signed in to change notification settings - Fork 0
/
decode.go
52 lines (47 loc) · 1.16 KB
/
decode.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
package subkers
import (
"bytes"
"errors"
"fmt"
"io/ioutil"
"strings"
"github.com/saintfish/chardet"
"golang.org/x/text/encoding/charmap"
"golang.org/x/text/transform"
)
// decode needed for translation any to utf-8 (but it does work only with Windows1251)
func decode(markers []Marker) ([]Marker, error) {
var buffer []byte
for _, m := range markers {
buffer = append(buffer, []byte(strings.Join(m.Lines, ""))...)
}
// Find out the encoding
detector := chardet.NewTextDetector()
result, err := detector.DetectBest(buffer)
if err != nil {
return nil, errors.New(fmt.Sprint("Detector error:", err))
}
// Process based on encoding
// it will be okay
switch result.Charset {
case "UTF-8":
// its okay
// just let it go
return markers, nil
default:
// should work almost anyway
// let it be so
for i, m := range markers {
for j, l := range m.Lines {
O := transform.NewReader(bytes.NewReader([]byte(l)), charmap.Windows1251.NewDecoder())
line, e := ioutil.ReadAll(O)
if e != nil {
return nil, errors.New(fmt.Sprint("Can't read:", err))
}
markers[i].Lines[j] = string(line)
}
}
// its okay
return markers, nil
}
}