From b4e6abc4607dcdfcd040e841891321707280f9c1 Mon Sep 17 00:00:00 2001 From: huangqian Date: Sat, 11 Dec 2021 23:11:17 +0800 Subject: [PATCH 1/3] Adjust the function sequence --- util/gvalid/gvalid_z_example_test.go | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/util/gvalid/gvalid_z_example_test.go b/util/gvalid/gvalid_z_example_test.go index 0c56c37f9b9..e438ce710d4 100644 --- a/util/gvalid/gvalid_z_example_test.go +++ b/util/gvalid/gvalid_z_example_test.go @@ -189,6 +189,16 @@ func ExampleValidator_Data() { // The Password2 value `gofra` is not a valid password format } +func ExampleValidator_Data_Value() { + err := g.Validator().Rules("min:18"). + Messages("未成年人不允许注册哟"). + Data(16).Run(gctx.New()) + fmt.Println(err.String()) + + // Output: + // 未成年人不允许注册哟 +} + func ExampleValidator_Data_Map1() { params := map[string]interface{}{ "passport": "", @@ -480,16 +490,6 @@ func ExampleValidator_RuleFuncMap() { // Value Length Error!; Pass is not Same! } -func ExampleValidator_Data_Value() { - err := g.Validator().Rules("min:18"). - Messages("未成年人不允许注册哟"). - Data(16).Run(gctx.New()) - fmt.Println(err.String()) - - // Output: - // 未成年人不允许注册哟 -} - func ExampleValidator_RegisterRule() { type User struct { Id int From e28be8d1149e194abbd214aadc570d6c16d7962c Mon Sep 17 00:00:00 2001 From: huangqian Date: Tue, 14 Dec 2021 23:54:15 +0800 Subject: [PATCH 2/3] reorder the sample function --- text/gstr/gstr_z_example_test.go | 1031 ++++++++++++++++-------------- 1 file changed, 535 insertions(+), 496 deletions(-) diff --git a/text/gstr/gstr_z_example_test.go b/text/gstr/gstr_z_example_test.go index c21805713bc..465997ce4f3 100644 --- a/text/gstr/gstr_z_example_test.go +++ b/text/gstr/gstr_z_example_test.go @@ -12,139 +12,144 @@ import ( "github.com/gogf/gf/v2/text/gstr" ) -func ExampleAddSlashes() { +func ExampleCount() { var ( - str = `'aa'"bb"cc\r\n\d\t` - result = gstr.AddSlashes(str) + str = `goframe is very, very easy to use` + substr1 = "goframe" + substr2 = "very" + result1 = gstr.Count(str, substr1) + result2 = gstr.Count(str, substr2) ) - - fmt.Println(result) + fmt.Println(result1) + fmt.Println(result2) // Output: - // \'aa\'\"bb\"cc\\r\\n\\d\\t + // 1 + // 2 } -func ExampleCaseCamel() { +func ExampleCountI() { var ( - str = `hello world` - result = gstr.CaseCamel(str) + str = `goframe is very, very easy to use` + substr1 = "GOFRAME" + substr2 = "VERY" + result1 = gstr.CountI(str, substr1) + result2 = gstr.CountI(str, substr2) ) - fmt.Println(result) + fmt.Println(result1) + fmt.Println(result2) // Output: - // HelloWorld + // 1 + // 2 } -func ExampleCaseCamelLower() { +func ExampleToLower() { var ( - str = `hello world` - result = gstr.CaseCamelLower(str) + s = `GOFRAME` + result = gstr.ToLower(s) ) fmt.Println(result) // Output: - // helloWorld + // goframe } -func ExampleCaseDelimited() { +func ExampleToUpper() { var ( - str = `hello world` - del = byte('-') - result = gstr.CaseDelimited(str, del) + s = `goframe` + result = gstr.ToUpper(s) ) fmt.Println(result) // Output: - // hello-world + // GOFRAME } -func ExampleCaseDelimitedScreaming() { - { - var ( - str = `hello world` - del = byte('-') - result = gstr.CaseDelimitedScreaming(str, del, true) - ) - fmt.Println(result) - } - { - var ( - str = `hello world` - del = byte('-') - result = gstr.CaseDelimitedScreaming(str, del, false) - ) - fmt.Println(result) - } +func ExampleUcFirst() { + var ( + s = `hello` + result = gstr.UcFirst(s) + ) + fmt.Println(result) // Output: - // HELLO-WORLD - // hello-world + // Hello } -func ExampleCaseKebab() { +func ExampleLcFirst() { var ( - str = `hello world` - result = gstr.CaseKebab(str) + str = `Goframe` + result = gstr.LcFirst(str) ) fmt.Println(result) // Output: - // hello-world + // goframe } -func ExampleCaseKebabScreaming() { +func ExampleUcWords() { var ( str = `hello world` - result = gstr.CaseKebabScreaming(str) + result = gstr.UcWords(str) ) fmt.Println(result) // Output: - // HELLO-WORLD + // Hello World } -func ExampleCaseSnake() { - var ( - str = `hello world` - result = gstr.CaseSnake(str) - ) - fmt.Println(result) +func ExampleIsLetterLower() { + fmt.Println(gstr.IsLetterLower('a')) + fmt.Println(gstr.IsLetterLower('A')) // Output: - // hello_world + // true + // false } -func ExampleCaseSnakeFirstUpper() { - var ( - str = `RGBCodeMd5` - result = gstr.CaseSnakeFirstUpper(str) - ) - fmt.Println(result) +func ExampleIsLetterUpper() { + fmt.Println(gstr.IsLetterUpper('A')) + fmt.Println(gstr.IsLetterUpper('a')) // Output: - // rgb_code_md5 + // true + // false } -func ExampleCaseSnakeScreaming() { +func ExampleIsNumeric() { + fmt.Println(gstr.IsNumeric("88")) + fmt.Println(gstr.IsNumeric("3.1415926")) + fmt.Println(gstr.IsNumeric("abc")) + // Output: + // true + // true + // false +} + +func ExampleReverse() { var ( - str = `hello world` - result = gstr.CaseSnakeScreaming(str) + str = `123456` + result = gstr.Reverse(str) ) fmt.Println(result) // Output: - // HELLO_WORLD + // 654321 } -func ExampleChr() { +func ExampleNumberFormat() { var ( - ascii = 65 // A - result = gstr.Chr(ascii) + number float64 = 123456 + decimals = 2 + decPoint = "." + thousandsSep = "," + result = gstr.NumberFormat(number, decimals, decPoint, thousandsSep) ) fmt.Println(result) // Output: - // A + // 123,456.00 } func ExampleChunkSplit() { @@ -171,156 +176,185 @@ func ExampleCompare() { // 1 } -func ExampleCompareVersion() { - fmt.Println(gstr.CompareVersion("v2.11.9", "v2.10.8")) - fmt.Println(gstr.CompareVersion("1.10.8", "1.19.7")) - fmt.Println(gstr.CompareVersion("2.8.beta", "2.8")) +func ExampleEqual() { + fmt.Println(gstr.Equal(`A`, `a`)) + fmt.Println(gstr.Equal(`A`, `A`)) + fmt.Println(gstr.Equal(`A`, `B`)) // Output: - // 1 - // -1 - // 0 + // true + // true + // false } -func ExampleCompareVersionGo() { - fmt.Println(gstr.CompareVersionGo("v2.11.9", "v2.10.8")) - fmt.Println(gstr.CompareVersionGo("v4.20.1", "v4.20.1+incompatible")) - fmt.Println(gstr.CompareVersionGo( - "v0.0.2-20180626092158-b2ccc119800e", - "v1.0.1-20190626092158-b2ccc519800e", - )) +func ExampleFields() { + var ( + str = `Hello World` + result = gstr.Fields(str) + ) + fmt.Printf(`%#v`, result) // Output: - // 1 - // 1 - // -1 + // []string{"Hello", "World"} } -func ExampleContains() { - { - var ( - str = `Hello World` - substr = `Hello` - result = gstr.Contains(str, substr) - ) - fmt.Println(result) - } - { - var ( - str = `Hello World` - substr = `hello` - result = gstr.Contains(str, substr) - ) - fmt.Println(result) - } +func ExampleHasPrefix() { + var ( + s = `Hello World` + prefix = "Hello" + result = gstr.HasPrefix(s, prefix) + ) + fmt.Println(result) // Output: // true - // false } -func ExampleContainsAny() { +func ExampleHasSuffix() { + var ( + s = `my best love is goframe` + prefix = "goframe" + result = gstr.HasSuffix(s, prefix) + ) + fmt.Println(result) + + // Output: + // true +} + +func ExampleCountWords() { + var ( + str = `goframe is very, very easy to use!` + result = gstr.CountWords(str) + ) + fmt.Printf(`%#v`, result) + + // Output: + // map[string]int{"easy":1, "goframe":1, "is":1, "to":1, "use!":1, "very":1, "very,":1} +} + +func ExampleCountChars() { + var ( + str = `goframe` + result = gstr.CountChars(str) + ) + fmt.Println(result) + + // May Output: + // map[a:1 e:1 f:1 g:1 m:1 o:1 r:1] +} + +func ExampleWordWrap() { { var ( - s = `goframe` - chars = "g" - result = gstr.ContainsAny(s, chars) + str = `A very long woooooooooooooooooord. and something` + width = 8 + br = "\n" + result = gstr.WordWrap(str, width, br) ) fmt.Println(result) } { var ( - s = `goframe` - chars = "G" - result = gstr.ContainsAny(s, chars) + str = `The quick brown fox jumped over the lazy dog.` + width = 20 + br = "
\n" + result = gstr.WordWrap(str, width, br) ) - fmt.Println(result) + fmt.Printf("%v", result) } // Output: - // true - // false + // A very + // long + // woooooooooooooooooord. + // and + // something + // The quick brown fox
+ // jumped over the lazy
+ // dog. } -func ExampleContainsI() { +func ExampleLenRune() { var ( - str = `Hello World` - substr = "hello" - result1 = gstr.Contains(str, substr) - result2 = gstr.ContainsI(str, substr) + str = `GoFrame框架` + result = gstr.LenRune(str) ) - fmt.Println(result1) - fmt.Println(result2) + fmt.Println(result) // Output: - // false - // true + // 9 } -func ExampleCount() { +func ExampleRepeat() { var ( - str = `goframe is very, very easy to use` - substr1 = "goframe" - substr2 = "very" - result1 = gstr.Count(str, substr1) - result2 = gstr.Count(str, substr2) + input = `goframe ` + multiplier = 3 + result = gstr.Repeat(input, multiplier) ) - fmt.Println(result1) - fmt.Println(result2) + fmt.Println(result) // Output: - // 1 - // 2 + // goframe goframe goframe } -func ExampleCountChars() { +func ExampleShuffle() { var ( - str = `goframe` - result = gstr.CountChars(str) + str = `123456` + result = gstr.Shuffle(str) ) fmt.Println(result) // May Output: - // map[a:1 e:1 f:1 g:1 m:1 o:1 r:1] - + // 563214 } -func ExampleCountI() { +func ExampleSplit() { var ( - str = `goframe is very, very easy to use` - substr1 = "GOFRAME" - substr2 = "VERY" - result1 = gstr.CountI(str, substr1) - result2 = gstr.CountI(str, substr2) + str = `a|b|c|d` + delimiter = `|` + result = gstr.Split(str, delimiter) ) - fmt.Println(result1) - fmt.Println(result2) + fmt.Printf(`%#v`, result) // Output: - // 1 - // 2 + // []string{"a", "b", "c", "d"} } -func ExampleCountWords() { +func ExampleSplitAndTrim() { var ( - str = `goframe is very, very easy to use!` - result = gstr.CountWords(str) + str = `a|b|||||c|d` + delimiter = `|` + result = gstr.SplitAndTrim(str, delimiter) ) fmt.Printf(`%#v`, result) // Output: - // map[string]int{"easy":1, "goframe":1, "is":1, "to":1, "use!":1, "very":1, "very,":1} + // []string{"a", "b", "c", "d"} } -func ExampleEqual() { - fmt.Println(gstr.Equal(`A`, `a`)) - fmt.Println(gstr.Equal(`A`, `A`)) - fmt.Println(gstr.Equal(`A`, `B`)) +func ExampleJoin() { + var ( + array = []string{"goframe", "is", "very", "easy", "to", "use"} + sep = ` ` + result = gstr.Join(array, sep) + ) + fmt.Println(result) // Output: - // true - // true - // false + // goframe is very easy to use +} + +func ExampleJoinAny() { + var ( + sep = `,` + arr2 = []int{99, 73, 85, 66} + result = gstr.JoinAny(arr2, sep) + ) + fmt.Println(result) + + // Output: + // 99,73,85,66 } func ExampleExplode() { @@ -335,39 +369,40 @@ func ExampleExplode() { // []string{"Hello", "World"} } -func ExampleFields() { +func ExampleImplode() { var ( - str = `Hello World` - result = gstr.Fields(str) + pieces = []string{"goframe", "is", "very", "easy", "to", "use"} + glue = " " + result = gstr.Implode(glue, pieces) ) - fmt.Printf(`%#v`, result) + fmt.Println(result) // Output: - // []string{"Hello", "World"} + // goframe is very easy to use } -func ExampleHasPrefix() { +func ExampleChr() { var ( - s = `Hello World` - prefix = "Hello" - result = gstr.HasPrefix(s, prefix) + ascii = 65 // A + result = gstr.Chr(ascii) ) fmt.Println(result) // Output: - // true + // A } -func ExampleHasSuffix() { +// '103' is the 'g' in ASCII +func ExampleOrd() { var ( - s = `my best love is goframe` - prefix = "goframe" - result = gstr.HasSuffix(s, prefix) + str = `goframe` + result = gstr.Ord(str) ) + fmt.Println(result) // Output: - // true + // 103 } func ExampleHideStr() { @@ -383,16 +418,79 @@ func ExampleHideStr() { // 138****8000 } -func ExampleImplode() { +func ExampleNl2Br() { var ( - pieces = []string{"goframe", "is", "very", "easy", "to", "use"} - glue = " " - result = gstr.Implode(glue, pieces) + str = `goframe +is +very +easy +to +use` + result = gstr.Nl2Br(str) ) + fmt.Println(result) // Output: - // goframe is very easy to use + // goframe
is
very
easy
to
use +} + +func ExampleAddSlashes() { + var ( + str = `'aa'"bb"cc\r\n\d\t` + result = gstr.AddSlashes(str) + ) + + fmt.Println(result) + + // Output: + // \'aa\'\"bb\"cc\\r\\n\\d\\t +} + +func ExampleStripSlashes() { + var ( + str = `C:\\windows\\GoFrame\\test` + result = gstr.StripSlashes(str) + ) + fmt.Println(result) + + // Output: + // C:\windows\GoFrame\test +} + +func ExampleQuoteMeta() { + { + var ( + str = `.\+?[^]()` + result = gstr.QuoteMeta(str) + ) + fmt.Println(result) + } + { + var ( + str = `https://goframe.org/pages/viewpage.action?pageId=1114327` + result = gstr.QuoteMeta(str) + ) + fmt.Println(result) + } + + // Output: + // \.\\\+\?\[\^\]\(\) + // https://goframe\.org/pages/viewpage\.action\?pageId=1114327 + +} + +// array +func ExampleSearchArray() { + var ( + array = []string{"goframe", "is", "very", "nice"} + str = `goframe` + result = gstr.SearchArray(array, str) + ) + fmt.Println(result) + + // Output: + // 0 } func ExampleInArray() { @@ -407,138 +505,187 @@ func ExampleInArray() { // true } -func ExampleIsLetterLower() { - fmt.Println(gstr.IsLetterLower('a')) - fmt.Println(gstr.IsLetterLower('A')) - +func ExamplePrefixArray() { // Output: - // true - // false + // test } -func ExampleIsLetterUpper() { - fmt.Println(gstr.IsLetterUpper('A')) - fmt.Println(gstr.IsLetterUpper('a')) +// case +func ExampleCaseCamel() { + var ( + str = `hello world` + result = gstr.CaseCamel(str) + ) + fmt.Println(result) // Output: - // true - // false + // HelloWorld } -func ExampleIsNumeric() { - fmt.Println(gstr.IsNumeric("88")) - fmt.Println(gstr.IsNumeric("3.1415926")) - fmt.Println(gstr.IsNumeric("abc")) +func ExampleCaseCamelLower() { + var ( + str = `hello world` + result = gstr.CaseCamelLower(str) + ) + fmt.Println(result) + // Output: - // true - // true - // false + // helloWorld } -func ExampleIsSubDomain() { +func ExampleCaseSnake() { var ( - subDomain = `s.goframe.org` - mainDomain = `goframe.org` - result = gstr.IsSubDomain(subDomain, mainDomain) + str = `hello world` + result = gstr.CaseSnake(str) ) fmt.Println(result) // Output: - // true + // hello_world } -func ExampleJoin() { +func ExampleCaseSnakeScreaming() { var ( - array = []string{"goframe", "is", "very", "easy", "to", "use"} - sep = ` ` - result = gstr.Join(array, sep) + str = `hello world` + result = gstr.CaseSnakeScreaming(str) ) fmt.Println(result) // Output: - // goframe is very easy to use + // HELLO_WORLD } -func ExampleJoinAny() { +func ExampleCaseSnakeFirstUpper() { var ( - sep = `,` - arr2 = []int{99, 73, 85, 66} - result = gstr.JoinAny(arr2, sep) + str = `RGBCodeMd5` + result = gstr.CaseSnakeFirstUpper(str) ) fmt.Println(result) // Output: - // 99,73,85,66 + // rgb_code_md5 } -func ExampleLcFirst() { +func ExampleCaseKebab() { var ( - str = `Goframe` - result = gstr.LcFirst(str) + str = `hello world` + result = gstr.CaseKebab(str) ) fmt.Println(result) // Output: - // goframe + // hello-world } -func ExampleLenRune() { +func ExampleCaseKebabScreaming() { var ( - str = `GoFrame框架` - result = gstr.LenRune(str) + str = `hello world` + result = gstr.CaseKebabScreaming(str) ) fmt.Println(result) // Output: - // 9 + // HELLO-WORLD } -func ExampleLevenshtein() { +func ExampleCaseDelimited() { var ( - str1 = "Hello World" - str2 = "hallo World" - costIns = 1 - costRep = 1 - costDel = 1 - result = gstr.Levenshtein(str1, str2, costIns, costRep, costDel) + str = `hello world` + del = byte('-') + result = gstr.CaseDelimited(str, del) ) fmt.Println(result) // Output: - // 2 + // hello-world } -func ExampleNl2Br() { - var ( - str = `goframe -is -very -easy -to -use` - result = gstr.Nl2Br(str) - ) +func ExampleCaseDelimitedScreaming() { + { + var ( + str = `hello world` + del = byte('-') + result = gstr.CaseDelimitedScreaming(str, del, true) + ) + fmt.Println(result) + } + { + var ( + str = `hello world` + del = byte('-') + result = gstr.CaseDelimitedScreaming(str, del, false) + ) + fmt.Println(result) + } - fmt.Println(result) + // Output: + // HELLO-WORLD + // hello-world +} + +// contain +func ExampleContains() { + { + var ( + str = `Hello World` + substr = `Hello` + result = gstr.Contains(str, substr) + ) + fmt.Println(result) + } + { + var ( + str = `Hello World` + substr = `hello` + result = gstr.Contains(str, substr) + ) + fmt.Println(result) + } // Output: - // goframe
is
very
easy
to
use + // true + // false } -func ExampleNumberFormat() { +func ExampleContainsI() { var ( - number float64 = 123456 - decimals = 2 - decPoint = "." - thousandsSep = "," - result = gstr.NumberFormat(number, decimals, decPoint, thousandsSep) + str = `Hello World` + substr = "hello" + result1 = gstr.Contains(str, substr) + result2 = gstr.ContainsI(str, substr) ) - fmt.Println(result) + fmt.Println(result1) + fmt.Println(result2) // Output: - // 123,456.00 + // false + // true +} + +func ExampleContainsAny() { + { + var ( + s = `goframe` + chars = "g" + result = gstr.ContainsAny(s, chars) + ) + fmt.Println(result) + } + { + var ( + s = `goframe` + chars = "G" + result = gstr.ContainsAny(s, chars) + ) + fmt.Println(result) + } + + // Output: + // true + // false } +// convert func ExampleOctStr() { var ( str = `\346\200\241` @@ -550,19 +697,36 @@ func ExampleOctStr() { // 怡 } -// '103' is the 'g' in ASCII -func ExampleOrd() { +// domain +func ExampleIsSubDomain() { var ( - str = `goframe` - result = gstr.Ord(str) + subDomain = `s.goframe.org` + mainDomain = `goframe.org` + result = gstr.IsSubDomain(subDomain, mainDomain) ) + fmt.Println(result) + + // Output: + // true +} +// levenshtein +func ExampleLevenshtein() { + var ( + str1 = "Hello World" + str2 = "hallo World" + costIns = 1 + costRep = 1 + costDel = 1 + result = gstr.Levenshtein(str1, str2, costIns, costRep, costDel) + ) fmt.Println(result) // Output: - // 103 + // 2 } +// parse func ExampleParse() { { var ( @@ -613,6 +777,7 @@ func ExampleParse() { // map[a___[b:c] } +// pos func ExamplePos() { var ( haystack = `Hello World` @@ -625,6 +790,21 @@ func ExamplePos() { // 6 } +func ExamplePosRune() { + var ( + haystack = `GoFrame是一款模块化、高性能、企业级的Go基础开发框架` + needle = `Go` + posI = gstr.PosRune(haystack, needle) + posR = gstr.PosRRune(haystack, needle) + ) + fmt.Println(posI) + fmt.Println(posR) + + // Output: + // 0 + // 22 +} + func ExamplePosI() { var ( haystack = `goframe is very, very easy to use` @@ -680,27 +860,12 @@ func ExamplePosR() { // 17 } -func ExamplePosRI() { - var ( - haystack = `goframe is very, very easy to use` - needle = `VERY` - posI = gstr.PosI(haystack, needle) - posR = gstr.PosRI(haystack, needle) - ) - fmt.Println(posI) - fmt.Println(posR) - - // Output: - // 11 - // 17 -} - -func ExamplePosRIRune() { +func ExamplePosRRune() { var ( haystack = `GoFrame是一款模块化、高性能、企业级的Go基础开发框架` - needle = `GO` + needle = `Go` posI = gstr.PosIRune(haystack, needle) - posR = gstr.PosRIRune(haystack, needle) + posR = gstr.PosRRune(haystack, needle) ) fmt.Println(posI) fmt.Println(posR) @@ -710,27 +875,27 @@ func ExamplePosRIRune() { // 22 } -func ExamplePosRRune() { +func ExamplePosRI() { var ( - haystack = `GoFrame是一款模块化、高性能、企业级的Go基础开发框架` - needle = `Go` - posI = gstr.PosIRune(haystack, needle) - posR = gstr.PosRRune(haystack, needle) + haystack = `goframe is very, very easy to use` + needle = `VERY` + posI = gstr.PosI(haystack, needle) + posR = gstr.PosRI(haystack, needle) ) fmt.Println(posI) fmt.Println(posR) // Output: - // 0 - // 22 + // 11 + // 17 } -func ExamplePosRune() { +func ExamplePosRIRune() { var ( haystack = `GoFrame是一款模块化、高性能、企业级的Go基础开发框架` - needle = `Go` - posI = gstr.PosRune(haystack, needle) - posR = gstr.PosRRune(haystack, needle) + needle = `GO` + posI = gstr.PosIRune(haystack, needle) + posR = gstr.PosRIRune(haystack, needle) ) fmt.Println(posI) fmt.Println(posR) @@ -740,46 +905,26 @@ func ExamplePosRune() { // 22 } -func ExampleQuoteMeta() { - { - var ( - str = `.\+?[^]()` - result = gstr.QuoteMeta(str) - ) - fmt.Println(result) - } - { - var ( - str = `https://goframe.org/pages/viewpage.action?pageId=1114327` - result = gstr.QuoteMeta(str) - ) - fmt.Println(result) - } - - // Output: - // \.\\\+\?\[\^\]\(\) - // https://goframe\.org/pages/viewpage\.action\?pageId=1114327 - -} - -func ExampleRepeat() { +// replace +func ExampleReplace() { var ( - input = `goframe ` - multiplier = 3 - result = gstr.Repeat(input, multiplier) + origin = `golang is very nice!` + search = `golang` + replace = `goframe` + result = gstr.Replace(origin, search, replace) ) fmt.Println(result) // Output: - // goframe goframe goframe + // goframe is very nice! } -func ExampleReplace() { +func ExampleReplaceI() { var ( origin = `golang is very nice!` - search = `golang` + search = `GOLANG` replace = `goframe` - result = gstr.Replace(origin, search, replace) + result = gstr.ReplaceI(origin, search, replace) ) fmt.Println(result) @@ -810,6 +955,19 @@ func ExampleReplaceByArray() { // goframe is very nice } +func ExampleReplaceIByArray() { + var ( + origin = `golang is very Good` + array = []string{"Golang", "goframe", "GOOD", "nice"} + result = gstr.ReplaceIByArray(origin, array) + ) + + fmt.Println(result) + + // Output: + // goframe is very nice +} + func ExampleReplaceByMap() { { var ( @@ -838,32 +996,6 @@ func ExampleReplaceByMap() { // goframe is very nice } -func ExampleReplaceI() { - var ( - origin = `golang is very nice!` - search = `GOLANG` - replace = `goframe` - result = gstr.ReplaceI(origin, search, replace) - ) - fmt.Println(result) - - // Output: - // goframe is very nice! -} - -func ExampleReplaceIByArray() { - var ( - origin = `golang is very Good` - array = []string{"Golang", "goframe", "GOOD", "nice"} - result = gstr.ReplaceIByArray(origin, array) - ) - - fmt.Println(result) - - // Output: - // goframe is very nice -} - func ExampleReplaceIByMap() { var ( origin = `golang is very nice` @@ -878,40 +1010,7 @@ func ExampleReplaceIByMap() { // goframe is very nice } -func ExampleReverse() { - var ( - str = `123456` - result = gstr.Reverse(str) - ) - fmt.Println(result) - - // Output: - // 654321 -} - -func ExampleSearchArray() { - var ( - array = []string{"goframe", "is", "very", "nice"} - str = `goframe` - result = gstr.SearchArray(array, str) - ) - fmt.Println(result) - - // Output: - // 0 -} - -func ExampleShuffle() { - var ( - str = `123456` - result = gstr.Shuffle(str) - ) - fmt.Println(result) - - // May Output: - // 563214 -} - +// similartext func ExampleSimilarText() { var ( first = `AaBbCcDd` @@ -925,6 +1024,7 @@ func ExampleSimilarText() { // 2 } +// soundex func ExampleSoundex() { var ( str1 = `Hello` @@ -938,30 +1038,7 @@ func ExampleSoundex() { // H400 H400 } -func ExampleSplit() { - var ( - str = `a|b|c|d` - delimiter = `|` - result = gstr.Split(str, delimiter) - ) - fmt.Printf(`%#v`, result) - - // Output: - // []string{"a", "b", "c", "d"} -} - -func ExampleSplitAndTrim() { - var ( - str = `a|b|||||c|d` - delimiter = `|` - result = gstr.SplitAndTrim(str, delimiter) - ) - fmt.Printf(`%#v`, result) - - // Output: - // []string{"a", "b", "c", "d"} -} - +// str func ExampleStr() { var ( haystack = `xxx.jpg` @@ -986,32 +1063,6 @@ func ExampleStrEx() { // a=1&b=2 } -func ExampleStrLimit() { - var ( - str = `123456789` - length = 3 - suffix = `...` - result = gstr.StrLimit(str, length, suffix) - ) - fmt.Println(result) - - // Output: - // 123... -} - -func ExampleStrLimitRune() { - var ( - str = `GoFrame是一款模块化、高性能、企业级的Go基础开发框架。` - length = 17 - suffix = "..." - result = gstr.StrLimitRune(str, length, suffix) - ) - fmt.Println(result) - - // Output: - // GoFrame是一款模块化、高性能... -} - func ExampleStrTill() { var ( haystack = `https://goframe.org/index.html?test=123456` @@ -1036,17 +1087,7 @@ func ExampleStrTillEx() { // https://goframe.org/index.html } -func ExampleStripSlashes() { - var ( - str = `C:\\windows\\GoFrame\\test` - result = gstr.StripSlashes(str) - ) - fmt.Println(result) - - // Output: - // C:\windows\GoFrame\test -} - +// substr func ExampleSubStr() { var ( str = `1234567890` @@ -1073,28 +1114,53 @@ func ExampleSubStrRune() { // 高性能 } -func ExampleToLower() { +func ExampleStrLimit() { var ( - s = `GOFRAME` - result = gstr.ToLower(s) + str = `123456789` + length = 3 + suffix = `...` + result = gstr.StrLimit(str, length, suffix) ) fmt.Println(result) // Output: - // goframe + // 123... } -func ExampleToUpper() { +func ExampleStrLimitRune() { var ( - s = `goframe` - result = gstr.ToUpper(s) + str = `GoFrame是一款模块化、高性能、企业级的Go基础开发框架。` + length = 17 + suffix = "..." + result = gstr.StrLimitRune(str, length, suffix) ) fmt.Println(result) // Output: - // GOFRAME + // GoFrame是一款模块化、高性能... } +func ExampleSubStrFrom() { + // Output: + // test +} + +func ExampleSubStrFromEx() { + // Output: + // test +} + +func ExampleSubStrFromR() { + // Output: + // test +} + +func ExampleSubStrFromREx() { + // Output: + // test +} + +// trim func ExampleTrim() { var ( str = `*Hello World*` @@ -1107,16 +1173,17 @@ func ExampleTrim() { // Hello World } -func ExampleTrimAll() { +func ExampleTrimStr() { var ( - str = `*Hello World*` - characterMask = "*" - result = gstr.TrimAll(str, characterMask) + str = `Hello World` + cut = "World" + count = -1 + result = gstr.TrimStr(str, cut, count) ) fmt.Println(result) // Output: - // HelloWorld + // Hello } func ExampleTrimLeft() { @@ -1169,68 +1236,40 @@ func ExampleTrimRightStr() { // Hello World } -func ExampleTrimStr() { - var ( - str = `Hello World` - cut = "World" - count = -1 - result = gstr.TrimStr(str, cut, count) - ) - fmt.Println(result) - - // Output: - // Hello -} - -func ExampleUcFirst() { +func ExampleTrimAll() { var ( - s = `hello` - result = gstr.UcFirst(s) + str = `*Hello World*` + characterMask = "*" + result = gstr.TrimAll(str, characterMask) ) fmt.Println(result) // Output: - // Hello + // HelloWorld } -func ExampleUcWords() { - var ( - str = `hello world` - result = gstr.UcWords(str) - ) - fmt.Println(result) +// version +func ExampleCompareVersion() { + fmt.Println(gstr.CompareVersion("v2.11.9", "v2.10.8")) + fmt.Println(gstr.CompareVersion("1.10.8", "1.19.7")) + fmt.Println(gstr.CompareVersion("2.8.beta", "2.8")) // Output: - // Hello World + // 1 + // -1 + // 0 } -func ExampleWordWrap() { - { - var ( - str = `A very long woooooooooooooooooord. and something` - width = 8 - br = "\n" - result = gstr.WordWrap(str, width, br) - ) - fmt.Println(result) - } - { - var ( - str = `The quick brown fox jumped over the lazy dog.` - width = 20 - br = "
\n" - result = gstr.WordWrap(str, width, br) - ) - fmt.Printf("%v", result) - } +func ExampleCompareVersionGo() { + fmt.Println(gstr.CompareVersionGo("v2.11.9", "v2.10.8")) + fmt.Println(gstr.CompareVersionGo("v4.20.1", "v4.20.1+incompatible")) + fmt.Println(gstr.CompareVersionGo( + "v0.0.2-20180626092158-b2ccc119800e", + "v1.0.1-20190626092158-b2ccc519800e", + )) // Output: - // A very - // long - // woooooooooooooooooord. - // and - // something - // The quick brown fox
- // jumped over the lazy
- // dog. + // 1 + // 1 + // -1 } From ff2e9b568b14d3898cd8f7cbdc30be424807894d Mon Sep 17 00:00:00 2001 From: huangqian Date: Wed, 15 Dec 2021 21:15:34 +0800 Subject: [PATCH 3/3] Implemented gstr Example 1. PrefixArray 2.SubStrFrom 3.SubStrFromEx 4.SubStrFromR 5.SubStrFromREx --- text/gstr/gstr_z_example_test.go | 46 ++++++++++++++++++++++++++++---- 1 file changed, 41 insertions(+), 5 deletions(-) diff --git a/text/gstr/gstr_z_example_test.go b/text/gstr/gstr_z_example_test.go index 465997ce4f3..c3499dda54b 100644 --- a/text/gstr/gstr_z_example_test.go +++ b/text/gstr/gstr_z_example_test.go @@ -506,8 +506,16 @@ func ExampleInArray() { } func ExamplePrefixArray() { + var ( + strArray = []string{"tom", "lily", "john"} + ) + + gstr.PrefixArray(strArray, "classA_") + + fmt.Println(strArray) + // Output: - // test + // [classA_tom classA_lily classA_john] } // case @@ -1141,23 +1149,51 @@ func ExampleStrLimitRune() { } func ExampleSubStrFrom() { + var ( + str = "我爱GoFrameGood" + need = `爱` + ) + + fmt.Println(gstr.SubStrFrom(str, need)) + // Output: - // test + // 爱GoFrameGood } func ExampleSubStrFromEx() { + var ( + str = "我爱GoFrameGood" + need = `爱` + ) + + fmt.Println(gstr.SubStrFromEx(str, need)) + // Output: - // test + // GoFrameGood } func ExampleSubStrFromR() { + var ( + str = "我爱GoFrameGood" + need = `Go` + ) + + fmt.Println(gstr.SubStrFromR(str, need)) + // Output: - // test + // Good } func ExampleSubStrFromREx() { + var ( + str = "我爱GoFrameGood" + need = `Go` + ) + + fmt.Println(gstr.SubStrFromREx(str, need)) + // Output: - // test + // od } // trim