Compare commits
No commits in common. "d7e591277a18b88bb22d9bde1db4f7d76e965da8" and "d9aed3d80000fb3f5ea7fa0ef81278e4bdba015d" have entirely different histories.
d7e591277a
...
d9aed3d800
@ -1,15 +0,0 @@
|
||||
// Package define ...
|
||||
//
|
||||
// Description : define ...
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 2023-12-27 17:08
|
||||
package define
|
||||
|
||||
const (
|
||||
VersionCompareFail = -2 // 版本比较失败
|
||||
VersionOneMax = -1 // 第一个版本号大
|
||||
VersionEqual = 0 // 两个版本号相等
|
||||
VersionTwoMax = 1 // 第二个版本号大
|
||||
)
|
@ -1,88 +0,0 @@
|
||||
// Package tool ...
|
||||
//
|
||||
// Description : util ...
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 2023-12-27 17:06
|
||||
package tool
|
||||
|
||||
import (
|
||||
"git.zhangdeman.cn/zhangdeman/wrapper"
|
||||
"git.zhangdeman.cn/zhangdeman/wrapper/tool/define"
|
||||
)
|
||||
|
||||
var (
|
||||
Version = &version{}
|
||||
)
|
||||
|
||||
type version struct {
|
||||
}
|
||||
|
||||
// getVersionArr 解析版本号
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 17:42 2023/12/27
|
||||
func (v *version) getVersionArr(versionOne string, versionTwo string) ([]int64, []int64, error) {
|
||||
oneWrapper := wrapper.String(versionOne).ToInt64Slice(".")
|
||||
twoWrapper := wrapper.String(versionTwo).ToInt64Slice(".")
|
||||
if oneWrapper.Err != nil {
|
||||
return nil, nil, oneWrapper.Err
|
||||
}
|
||||
if twoWrapper.Err != nil {
|
||||
return nil, nil, twoWrapper.Err
|
||||
}
|
||||
return oneWrapper.Value, twoWrapper.Value, nil
|
||||
}
|
||||
|
||||
// Compare 比较版本号的大小, 版本号格式必须是 x.y.z 的形式,几个 . 不限制, x、y、z 必须是是数字
|
||||
//
|
||||
// strictMode 严格模式, 非严格模式下, 2.4 == 2.4.0 , 开启严格模式, 则认为 2.4 < 2.4.0 , 因为 2.4 没有小版本号
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 17:07 2023/12/27
|
||||
func (v *version) Compare(versionOne string, versionTwo string, strictMode bool) (int, error) {
|
||||
oneVersionArr, twoVersionArr, err := v.getVersionArr(versionOne, versionTwo)
|
||||
if nil != err {
|
||||
return define.VersionCompareFail, err
|
||||
}
|
||||
oneVersionLength := len(oneVersionArr)
|
||||
twoVersionLength := len(twoVersionArr)
|
||||
// 版本号长度不一致补位
|
||||
if oneVersionLength > twoVersionLength {
|
||||
for fill := 0; fill < oneVersionLength-twoVersionLength; fill++ {
|
||||
twoVersionArr = append(twoVersionArr, 0)
|
||||
}
|
||||
}
|
||||
if oneVersionLength < twoVersionLength {
|
||||
for fill := 0; fill < twoVersionLength-oneVersionLength; fill++ {
|
||||
oneVersionArr = append(oneVersionArr, 0)
|
||||
}
|
||||
}
|
||||
|
||||
// 开始比较
|
||||
for offset := 0; offset < len(oneVersionArr); offset++ {
|
||||
if oneVersionArr[offset] == twoVersionArr[offset] {
|
||||
// 当前位版本相等
|
||||
continue
|
||||
}
|
||||
if oneVersionArr[offset] > twoVersionArr[offset] {
|
||||
// 第一个版本号大
|
||||
return define.VersionOneMax, nil
|
||||
} else {
|
||||
// 第二个版本号大
|
||||
return define.VersionTwoMax, nil
|
||||
}
|
||||
}
|
||||
// 版本号一致, 判断严格模式与非严格模式
|
||||
if !strictMode || oneVersionLength == twoVersionLength {
|
||||
return define.VersionEqual, nil
|
||||
}
|
||||
return wrapper.TernaryOperator.Int(
|
||||
oneVersionLength > twoVersionLength,
|
||||
wrapper.Int(define.VersionOneMax),
|
||||
wrapper.Int(define.VersionTwoMax),
|
||||
).ToInt().Value, nil
|
||||
}
|
@ -1,22 +0,0 @@
|
||||
// Package tool ...
|
||||
//
|
||||
// Description : tool ...
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 2023-12-27 17:51
|
||||
package tool
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func Test_version_Compare(t *testing.T) {
|
||||
fmt.Println(Version.Compare("2.4.1", "2.4.1.0", true))
|
||||
fmt.Println(Version.Compare("2.4.1", "2.4.1.0", false))
|
||||
fmt.Println(Version.Compare("2.4.0", "2.4.1", true))
|
||||
fmt.Println(Version.Compare("2.4.2", "2.4.1", true))
|
||||
fmt.Println(Version.Compare("2.4", "2.4.1", true))
|
||||
fmt.Println(Version.Compare("2.4.2", "2.4", true))
|
||||
}
|
Loading…
Reference in New Issue
Block a user