37 Star 404 Fork 74

GVPrancher/rancher

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
version.go 1.52 KB
一键复制 编辑 原始数据 按行查看 历史
Darren Shepherd 提交于 2018-01-26 08:50 . Move sub repos to pkg/
package version
import (
"regexp"
"strconv"
"strings"
)
var (
numberRe = regexp.MustCompile("[0-9]+")
wordRe = regexp.MustCompile("[a-z]+")
)
func GreaterThan(a, b string) bool {
a = stripMetadata(a)
b = stripMetadata(b)
a = strings.TrimLeft(a, "v")
b = strings.TrimLeft(b, "v")
aSplit := periodDashSplit(a)
bSplit := periodDashSplit(b)
if len(bSplit) > len(aSplit) {
return !GreaterThan(b, a) && a != b
}
for i := 0; i < len(aSplit); i++ {
if i == len(bSplit) {
if _, err := strconv.Atoi(aSplit[i]); err == nil {
return true
}
return false
}
aWord := wordRe.FindString(aSplit[i])
bWord := wordRe.FindString(bSplit[i])
if aWord != "" && bWord != "" {
if strings.Compare(aWord, bWord) > 0 {
return true
}
if strings.Compare(bWord, aWord) > 0 {
return false
}
}
aMatch := numberRe.FindString(aSplit[i])
bMatch := numberRe.FindString(bSplit[i])
if aMatch == "" || bMatch == "" {
if strings.Compare(aSplit[i], bSplit[i]) > 0 {
return true
}
if strings.Compare(bSplit[i], aSplit[i]) > 0 {
return false
}
}
aNum, _ := strconv.Atoi(aMatch)
bNum, _ := strconv.Atoi(bMatch)
if aNum > bNum {
return true
}
if bNum > aNum {
return false
}
}
return false
}
func stripMetadata(v string) string {
split := strings.Split(v, "+")
if len(split) > 1 {
return split[0]
}
return v
}
func periodDashSplit(s string) []string {
return strings.FieldsFunc(s, func(r rune) bool {
switch r {
case '.', '-':
return true
}
return false
})
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/rancher/rancher.git
git@gitee.com:rancher/rancher.git
rancher
rancher
rancher
v2.2.0-rc6

搜索帮助