mirror of
https://gitee.com/johng/gf.git
synced 2024-11-29 18:57:44 +08:00
add gstr.IsGNUVersion (#1937)
This commit is contained in:
parent
f0511592b5
commit
d7faae0531
@ -12,6 +12,32 @@ import (
|
|||||||
"github.com/gogf/gf/v2/util/gconv"
|
"github.com/gogf/gf/v2/util/gconv"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// IsGNUVersion checks and returns whether given `version` is valid GNU version string.
|
||||||
|
func IsGNUVersion(version string) bool {
|
||||||
|
if version != "" && (version[0] == 'v' || version[0] == 'V') {
|
||||||
|
version = version[1:]
|
||||||
|
}
|
||||||
|
if version == "" {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
var array = strings.Split(version, ".")
|
||||||
|
if len(array) > 3 {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
for _, v := range array {
|
||||||
|
if v == "" {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
if !IsNumeric(v) {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
if v[0] == '-' || v[0] == '+' {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
// CompareVersion compares `a` and `b` as standard GNU version.
|
// CompareVersion compares `a` and `b` as standard GNU version.
|
||||||
// It returns 1 if `a` > `b`.
|
// It returns 1 if `a` > `b`.
|
||||||
// It returns -1 if `a` < `b`.
|
// It returns -1 if `a` < `b`.
|
||||||
|
@ -15,6 +15,23 @@ import (
|
|||||||
"github.com/gogf/gf/v2/text/gstr"
|
"github.com/gogf/gf/v2/text/gstr"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func Test_IsGNUVersion(t *testing.T) {
|
||||||
|
gtest.C(t, func(t *gtest.T) {
|
||||||
|
t.AssertEQ(gstr.IsGNUVersion(""), false)
|
||||||
|
t.AssertEQ(gstr.IsGNUVersion("v"), false)
|
||||||
|
t.AssertEQ(gstr.IsGNUVersion("v0"), true)
|
||||||
|
t.AssertEQ(gstr.IsGNUVersion("v0."), false)
|
||||||
|
t.AssertEQ(gstr.IsGNUVersion("v1."), false)
|
||||||
|
t.AssertEQ(gstr.IsGNUVersion("v1.1"), true)
|
||||||
|
t.AssertEQ(gstr.IsGNUVersion("v1.1.0"), true)
|
||||||
|
t.AssertEQ(gstr.IsGNUVersion("v1.1."), false)
|
||||||
|
t.AssertEQ(gstr.IsGNUVersion("v1.1.0.0"), false)
|
||||||
|
t.AssertEQ(gstr.IsGNUVersion("v0.0.0"), true)
|
||||||
|
t.AssertEQ(gstr.IsGNUVersion("v1.1.-1"), false)
|
||||||
|
t.AssertEQ(gstr.IsGNUVersion("v1.1.+1"), false)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
func Test_CompareVersion(t *testing.T) {
|
func Test_CompareVersion(t *testing.T) {
|
||||||
gtest.C(t, func(t *gtest.T) {
|
gtest.C(t, func(t *gtest.T) {
|
||||||
t.AssertEQ(gstr.CompareVersion("1", ""), 1)
|
t.AssertEQ(gstr.CompareVersion("1", ""), 1)
|
||||||
|
Loading…
Reference in New Issue
Block a user