mirror of https://github.com/Xhofe/alist
60 lines
1.0 KiB
Go
60 lines
1.0 KiB
Go
package utils
|
||
|
||
import (
|
||
"bytes"
|
||
"golang.org/x/text/encoding/simplifiedchinese"
|
||
"golang.org/x/text/transform"
|
||
"io/ioutil"
|
||
"unicode/utf8"
|
||
)
|
||
|
||
func IsGBK(data []byte) bool {
|
||
length := len(data)
|
||
var i = 0
|
||
for i < length {
|
||
if data[i] <= 0x7f {
|
||
//编码0~127,只有一个字节的编码,兼容ASCII码
|
||
i++
|
||
continue
|
||
} else {
|
||
//大于127的使用双字节编码,落在gbk编码范围内的字符
|
||
if data[i] >= 0x81 &&
|
||
data[i] <= 0xfe &&
|
||
data[i+1] >= 0x40 &&
|
||
data[i+1] <= 0xfe &&
|
||
data[i+1] != 0xf7 {
|
||
i += 2
|
||
continue
|
||
} else {
|
||
return false
|
||
}
|
||
}
|
||
}
|
||
return true
|
||
}
|
||
|
||
const (
|
||
GBK string = "GBK"
|
||
UTF8 string = "UTF8"
|
||
UNKNOWN string = "UNKNOWN"
|
||
)
|
||
|
||
func GetStrCoding(data []byte) string {
|
||
if utf8.Valid(data) {
|
||
return UTF8
|
||
} else if IsGBK(data) {
|
||
return GBK
|
||
} else {
|
||
return UNKNOWN
|
||
}
|
||
}
|
||
|
||
func GbkToUtf8(s []byte) ([]byte, error) {
|
||
reader := transform.NewReader(bytes.NewReader(s), simplifiedchinese.GBK.NewDecoder())
|
||
d, e := ioutil.ReadAll(reader)
|
||
if e != nil {
|
||
return nil, e
|
||
}
|
||
return d, nil
|
||
}
|