From be355a23f39cf37527c8c7f0e24261f45fe6f347 Mon Sep 17 00:00:00 2001 From: dudaodong Date: Fri, 9 Jun 2023 11:29:29 +0800 Subject: [PATCH] doc: add doc for Utf8ToGbk and GbkToUtf8 --- convertor/convertor.go | 4 +- docs/convertor.md | 85 +++++++++++++++++++++++++++++++++++++---- docs/convertor_zh-CN.md | 83 ++++++++++++++++++++++++++++++++++++---- 3 files changed, 154 insertions(+), 18 deletions(-) diff --git a/convertor/convertor.go b/convertor/convertor.go index e9e0b6f..320182e 100644 --- a/convertor/convertor.go +++ b/convertor/convertor.go @@ -379,7 +379,7 @@ func ToInterface(v reflect.Value) (value interface{}, ok bool) { } } -// Utf8ToGbk convert utf8 encoding to GBK encoding. +// Utf8ToGbk convert utf8 encoding data to GBK encoding data. // Play: todo func Utf8ToGbk(bs []byte) ([]byte, error) { r := transform.NewReader(bytes.NewReader(bs), simplifiedchinese.GBK.NewEncoder()) @@ -387,7 +387,7 @@ func Utf8ToGbk(bs []byte) ([]byte, error) { return b, err } -// GbkToUtf8 convert GBK encoding to utf8 encoding. +// GbkToUtf8 convert GBK encoding data to utf8 encoding data. // Play: todo func GbkToUtf8(bs []byte) ([]byte, error) { r := transform.NewReader(bytes.NewReader(bs), simplifiedchinese.GBK.NewDecoder()) diff --git a/docs/convertor.md b/docs/convertor.md index 394858a..739662a 100644 --- a/docs/convertor.md +++ b/docs/convertor.md @@ -41,6 +41,8 @@ import ( - [DeepClone](#DeepClone) - [CopyProperties](#CopyProperties) - [ToInterface](#ToInterface) +- [Utf8ToGbk](#Utf8ToGbk) +- [GbkToUtf8](#GbkToUtf8)
@@ -795,15 +797,82 @@ import ( func main() { val := reflect.ValueOf("abc") - iVal, ok := convertor.ToInterface(val) + iVal, ok := convertor.ToInterface(val) - fmt.Printf("%T\n", iVal) - fmt.Printf("%v\n", iVal) - fmt.Println(ok) + fmt.Printf("%T\n", iVal) + fmt.Printf("%v\n", iVal) + fmt.Println(ok) - // Output: - // string - // abc - // true + // Output: + // string + // abc + // true +} +``` + +### Utf8ToGbk + +

Converts utf8 encoding data to GBK encoding data.

+ +Signature: + +```go +func Utf8ToGbk(bs []byte) ([]byte, error) +``` + +Example: + +```go +package main + +import ( + "fmt" + "github.com/duke-git/lancet/v2/convertor" + "github.com/duke-git/lancet/v2/validator" +) + +func main() { + utf8Data := []byte("hello") + gbkData, _ := convertor.Utf8ToGbk(utf8Data) + + fmt.Println(utf8.Valid(utf8Data)) + fmt.Println(validator.IsGBK(gbkData)) + + // Output: + // true + // true +} +``` + +### GbkToUtf8 + +

Converts GBK encoding data to utf8 encoding data.

+ +Signature: + +```go +func GbkToUtf8(bs []byte) ([]byte, error) +``` + +Example: + +```go +package main + +import ( + "fmt" + "github.com/duke-git/lancet/v2/convertor" +) + +func main() { + gbkData, _ := convertor.Utf8ToGbk([]byte("hello")) + utf8Data, _ := convertor.GbkToUtf8(gbkData) + + fmt.Println(utf8.Valid(utf8Data)) + fmt.Println(string(utf8Data)) + + // Output: + // true + // hello } ``` \ No newline at end of file diff --git a/docs/convertor_zh-CN.md b/docs/convertor_zh-CN.md index aa6908f..9fe35d7 100644 --- a/docs/convertor_zh-CN.md +++ b/docs/convertor_zh-CN.md @@ -795,15 +795,82 @@ import ( func main() { val := reflect.ValueOf("abc") - iVal, ok := convertor.ToInterface(val) + iVal, ok := convertor.ToInterface(val) - fmt.Printf("%T\n", iVal) - fmt.Printf("%v\n", iVal) - fmt.Println(ok) + fmt.Printf("%T\n", iVal) + fmt.Printf("%v\n", iVal) + fmt.Println(ok) - // Output: - // string - // abc - // true + // Output: + // string + // abc + // true } ``` + +### Utf8ToGbk + +

utf8编码转GBK编码。

+ +函数签名: + +```go +func Utf8ToGbk(bs []byte) ([]byte, error) +``` + +示例: + +```go +package main + +import ( + "fmt" + "github.com/duke-git/lancet/v2/convertor" + "github.com/duke-git/lancet/v2/validator" +) + +func main() { + utf8Data := []byte("hello") + gbkData, _ := convertor.Utf8ToGbk(utf8Data) + + fmt.Println(utf8.Valid(utf8Data)) + fmt.Println(validator.IsGBK(gbkData)) + + // Output: + // true + // true +} +``` + +### GbkToUtf8 + +

GBK编码转utf8编码。

+ +函数签名: + +```go +func GbkToUtf8(bs []byte) ([]byte, error) +``` + +示例: + +```go +package main + +import ( + "fmt" + "github.com/duke-git/lancet/v2/convertor" +) + +func main() { + gbkData, _ := convertor.Utf8ToGbk([]byte("hello")) + utf8Data, _ := convertor.GbkToUtf8(gbkData) + + fmt.Println(utf8.Valid(utf8Data)) + fmt.Println(string(utf8Data)) + + // Output: + // true + // hello +} +``` \ No newline at end of file