From 96bb62c07b5ceb6cb9705ed622201b996a49dc8d Mon Sep 17 00:00:00 2001 From: Gareth Jones Date: Fri, 1 Nov 2024 08:42:07 +1300 Subject: [PATCH] refactor(semantic): simplify comparing of RubyGem version components (#275) --- pkg/semantic/version-rubygems.go | 45 +++++++------------------------- 1 file changed, 9 insertions(+), 36 deletions(-) diff --git a/pkg/semantic/version-rubygems.go b/pkg/semantic/version-rubygems.go index 66c6e87f..e75eae51 100644 --- a/pkg/semantic/version-rubygems.go +++ b/pkg/semantic/version-rubygems.go @@ -1,7 +1,6 @@ package semantic import ( - "strconv" "strings" ) @@ -73,11 +72,9 @@ func canonicalSegments(segs []string) (canSegs []string) { } func compareRubyGemsComponents(a, b []string) int { - max := maxInt(len(a), len(b)) + numberOfComponents := maxInt(len(a), len(b)) - var compare int - - for i := 0; i < max; i++ { + for i := 0; i < numberOfComponents; i++ { as := fetch(a, i, "0") bs := fetch(b, i, "0") @@ -86,42 +83,18 @@ func compareRubyGemsComponents(a, b []string) int { switch { case aIsNumber && bIsNumber: - compare = ai.Cmp(bi) + if diff := ai.Cmp(bi); diff != 0 { + return diff + } case !aIsNumber && !bIsNumber: - compare = strings.Compare(as, bs) + if diff := strings.Compare(as, bs); diff != 0 { + return diff + } case aIsNumber: - compare = +1 + return +1 default: - compare = -1 - } - - if compare != 0 { - if compare > 0 { - return 1 - } - - return -1 - } - } - - if len(a) > len(b) { - next := a[len(b)] - - if _, err := strconv.Atoi(next); err == nil { - return 1 - } - - return -1 - } - - if len(a) < len(b) { - next := b[len(a)] - - if _, err := strconv.Atoi(next); err == nil { return -1 } - - return +1 } return 0