mirror of
https://github.com/duke-git/lancet.git
synced 2026-02-23 13:52:26 +08:00
doc: add example for strutil new functions
This commit is contained in:
@@ -2,6 +2,7 @@ package strutil
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"reflect"
|
||||||
)
|
)
|
||||||
|
|
||||||
func ExampleAfter() {
|
func ExampleAfter() {
|
||||||
@@ -449,3 +450,81 @@ func ExampleRemoveNonPrintable() {
|
|||||||
// hello world
|
// 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
|
||||||
|
}
|
||||||
|
|||||||
@@ -1,9 +1,10 @@
|
|||||||
package strutil
|
package strutil
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/duke-git/lancet/v2/internal"
|
|
||||||
"reflect"
|
"reflect"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"github.com/duke-git/lancet/v2/internal"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestCamelCase(t *testing.T) {
|
func TestCamelCase(t *testing.T) {
|
||||||
@@ -350,15 +351,15 @@ func TestRemoveNonPrintable(t *testing.T) {
|
|||||||
assert.Equal("你好😄", RemoveNonPrintable("你好😄"))
|
assert.Equal("你好😄", RemoveNonPrintable("你好😄"))
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestString2Bytes(t *testing.T) {
|
func TestStringToBytes(t *testing.T) {
|
||||||
assert := internal.NewAssert(t, "TestString2Bytes")
|
assert := internal.NewAssert(t, "TestStringToBytes")
|
||||||
str := "abc"
|
str := "abc"
|
||||||
bytes := StringToBytes(str)
|
bytes := StringToBytes(str)
|
||||||
assert.Equal(reflect.DeepEqual(bytes, []byte{'a', 'b', 'c'}), true)
|
assert.Equal(reflect.DeepEqual(bytes, []byte{'a', 'b', 'c'}), true)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestBytes2String(t *testing.T) {
|
func TestBytesToString(t *testing.T) {
|
||||||
assert := internal.NewAssert(t, "TestBytes2String")
|
assert := internal.NewAssert(t, "TestBytesToString")
|
||||||
bytes := []byte{'a', 'b', 'c'}
|
bytes := []byte{'a', 'b', 'c'}
|
||||||
str := BytesToString(bytes)
|
str := BytesToString(bytes)
|
||||||
assert.Equal(str == "abc", true)
|
assert.Equal(str == "abc", true)
|
||||||
|
|||||||
Reference in New Issue
Block a user