diff --git a/go.mod b/go.mod index db6f8bd..3377344 100644 --- a/go.mod +++ b/go.mod @@ -1,3 +1,5 @@ module github.com/duke-git/lancet/v2 go 1.18 + +require golang.org/x/text v0.5.0 diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..a0726a8 --- /dev/null +++ b/go.sum @@ -0,0 +1,2 @@ +golang.org/x/text v0.5.0 h1:OLmvp0KP+FVG99Ct/qFiL/Fhk4zp4QQnZ7b2U+5piUM= +golang.org/x/text v0.5.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8= diff --git a/validator/validator.go b/validator/validator.go index 2afaaa3..8fb463d 100644 --- a/validator/validator.go +++ b/validator/validator.go @@ -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 +} diff --git a/validator/validator_test.go b/validator/validator_test.go index ad9a9e1..6a237a5 100644 --- a/validator/validator_test.go +++ b/validator/validator_test.go @@ -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)) +}