Skip to content

Commit

Permalink
refactor(semantic): simplify comparing of RubyGem version components (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
G-Rath authored Oct 31, 2024
1 parent 79317a8 commit 96bb62c
Showing 1 changed file with 9 additions and 36 deletions.
45 changes: 9 additions & 36 deletions pkg/semantic/version-rubygems.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package semantic

import (
"strconv"
"strings"
)

Expand Down Expand Up @@ -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")

Expand All @@ -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
Expand Down

0 comments on commit 96bb62c

Please sign in to comment.