diff --git a/docs/validator.md b/docs/validator.md index ba5896f..db0c228 100644 --- a/docs/validator.md +++ b/docs/validator.md @@ -47,6 +47,7 @@ import ( - [IsUrl](#IsUrl) - [IsWeakPassword](#IsWeakPassword) - [IsZeroValue](#IsZeroValue) +- [IsGBK](#IsGBK)
@@ -821,7 +822,34 @@ func main() { ``` - +### IsGBK +Checks if data encoding is gbk(Chinese character internal code extension specification). 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 the example.
+ +Signature: + +```go +func IsGBK(data []byte) bool +``` +Example: + +```go +import ( + "fmt" + "github.com/duke-git/lancet/v2/validator" +) + +func main() { + data := []byte("你好") + + // check utf8 first + if utf8.Valid(data) { + fmt.Println("data encoding is utf-8") + }else if(validator.IsGBK(data)) { + fmt.Println("data encoding is GBK") + } + fmt.Println("data encoding is unknown") +} +``` diff --git a/docs/validator_zh-CN.md b/docs/validator_zh-CN.md index b2b0862..909999d 100644 --- a/docs/validator_zh-CN.md +++ b/docs/validator_zh-CN.md @@ -47,6 +47,7 @@ import ( - [IsUrl](#IsUrl) - [IsWeakPassword](#IsWeakPassword) - [IsZeroValue](#IsZeroValue) +- [IsGBK](#IsGBK) @@ -820,3 +821,31 @@ func main() { } ``` +### IsGBK +检查数据编码是否为gbk(汉字内部代码扩展规范)。该函数的实现取决于双字节是否在gbk的编码范围内,而utf-8编码格式的每个字节都在gbk编码范围内。因此,应该首先调用utf8.valid检查它是否是utf-8编码,然后调用IsGBK检查gbk编码。如示例所示。
+ +函数签名: + +```go +func IsGBK(data []byte) bool +``` +例子: + +```go +import ( + "fmt" + "github.com/duke-git/lancet/v2/validator" +) + +func main() { + data := []byte("你好") + + // 先检查utf8编码 + if utf8.Valid(data) { + fmt.Println("data encoding is utf-8") + }else if(validator.IsGBK(data)) { + fmt.Println("data encoding is GBK") + } + fmt.Println("data encoding is unknown") +} +``` \ No newline at end of file diff --git a/validator/validator.go b/validator/validator.go index 8fb463d..84afbb8 100644 --- a/validator/validator.go +++ b/validator/validator.go @@ -275,7 +275,7 @@ func IsZeroValue(value any) bool { return reflect.DeepEqual(rv.Interface(), reflect.Zero(rv.Type()).Interface()) } -// IsGBK check if data encoinge is gbk +// IsGBK check if data encoding 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,