-
Notifications
You must be signed in to change notification settings - Fork 3
/
encoding.go
66 lines (57 loc) · 1.68 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
package charsetKit
import (
"github.com/gogf/gf/v2/encoding/gcharset"
"github.com/richelieu-yang/chimera/v3/src/core/interfaceKit"
"github.com/saintfish/chardet"
"golang.org/x/net/html/charset"
"golang.org/x/text/encoding"
"unicode/utf8"
)
// IsSupported 是否支持 指定字符集 ?
var IsSupported func(charset string) bool = gcharset.Supported
// DetermineEncoding 编码推断.
/*
Deprecated: 不一定准(检测GBK文本有问题,推断出来的是 "windows-1252").
*/
var DetermineEncoding func(content []byte, contentType string) (e encoding.Encoding, name string, certain bool) = charset.DetermineEncoding
// Detect 检测文本的编码.
/*
Deprecated: 不一定准(检测GBK文本有问题,推断出来的是 "ISO-8859-1").
*/
func Detect(data []byte) (charset string, err error) {
if err = interfaceKit.AssertNotNil(data, "data"); err != nil {
return
}
var r *chardet.Result
r, err = chardet.NewTextDetector().DetectBest(data)
if err != nil {
return
}
return r.Charset, nil
}
var IsUTF8 func(data []byte) bool = utf8.Valid
var IsUTF8String func(s string) bool = utf8.ValidString
// IsGBK
/*
PS: 在Go语言中,你可以通过检查字节序列是否落在 GBK 编码范围内来判断文本编码是否为 GBK。
*/
func IsGBK(data []byte) bool {
if IsUTF8(data) {
return false
}
i := 0
for i < len(data) {
if data[i] <= 0x7f {
// 编码0~127,只有一个字节的编码,兼容ASCII码
i++
continue
}
if data[i] >= 0x81 && data[i] <= 0xfe && data[i+1] >= 0x40 && data[i+1] <= 0xfe && data[i+1] != 0xf7 {
// 大于127的使用双字节编码,落在gbk编码范围内的字符
i += 2
continue
}
return false
}
return true
}