2021-01-17 21:46:25 +08:00
|
|
|
// Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
|
2020-05-16 00:10:12 +08:00
|
|
|
//
|
|
|
|
// This Source Code Form is subject to the terms of the MIT License.
|
|
|
|
// If a copy of the MIT was not distributed with this file,
|
|
|
|
// You can obtain one at https://github.com/gogf/gf.
|
|
|
|
|
|
|
|
package gstr
|
|
|
|
|
|
|
|
import (
|
|
|
|
"strings"
|
2021-11-18 22:13:12 +08:00
|
|
|
|
|
|
|
"github.com/gogf/gf/v2/util/gconv"
|
2020-05-16 00:10:12 +08:00
|
|
|
)
|
|
|
|
|
2022-06-24 16:54:24 +08:00
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
2021-10-21 18:22:47 +08:00
|
|
|
// CompareVersion compares `a` and `b` as standard GNU version.
|
|
|
|
// It returns 1 if `a` > `b`.
|
|
|
|
// It returns -1 if `a` < `b`.
|
|
|
|
// It returns 0 if `a` = `b`.
|
2020-05-16 00:12:23 +08:00
|
|
|
// GNU standard version is like:
|
2020-05-16 00:10:12 +08:00
|
|
|
// v1.0
|
|
|
|
// 1
|
|
|
|
// 1.0.0
|
|
|
|
// v1.0.1
|
|
|
|
// v2.10.8
|
|
|
|
// 10.2.0
|
|
|
|
// etc.
|
|
|
|
func CompareVersion(a, b string) int {
|
2021-08-10 19:29:54 +08:00
|
|
|
if a != "" && a[0] == 'v' {
|
2020-05-16 00:10:12 +08:00
|
|
|
a = a[1:]
|
|
|
|
}
|
2021-08-10 19:29:54 +08:00
|
|
|
if b != "" && b[0] == 'v' {
|
2020-05-16 00:10:12 +08:00
|
|
|
b = b[1:]
|
|
|
|
}
|
|
|
|
var (
|
|
|
|
array1 = strings.Split(a, ".")
|
|
|
|
array2 = strings.Split(b, ".")
|
2022-11-01 20:12:21 +08:00
|
|
|
diff int
|
2020-05-16 00:10:12 +08:00
|
|
|
)
|
|
|
|
diff = len(array2) - len(array1)
|
|
|
|
for i := 0; i < diff; i++ {
|
|
|
|
array1 = append(array1, "0")
|
|
|
|
}
|
|
|
|
diff = len(array1) - len(array2)
|
|
|
|
for i := 0; i < diff; i++ {
|
|
|
|
array2 = append(array2, "0")
|
|
|
|
}
|
|
|
|
v1 := 0
|
|
|
|
v2 := 0
|
|
|
|
for i := 0; i < len(array1); i++ {
|
|
|
|
v1 = gconv.Int(array1[i])
|
|
|
|
v2 = gconv.Int(array2[i])
|
|
|
|
if v1 > v2 {
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
if v1 < v2 {
|
|
|
|
return -1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
2021-10-21 18:22:47 +08:00
|
|
|
// CompareVersionGo compares `a` and `b` as standard Golang version.
|
|
|
|
// It returns 1 if `a` > `b`.
|
|
|
|
// It returns -1 if `a` < `b`.
|
|
|
|
// It returns 0 if `a` = `b`.
|
2020-05-16 00:10:12 +08:00
|
|
|
// Golang standard version is like:
|
|
|
|
// 1.0.0
|
|
|
|
// v1.0.1
|
|
|
|
// v2.10.8
|
|
|
|
// 10.2.0
|
|
|
|
// v0.0.0-20190626092158-b2ccc519800e
|
|
|
|
// v4.20.0+incompatible
|
|
|
|
// etc.
|
2020-05-16 00:11:08 +08:00
|
|
|
func CompareVersionGo(a, b string) int {
|
2021-11-18 22:13:12 +08:00
|
|
|
a = Trim(a)
|
|
|
|
b = Trim(b)
|
2021-08-10 19:29:54 +08:00
|
|
|
if a != "" && a[0] == 'v' {
|
2020-05-16 00:10:12 +08:00
|
|
|
a = a[1:]
|
|
|
|
}
|
2021-08-10 19:29:54 +08:00
|
|
|
if b != "" && b[0] == 'v' {
|
2020-05-16 00:10:12 +08:00
|
|
|
b = b[1:]
|
|
|
|
}
|
2021-11-18 22:13:12 +08:00
|
|
|
var (
|
|
|
|
rawA = a
|
|
|
|
rawB = b
|
|
|
|
)
|
2020-05-16 00:10:12 +08:00
|
|
|
if Count(a, "-") > 1 {
|
|
|
|
if i := PosR(a, "-"); i > 0 {
|
|
|
|
a = a[:i]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if Count(b, "-") > 1 {
|
|
|
|
if i := PosR(b, "-"); i > 0 {
|
|
|
|
b = b[:i]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if i := Pos(a, "+"); i > 0 {
|
|
|
|
a = a[:i]
|
|
|
|
}
|
|
|
|
if i := Pos(b, "+"); i > 0 {
|
|
|
|
b = b[:i]
|
|
|
|
}
|
|
|
|
a = Replace(a, "-", ".")
|
|
|
|
b = Replace(b, "-", ".")
|
|
|
|
var (
|
|
|
|
array1 = strings.Split(a, ".")
|
|
|
|
array2 = strings.Split(b, ".")
|
2022-11-01 20:12:21 +08:00
|
|
|
diff int
|
2020-05-16 00:10:12 +08:00
|
|
|
)
|
|
|
|
// Specially in Golang:
|
|
|
|
// "v1.12.2-0.20200413154443-b17e3a6804fa" < "v1.12.2"
|
|
|
|
if len(array1) > 3 && len(array2) <= 3 {
|
|
|
|
return -1
|
|
|
|
}
|
|
|
|
if len(array1) <= 3 && len(array2) > 3 {
|
|
|
|
return 1
|
|
|
|
}
|
2021-11-18 22:13:12 +08:00
|
|
|
|
2020-05-16 00:10:12 +08:00
|
|
|
diff = len(array2) - len(array1)
|
|
|
|
for i := 0; i < diff; i++ {
|
|
|
|
array1 = append(array1, "0")
|
|
|
|
}
|
|
|
|
diff = len(array1) - len(array2)
|
|
|
|
for i := 0; i < diff; i++ {
|
|
|
|
array2 = append(array2, "0")
|
|
|
|
}
|
|
|
|
v1 := 0
|
|
|
|
v2 := 0
|
|
|
|
for i := 0; i < len(array1); i++ {
|
|
|
|
v1 = gconv.Int(array1[i])
|
|
|
|
v2 = gconv.Int(array2[i])
|
|
|
|
if v1 > v2 {
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
if v1 < v2 {
|
|
|
|
return -1
|
|
|
|
}
|
|
|
|
}
|
2021-11-18 22:13:12 +08:00
|
|
|
// Specially in Golang:
|
|
|
|
// "v4.20.1+incompatible" < "v4.20.1"
|
|
|
|
if Contains(rawA, "incompatible") {
|
|
|
|
return -1
|
|
|
|
}
|
|
|
|
if Contains(rawB, "incompatible") {
|
|
|
|
return 1
|
|
|
|
}
|
2020-05-16 00:10:12 +08:00
|
|
|
return 0
|
|
|
|
}
|