forked from martinlindhe/subtitles
-
Notifications
You must be signed in to change notification settings - Fork 0
/
encoding.go
167 lines (137 loc) · 2.69 KB
/
encoding.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
package subtitles
import (
"bytes"
"fmt"
"io"
"strings"
"unicode/utf16"
"unicode/utf8"
)
// readAsUTF8 tries to convert io.Reader to UTF8
func readAsUTF8(r io.Reader) (string, error) {
buf := new(bytes.Buffer)
_, err := buf.ReadFrom(r)
if err != nil {
return "", nil
}
utf8 := ConvertToUTF8(buf.Bytes())
return utf8, nil
}
// ConvertToUTF8 returns a utf8 string
func ConvertToUTF8(b []byte) string {
s := ""
if hasUTF16BeMarker(b) {
s, _ = utf16ToUTF8(b[2:], true)
} else if hasUTF16LeMarker(b) {
s, _ = utf16ToUTF8(b[2:], false)
} else if hasUTF8Marker(b) {
s = string(b[3:])
} else if looksLikeLatin1(b) {
s = latin1toUTF8(b)
} else {
s = string(b)
}
return normalizeLineFeeds(s)
}
// NormalizeLineFeeds will return a string with \n as linefeeds
func normalizeLineFeeds(s string) string {
if len(s) < 80 {
return s
}
r := 0
n := 0
for i := 0; i < 80; i++ {
if s[i] == '\r' {
r++
} else if s[i] == '\n' {
n++
}
}
if n == 0 && r > 0 {
// older Mac files has \r linebreak
return strings.Replace(s, "\r", "\n", -1)
}
return strings.Replace(s, "\r\n", "\n", -1)
}
func looksLikeLatin1(b []byte) bool {
swe := float64(0)
for i := 0; i < len(b); i++ {
switch {
case b[i] == 0xe5: // å
swe++
case b[i] == 0xe4: // ä
swe++
case b[i] == 0xf6: // ö
swe++
case b[i] == 0xc4: // Ä
swe++
case b[i] == 0xc5: // Å
swe++
case b[i] == 0xd6: // Ö
swe++
}
}
// calc percent of swe letters
pct := (swe / float64(len(b))) * 100
if pct >= 1 {
return true
}
if pct > 0 {
//fmt.Printf("XXX %v %% swe letters, %v\n", pct, swe)
}
return false
}
func latin1toUTF8(in []byte) string {
res := make([]rune, len(in))
for i, b := range in {
res[i] = rune(b)
}
return string(res)
}
func hasUTF8Marker(b []byte) bool {
if len(b) < 3 {
return false
}
if b[0] == 0xef && b[1] == 0xbb && b[2] == 0xbf {
return true
}
return false
}
func hasUTF16BeMarker(b []byte) bool {
if len(b) < 2 {
return false
}
if b[0] == 0xfe && b[1] == 0xff {
return true
}
return false
}
func hasUTF16LeMarker(b []byte) bool {
if len(b) < 2 {
return false
}
if b[0] == 0xff && b[1] == 0xfe {
return true
}
return false
}
func utf16ToUTF8(b []byte, bigEndian bool) (string, error) {
if len(b)%2 != 0 {
return "", fmt.Errorf("Must have even length byte slice")
}
u16s := make([]uint16, 1)
ret := &bytes.Buffer{}
b8buf := make([]byte, 4)
lb := len(b)
for i := 0; i < lb; i += 2 {
if bigEndian {
u16s[0] = uint16(b[i+1]) + (uint16(b[i]) << 8)
} else {
u16s[0] = uint16(b[i]) + (uint16(b[i+1]) << 8)
}
r := utf16.Decode(u16s)
n := utf8.EncodeRune(b8buf, r[0])
ret.Write(b8buf[:n])
}
return ret.String(), nil
}