From 8afe68f3f1ddfad0310f2bcc5ce54b8e235a1d26 Mon Sep 17 00:00:00 2001 From: warrior <106664363+shssen@users.noreply.github.com> Date: Sat, 9 Jul 2022 14:56:49 +0800 Subject: [PATCH] refactor:remove duplicate codes (#2101) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: 沈四胜 --- core/utils/version.go | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/core/utils/version.go b/core/utils/version.go index 67ea203f..c50544d8 100644 --- a/core/utils/version.go +++ b/core/utils/version.go @@ -35,13 +35,12 @@ func CompareVersions(v1, op, v2 string) bool { // return -1 if v1v2 func compare(v1, v2 string) int { - v1 = replacer.Replace(v1) - v2 = replacer.Replace(v2) - fields1 := strings.Split(v1, ".") - fields2 := strings.Split(v2, ".") - ver1 := strsToInts(fields1) - ver2 := strsToInts(fields2) - shorter := mathx.MinInt(len(ver1), len(ver2)) + v1, v2 = replacer.Replace(v1), replacer.Replace(v2) + fields1, fields2 := strings.Split(v1, "."), strings.Split(v2, ".") + + ver1, ver2 := strsToInts(fields1), strsToInts(fields2) + ver1len, ver2len := len(ver1), len(ver2) + shorter := mathx.MinInt(ver1len, ver2len) for i := 0; i < shorter; i++ { if ver1[i] == ver2[i] { @@ -53,9 +52,9 @@ func compare(v1, v2 string) int { } } - if len(ver1) < len(ver2) { + if ver1len < ver2len { return -1 - } else if len(ver1) == len(ver2) { + } else if ver1len == ver2len { return 0 } else { return 1