mirror of
https://gitee.com/johng/gf
synced 2026-07-06 13:42:46 +08:00
fix issue in border value checks for gstr.CompareVersion/CompareVersionGo
This commit is contained in:
@ -24,10 +24,10 @@ import (
|
||||
// 10.2.0
|
||||
// etc.
|
||||
func CompareVersion(a, b string) int {
|
||||
if a[0] == 'v' {
|
||||
if a != "" && a[0] == 'v' {
|
||||
a = a[1:]
|
||||
}
|
||||
if b[0] == 'v' {
|
||||
if b != "" && b[0] == 'v' {
|
||||
b = b[1:]
|
||||
}
|
||||
var (
|
||||
@ -71,10 +71,10 @@ func CompareVersion(a, b string) int {
|
||||
// v4.20.0+incompatible
|
||||
// etc.
|
||||
func CompareVersionGo(a, b string) int {
|
||||
if a[0] == 'v' {
|
||||
if a != "" && a[0] == 'v' {
|
||||
a = a[1:]
|
||||
}
|
||||
if b[0] == 'v' {
|
||||
if b != "" && b[0] == 'v' {
|
||||
b = b[1:]
|
||||
}
|
||||
if Count(a, "-") > 1 {
|
||||
|
||||
@ -17,6 +17,9 @@ import (
|
||||
|
||||
func Test_CompareVersion(t *testing.T) {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
t.AssertEQ(gstr.CompareVersion("1", ""), 1)
|
||||
t.AssertEQ(gstr.CompareVersion("", ""), 0)
|
||||
t.AssertEQ(gstr.CompareVersion("", "v0.1"), -1)
|
||||
t.AssertEQ(gstr.CompareVersion("1", "v0.99"), 1)
|
||||
t.AssertEQ(gstr.CompareVersion("v1.0", "v0.99"), 1)
|
||||
t.AssertEQ(gstr.CompareVersion("v1.0.1", "v1.1.0"), -1)
|
||||
@ -28,6 +31,9 @@ func Test_CompareVersion(t *testing.T) {
|
||||
|
||||
func Test_CompareVersionGo(t *testing.T) {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
t.AssertEQ(gstr.CompareVersionGo("1", ""), 1)
|
||||
t.AssertEQ(gstr.CompareVersionGo("", ""), 0)
|
||||
t.AssertEQ(gstr.CompareVersionGo("", "v0.1"), -1)
|
||||
t.AssertEQ(gstr.CompareVersionGo("v1.0.1", "v1.1.0"), -1)
|
||||
t.AssertEQ(gstr.CompareVersionGo("1.0.1", "v1.1.0"), -1)
|
||||
t.AssertEQ(gstr.CompareVersionGo("1.0.0", "v0.1.0"), 1)
|
||||
|
||||
Reference in New Issue
Block a user