1
0
mirror of https://github.com/duke-git/lancet.git synced 2026-02-04 12:52:28 +08:00

doc: add doc for IsGBK'

This commit is contained in:
dudaodong
2022-12-09 19:28:45 +08:00
parent 037d2729ce
commit 2725575d2f
3 changed files with 59 additions and 2 deletions

View File

@@ -47,6 +47,7 @@ import (
- [IsUrl](#IsUrl)
- [IsWeakPassword](#IsWeakPassword)
- [IsZeroValue](#IsZeroValue)
- [IsGBK](#IsGBK)
<div STYLE="page-break-after: always;"></div>
@@ -821,7 +822,34 @@ func main() {
```
### <span id="IsGBK">IsGBK</span>
<p>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.</p>
<b>Signature:</b>
```go
func IsGBK(data []byte) bool
```
<b>Example:</b>
```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")
}
```

View File

@@ -47,6 +47,7 @@ import (
- [IsUrl](#IsUrl)
- [IsWeakPassword](#IsWeakPassword)
- [IsZeroValue](#IsZeroValue)
- [IsGBK](#IsGBK)
<div STYLE="page-break-after: always;"></div>
@@ -820,3 +821,31 @@ func main() {
}
```
### <span id="IsGBK">IsGBK</span>
<p>检查数据编码是否为gbk汉字内部代码扩展规范。该函数的实现取决于双字节是否在gbk的编码范围内而utf-8编码格式的每个字节都在gbk编码范围内。因此应该首先调用utf8.valid检查它是否是utf-8编码然后调用IsGBK检查gbk编码。如示例所示。</p>
<b>函数签名:</b>
```go
func IsGBK(data []byte) bool
```
<b>例子:</b>
```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")
}
```

View File

@@ -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,