1
0
mirror of https://github.com/duke-git/lancet.git synced 2026-02-07 22:22:29 +08:00

doc: add example for strutil new functions

This commit is contained in:
dudaodong
2023-04-17 16:35:26 +08:00
parent 04abb7a3ea
commit 66bd339e3a
2 changed files with 85 additions and 5 deletions

View File

@@ -2,6 +2,7 @@ package strutil
import (
"fmt"
"reflect"
)
func ExampleAfter() {
@@ -449,3 +450,81 @@ func ExampleRemoveNonPrintable() {
// hello world
// 你好😄
}
func ExampleStringToBytes() {
result1 := StringToBytes("abc")
result2 := reflect.DeepEqual(result1, []byte{'a', 'b', 'c'})
fmt.Println(result1)
fmt.Println(result2)
// Output:
// [97 98 99]
// true
}
func ExampleBytesToString() {
bytes := []byte{'a', 'b', 'c'}
result := BytesToString(bytes)
fmt.Println(result)
// Output:
// abc
}
func ExampleIsBlank() {
result1 := IsBlank("")
result2 := IsBlank("\t\v\f\n")
result3 := IsBlank(" 中文")
fmt.Println(result1)
fmt.Println(result2)
fmt.Println(result3)
// Output:
// true
// true
// false
}
func ExampleHasPrefixAny() {
result1 := HasPrefixAny("foo bar", []string{"fo", "xyz", "hello"})
result2 := HasPrefixAny("foo bar", []string{"oom", "world"})
fmt.Println(result1)
fmt.Println(result2)
// Output:
// true
// false
}
func ExampleHasSuffixAny() {
result1 := HasSuffixAny("foo bar", []string{"bar", "xyz", "hello"})
result2 := HasSuffixAny("foo bar", []string{"oom", "world"})
fmt.Println(result1)
fmt.Println(result2)
// Output:
// true
// false
}
func ExampleIndexOffset() {
str := "foo bar hello world"
result1 := IndexOffset(str, "o", 5)
result2 := IndexOffset(str, "o", 0)
result3 := IndexOffset(str, "d", len(str)-1)
result4 := IndexOffset(str, "d", len(str))
result5 := IndexOffset(str, "f", -1)
fmt.Println(result1)
fmt.Println(result2)
fmt.Println(result3)
fmt.Println(result4)
fmt.Println(result5)
// Output:
// 12
// 1
// 18
// -1
// -1
}

View File

@@ -1,9 +1,10 @@
package strutil
import (
"github.com/duke-git/lancet/v2/internal"
"reflect"
"testing"
"github.com/duke-git/lancet/v2/internal"
)
func TestCamelCase(t *testing.T) {
@@ -350,15 +351,15 @@ func TestRemoveNonPrintable(t *testing.T) {
assert.Equal("你好😄", RemoveNonPrintable("你好😄"))
}
func TestString2Bytes(t *testing.T) {
assert := internal.NewAssert(t, "TestString2Bytes")
func TestStringToBytes(t *testing.T) {
assert := internal.NewAssert(t, "TestStringToBytes")
str := "abc"
bytes := StringToBytes(str)
assert.Equal(reflect.DeepEqual(bytes, []byte{'a', 'b', 'c'}), true)
}
func TestBytes2String(t *testing.T) {
assert := internal.NewAssert(t, "TestBytes2String")
func TestBytesToString(t *testing.T) {
assert := internal.NewAssert(t, "TestBytesToString")
bytes := []byte{'a', 'b', 'c'}
str := BytesToString(bytes)
assert.Equal(str == "abc", true)