From 66bd339e3a440d67bc75f41a190b8e6d46a70f60 Mon Sep 17 00:00:00 2001 From: dudaodong Date: Mon, 17 Apr 2023 16:35:26 +0800 Subject: [PATCH] doc: add example for strutil new functions --- strutil/string_example_test.go | 79 ++++++++++++++++++++++++++++++++++ strutil/string_test.go | 11 ++--- 2 files changed, 85 insertions(+), 5 deletions(-) diff --git a/strutil/string_example_test.go b/strutil/string_example_test.go index d0edece..d295eff 100644 --- a/strutil/string_example_test.go +++ b/strutil/string_example_test.go @@ -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 +} diff --git a/strutil/string_test.go b/strutil/string_test.go index e151f81..46202d0 100644 --- a/strutil/string_test.go +++ b/strutil/string_test.go @@ -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)