Skip to content

Commit

Permalink
style:rename gbk to utf8 functions to comform to CamelCase
Browse files Browse the repository at this point in the history
  • Loading branch information
R22627 authored and R22627 committed Nov 16, 2023
1 parent 32de451 commit 2fd7181
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 18 deletions.
11 changes: 5 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,9 @@
</div>

# Synopsis
A modern, comprehensive and efficient utility library of Go.
A modern, comprehensive and efficient utility library of Golang.

# Features

- Comprehensive, efficient and reusable.
- Numerous util functions, support string, slice, map, datetime, crypto...
- Only depend on the go standard and golang.org/x library.
Expand All @@ -25,8 +24,8 @@ A modern, comprehensive and efficient utility library of Go.
Some utility functions can be used to handle conversion of different character encoding, such as gbk to utf8.
```go
gbk := []byte{0xC4, 0xE3, 0xBA, 0xC3} // 你好 in gbk
GBKToUTF8(gbk) // 你好 in utf8
UTF8ToGBK(utf8) // [196 227 186 195]
GbkToUtf8(gbk) // 你好 in utf8
Utf8ToGbk(utf8) // [196 227 186 195]
```

Some converting function to json.
Expand Down Expand Up @@ -334,8 +333,8 @@ CreateFile()
ClearFile()

// Gets file info.
FileMD5()
FileMD5Reader()
FileMd5()
FileMd5Reader()
FileSize()
FileSizeFile()

Expand Down
20 changes: 10 additions & 10 deletions encoding/encode.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,26 +8,26 @@ import (
"golang.org/x/text/transform"
)

// GBKToUTF8 transforms GBK to UTF8.
func GBKToUTF8(s []byte) ([]byte, error) {
// GbkToUtf8 transforms GBK to UTF8.
func GbkToUtf8(s []byte) ([]byte, error) {
reader := transform.NewReader(bytes.NewReader(s), simplifiedchinese.GBK.NewDecoder())
return io.ReadAll(reader)
}

// GBKToUTF8Str transforms GBK to UTF8 string.
func GBKToUTF8Str(s string) string {
dst, _ := GBKToUTF8([]byte(s))
// GbkToUtf8Str transforms GBK to UTF8 string.
func GbkToUtf8Str(s string) string {
dst, _ := GbkToUtf8([]byte(s))
return string(dst)
}

// UTF82GBK transforms utf8 to gbk.
func UTF8ToGBK(b []byte) ([]byte, error) {
// Utf8ToGbk transforms utf8 to gbk.
func Utf8ToGbk(b []byte) ([]byte, error) {
reader := transform.NewReader(bytes.NewReader(b), simplifiedchinese.GBK.NewEncoder())
return io.ReadAll(reader)
}

// UTF82GBKStr transforms utf8 to gbk string.
func UTF8ToGBKStr(s string) string {
dst, _ := UTF8ToGBK([]byte(s))
// Utf8ToGbkStr transforms utf8 to gbk string.
func Utf8ToGbkStr(s string) string {
dst, _ := Utf8ToGbk([]byte(s))
return string(dst)
}
4 changes: 2 additions & 2 deletions encoding/encode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ import (

func TestGBKToUTF8Str(t *testing.T) {
assert := internal.NewAssert(t, "GBKToUTF8Str")
assert.Equal("中文", GBKToUTF8Str(string([]byte{0xD6, 0xD0, 0xCE, 0xC4})))
assert.Equal("中文", GbkToUtf8Str(string([]byte{0xD6, 0xD0, 0xCE, 0xC4})))
}

func TestUTF8ToGBKStr(t *testing.T) {
assert := internal.NewAssert(t, "UTF8ToGBKStr")
assert.Equal(string([]byte{0xD6, 0xD0, 0xCE, 0xC4}), UTF8ToGBKStr("中文"))
assert.Equal(string([]byte{0xD6, 0xD0, 0xCE, 0xC4}), Utf8ToGbkStr("中文"))
}

0 comments on commit 2fd7181

Please sign in to comment.