-
Notifications
You must be signed in to change notification settings - Fork 0
/
charsetutil.go
202 lines (170 loc) · 5.13 KB
/
charsetutil.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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
package charsetutil
import (
"bytes"
"fmt"
"io"
"io/ioutil"
"strings"
"github.com/gogs/chardet"
"golang.org/x/net/html/charset"
"golang.org/x/text/transform"
)
func panicIfError(err error) {
if err != nil {
panic(err)
}
}
// CharsetGuess is a guessd charcter set
type CharsetGuess interface {
// Charset returns a guessed charcter set
Charset() string
// Language returns a guessed language
Language() string
// Confidence returns a confidence of this guess
Confidence() int
}
type charsetGuess struct {
*chardet.Result
}
func (g *charsetGuess) Charset() string {
return g.Result.Charset
}
func (g *charsetGuess) Language() string {
return g.Result.Language
}
func (g *charsetGuess) Confidence() int {
return g.Result.Confidence
}
// GuessBytes guesses a character set of given bytes
func GuessBytes(s []byte) (CharsetGuess, error) {
detector := chardet.NewTextDetector()
result, err := detector.DetectBest(s)
if err != nil {
return nil, err
}
return &charsetGuess{result}, err
}
// Guess guesses a character set of given bytes
func Guess(s []byte) (CharsetGuess, error) {
return GuessBytes(s)
}
// GuessBytes guesses a character set of given Reader
func GuessReader(s io.Reader) (CharsetGuess, error) {
detector := chardet.NewTextDetector()
buf := make([]byte, 128)
if _, err := s.Read(buf); err != nil {
return nil, err
}
result, err := detector.DetectBest(buf)
if err != nil {
return nil, err
}
return &charsetGuess{result}, err
}
// GuessBytes guesses a character set of given string
func GuessString(s string) (CharsetGuess, error) {
detector := chardet.NewTextDetector()
result, err := detector.DetectBest([]byte(s))
if err != nil {
return nil, err
}
return &charsetGuess{result}, err
}
// DecodeReader converts given Reader to a UTF-8 string
func DecodeReader(s io.Reader, enc string) (string, error) {
reader, err := charset.NewReaderLabel(enc, s)
if err != nil {
return "", err
}
bytes, err := ioutil.ReadAll(reader)
if err != nil {
return "", err
}
return string(bytes), nil
}
// MustDecodeReader converts given Reader to a UTF-8 string and panics if errros occur.
func MustDecodeReader(s io.Reader, enc string) string {
ret, err := DecodeReader(s, enc)
panicIfError(err)
return ret
}
// DecodeBytes converts given bytes to a UTF-8 string
func DecodeBytes(s []byte, enc string) (string, error) {
return DecodeReader(bytes.NewReader(s), enc)
}
// MustDecodeBytes converts given bytes to a UTF-8 string and panics if errros occur.
func MustDecodeBytes(s []byte, enc string) string {
ret, err := DecodeReader(bytes.NewReader(s), enc)
panicIfError(err)
return ret
}
// DecodeString converts given string to a UTF-8 string
func DecodeString(s, enc string) (string, error) {
return DecodeReader(strings.NewReader(s), enc)
}
// MustDecodeString converts given string to a UTF-8 string and panics if errros occur.
func MustDecodeString(s, enc string) string {
ret, err := DecodeReader(strings.NewReader(s), enc)
panicIfError(err)
return ret
}
// DecodeBytes converts given bytes to a UTF-8 string
func Decode(s []byte, enc string) (string, error) {
return DecodeReader(bytes.NewReader(s), enc)
}
// MustDecodeBytes converts given bytes to a UTF-8 string and panics if errros occur.
func MustDecode(s []byte, enc string) string {
ret, err := DecodeReader(bytes.NewReader(s), enc)
panicIfError(err)
return ret
}
// EncodeReader converts a Reader to bytes encoded with given encoding
func EncodeReader(s io.Reader, enc string) ([]byte, error) {
e, _ := charset.Lookup(enc)
if e == nil {
return nil, fmt.Errorf("unsupported charset: %q", enc)
}
var buf bytes.Buffer
writer := transform.NewWriter(&buf, e.NewEncoder())
_, err := io.Copy(writer, s)
if err != nil {
return nil, err
}
return buf.Bytes(), nil
}
// MustEncodeReader converts a Reader to bytes encoded with given encoding and panics if errors occur
func MustEncodeReader(s io.Reader, enc string) []byte {
ret, err := EncodeReader(s, enc)
panicIfError(err)
return ret
}
// EncodeBytes converts bytes to bytes encoded with given encoding
func EncodeBytes(s []byte, enc string) ([]byte, error) {
return EncodeReader(bytes.NewReader(s), enc)
}
// MustEncodeBytes converts a bytes to bytes encoded with given encoding and panics if errors occur
func MustEncodeBytes(s []byte, enc string) []byte {
ret, err := EncodeReader(bytes.NewReader(s), enc)
panicIfError(err)
return ret
}
// EncodeString converts a string to bytes encoded with given encoding
func EncodeString(s, enc string) ([]byte, error) {
return EncodeReader(strings.NewReader(s), enc)
}
// MustEncodeString converts a bytes to bytes encoded with given encoding and panics if errors occur
func MustEncodeString(s, enc string) []byte {
ret, err := EncodeReader(strings.NewReader(s), enc)
panicIfError(err)
return ret
}
// Encode converts a string to bytes encoded with given encoding
func Encode(s string, enc string) ([]byte, error) {
return EncodeReader(strings.NewReader(s), enc)
}
// MustEncode converts a bytes to bytes encoded with given encoding and panics if errors occur
func MustEncode(s string, enc string) []byte {
ret, err := EncodeReader(strings.NewReader(s), enc)
panicIfError(err)
return ret
}