fix issue in border value checks for gstr.CompareVersion/CompareVersionGo

This commit is contained in:
John Guo
2021-08-10 19:29:54 +08:00
parent 00f0a743fc
commit 9c5642f141
2 changed files with 10 additions and 4 deletions

View File

@ -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 {

View File

@ -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)