improve example for package gstr

This commit is contained in:
John Guo 2021-11-18 22:13:12 +08:00
parent 7f33c101c8
commit 0c62c62a7a
5 changed files with 1255 additions and 2130 deletions

View File

@ -94,7 +94,7 @@ func CaseKebabScreaming(s string) string {
}
// CaseDelimited converts a string to snake.case.delimited.
func CaseDelimited(s string, del uint8) string {
func CaseDelimited(s string, del byte) string {
return CaseDelimitedScreaming(s, del, false)
}

View File

@ -7,8 +7,9 @@
package gstr
import (
"github.com/gogf/gf/v2/util/gconv"
"strings"
"github.com/gogf/gf/v2/util/gconv"
)
// CompareVersion compares `a` and `b` as standard GNU version.
@ -71,12 +72,18 @@ func CompareVersion(a, b string) int {
// v4.20.0+incompatible
// etc.
func CompareVersionGo(a, b string) int {
a = Trim(a)
b = Trim(b)
if a != "" && a[0] == 'v' {
a = a[1:]
}
if b != "" && b[0] == 'v' {
b = b[1:]
}
var (
rawA = a
rawB = b
)
if Count(a, "-") > 1 {
if i := PosR(a, "-"); i > 0 {
a = a[:i]
@ -108,6 +115,7 @@ func CompareVersionGo(a, b string) int {
if len(array1) <= 3 && len(array2) > 3 {
return 1
}
diff = len(array2) - len(array1)
for i := 0; i < diff; i++ {
array1 = append(array1, "0")
@ -128,5 +136,13 @@ func CompareVersionGo(a, b string) int {
return -1
}
}
// Specially in Golang:
// "v4.20.1+incompatible" < "v4.20.1"
if Contains(rawA, "incompatible") {
return -1
}
if Contains(rawB, "incompatible") {
return 1
}
return 0
}

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

View File

@ -40,7 +40,7 @@ func Test_CompareVersionGo(t *testing.T) {
t.AssertEQ(gstr.CompareVersionGo("1.0.0", "v1.0.0"), 0)
t.AssertEQ(gstr.CompareVersionGo("v0.0.0-20190626092158-b2ccc519800e", "0.0.0-20190626092158"), 0)
t.AssertEQ(gstr.CompareVersionGo("v0.0.0-20190626092159-b2ccc519800e", "0.0.0-20190626092158"), 1)
t.AssertEQ(gstr.CompareVersionGo("v4.20.0+incompatible", "4.20.0"), 0)
t.AssertEQ(gstr.CompareVersionGo("v4.20.0+incompatible", "4.20.0"), -1)
t.AssertEQ(gstr.CompareVersionGo("v4.20.0+incompatible", "4.20.1"), -1)
// Note that this comparison a < b.
t.AssertEQ(gstr.CompareVersionGo("v1.12.2-0.20200413154443-b17e3a6804fa", "v1.12.2"), -1)