代码拉取完成,页面将自动刷新
package glib
import (
"strconv"
"strings"
)
func _vcCanonicalize(version string) string {
ver := []byte(version)
l := len(ver)
if l == 0 {
return ""
}
var buf = make([]byte, l*2)
j := 0
for i, v := range ver {
next := uint8(0)
if i+1 < l { // Have the next one
next = ver[i+1]
}
if v == '-' || v == '_' || v == '+' { // repalce '-', '_', '+' to '.'
if j > 0 && buf[j-1] != '.' {
buf[j] = '.'
j++
}
} else if (next > 0) &&
(!(next >= '0' && next <= '9') && (v >= '0' && v <= '9')) ||
(!(v >= '0' && v <= '9') && (next >= '0' && next <= '9')) { // Insert '.' before and after a non-digit
buf[j] = v
j++
if v != '.' && next != '.' {
buf[j] = '.'
j++
}
continue
} else if !((v >= '0' && v <= '9') ||
(v >= 'a' && v <= 'z') || (v >= 'A' && v <= 'Z')) { // Non-letters and numbers
if j > 0 && buf[j-1] != '.' {
buf[j] = '.'
j++
}
} else {
buf[j] = v
j++
}
}
return string(buf[:j])
}
func _vcSpecial(form1, form2 string) int {
found1, found2, len1, len2 := -1, -1, len(form1), len(form2)
// (Any string not found) < dev < alpha = a < beta = b < RC = rc < # < pl = p
forms := map[string]int{
"dev": 0,
"alpha": 1,
"a": 1,
"beta": 2,
"b": 2,
"RC": 3,
"rc": 3,
"#": 4,
"pl": 5,
"p": 5,
}
for name, order := range forms {
if len1 < len(name) {
continue
}
if strings.Compare(form1[:len(name)], name) == 0 {
found1 = order
break
}
}
for name, order := range forms {
if len2 < len(name) {
continue
}
if strings.Compare(form2[:len(name)], name) == 0 {
found2 = order
break
}
}
if found1 == found2 {
return 0
} else if found1 > found2 {
return 1
} else {
return -1
}
}
func _vcCompare(origV1, origV2 string) int {
if origV1 == "" || origV2 == "" {
if origV1 == "" && origV2 == "" {
return 0
}
if origV1 == "" {
return -1
}
return 1
}
ver1, ver2, compare := "", "", 0
if origV1[0] == '#' {
ver1 = origV1
} else {
ver1 = _vcCanonicalize(origV1)
}
if origV2[0] == '#' {
ver2 = origV2
} else {
ver2 = _vcCanonicalize(origV2)
}
n1, n2 := 0, 0
for {
p1, p2 := "", ""
n1 = strings.IndexByte(ver1, '.')
if n1 == -1 {
p1, ver1 = ver1[:], ""
} else {
p1, ver1 = ver1[:n1], ver1[n1+1:]
}
n2 = strings.IndexByte(ver2, '.')
if n2 == -1 {
p2, ver2 = ver2, ""
} else {
p2, ver2 = ver2[:n2], ver2[n2+1:]
}
if (p1[0] >= '0' && p1[0] <= '9') && (p2[0] >= '0' && p2[0] <= '9') { // all isdigit
l1, _ := strconv.Atoi(p1)
l2, _ := strconv.Atoi(p2)
if l1 > l2 {
compare = 1
} else if l1 == l2 {
compare = 0
} else {
compare = -1
}
} else if !(p1[0] >= '0' && p1[0] <= '9') && !(p2[0] >= '0' && p2[0] <= '9') { // all isndigit
compare = _vcSpecial(p1, p2)
} else { // part is digit
if p1[0] >= '0' && p1[0] <= '9' { // isdigit
compare = _vcSpecial("#N#", p2)
} else {
compare = _vcSpecial(p1, "#N#")
}
}
if compare != 0 || n1 == -1 || n2 == -1 {
break
}
}
if compare == 0 {
if ver1 != "" {
if ver1[0] >= '0' && ver1[0] <= '9' {
compare = 1
} else {
compare = _vcCompare(ver1, "#N#")
}
} else if ver2 != "" {
if ver2[0] >= '0' && ver2[0] <= '9' {
compare = -1
} else {
compare = _vcCompare("#N#", ver2)
}
}
}
return compare
}
// 版本对比
func VersionCompare(version1, version2, operator string) bool {
compare := _vcCompare(version1, version2)
switch operator {
case "<", "lt":
return compare == -1
case "<=", "le":
return compare != 1
case ">", "gt":
return compare == 1
case ">=", "ge":
return compare != -1
case "==", "=", "eq":
return compare == 0
case "!=", "<>", "ne":
return compare != 0
default:
panic("operator: invalid")
}
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。