fix: 解决部分软件错误判断升级的问题 (#3314)

pull/3318/head
zhengkunwang 12 months ago committed by GitHub
parent b93ea93cfe
commit c873c18a42
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -17,44 +17,60 @@ import (
"github.com/mozillazg/go-pinyin" "github.com/mozillazg/go-pinyin"
) )
func CompareVersion(version1 string, version2 string) bool { func CompareVersion(version1, version2 string) bool {
if version1 == version2 { v1s := extractNumbers(version1)
return false v2s := extractNumbers(version2)
}
version1s := strings.Split(version1, ".") maxLen := max(len(v1s), len(v2s))
version2s := strings.Split(version2, ".") v1s = append(v1s, make([]string, maxLen-len(v1s))...)
v2s = append(v2s, make([]string, maxLen-len(v2s))...)
if len(version2s) > len(version1s) {
for i := 0; i < len(version2s)-len(version1s); i++ { for i := 0; i < maxLen; i++ {
version1s = append(version1s, "0") v1, err1 := strconv.Atoi(v1s[i])
v2, err2 := strconv.Atoi(v2s[i])
if err1 != nil {
v1 = 0
} }
} if err2 != nil {
if len(version1s) > len(version2s) { v2 = 0
for i := 0; i < len(version1s)-len(version2s); i++ { }
version2s = append(version2s, "0") if v1 != v2 {
return v1 > v2
} }
} }
return false
}
n := min(len(version1s), len(version2s)) func extractNumbers(version string) []string {
re := regexp.MustCompile("[0-9]+") var numbers []string
for i := 0; i < n; i++ { start := -1
sVersion1s := re.FindAllString(version1s[i], -1) for i, r := range version {
sVersion2s := re.FindAllString(version2s[i], -1) if isDigit(r) {
if len(sVersion1s) == 0 { if start == -1 {
return false start = i
} }
if len(sVersion2s) == 0 {
return false
}
v1num, _ := strconv.Atoi(sVersion1s[0])
v2num, _ := strconv.Atoi(sVersion2s[0])
if v1num == v2num {
continue
} else { } else {
return v1num > v2num if start != -1 {
numbers = append(numbers, version[start:i])
start = -1
}
} }
} }
return true if start != -1 {
numbers = append(numbers, version[start:])
}
return numbers
}
func isDigit(r rune) bool {
return r >= '0' && r <= '9'
}
func max(x, y int) int {
if x > y {
return x
}
return y
} }
func GetSortedVersions(versions []string) []string { func GetSortedVersions(versions []string) []string {

Loading…
Cancel
Save