mirror of
https://github.com/duke-git/lancet.git
synced 2026-02-04 12:52:28 +08:00
feat: add IsGBK validator
This commit is contained in:
@@ -274,3 +274,40 @@ func IsZeroValue(value any) bool {
|
||||
|
||||
return reflect.DeepEqual(rv.Interface(), reflect.Zero(rv.Type()).Interface())
|
||||
}
|
||||
|
||||
// IsGBK check if data encoinge is gbk
|
||||
// Note: this function is implemented by whether double bytes fall within the encoding range of gbk,
|
||||
// while each byte of utf-8 encoding format falls within the encoding range of gbk.
|
||||
// Therefore, utf8.valid() should be called first to check whether it is not utf-8 encoding,
|
||||
// and then call IsGBK() to check gbk encoding. like below
|
||||
/**
|
||||
data := []byte("你好")
|
||||
if utf8.Valid(data) {
|
||||
fmt.Println("data encoding is utf-8")
|
||||
}else if(IsGBK(data)) {
|
||||
fmt.Println("data encoding is GBK")
|
||||
}
|
||||
fmt.Println("data encoding is unknown")
|
||||
**/
|
||||
func IsGBK(data []byte) bool {
|
||||
i := 0
|
||||
for i < len(data) {
|
||||
if data[i] <= 0xff {
|
||||
i++
|
||||
continue
|
||||
} else {
|
||||
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
|
||||
}
|
||||
|
||||
@@ -4,8 +4,10 @@ import (
|
||||
"fmt"
|
||||
"testing"
|
||||
"time"
|
||||
"unicode/utf8"
|
||||
|
||||
"github.com/duke-git/lancet/v2/internal"
|
||||
"golang.org/x/text/encoding/simplifiedchinese"
|
||||
)
|
||||
|
||||
func TestIsAllUpper(t *testing.T) {
|
||||
@@ -388,3 +390,13 @@ func TestIsZeroValue(t *testing.T) {
|
||||
assert.Equal(false, IsZeroValue(value))
|
||||
}
|
||||
}
|
||||
|
||||
func TestIsGBK(t *testing.T) {
|
||||
assert := internal.NewAssert(t, "TestIsGBK")
|
||||
|
||||
str := "你好"
|
||||
gbkData, _ := simplifiedchinese.GBK.NewEncoder().Bytes([]byte(str))
|
||||
|
||||
assert.Equal(true, IsGBK(gbkData))
|
||||
assert.Equal(false, utf8.Valid(gbkData))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user