Skip to content

Commit

Permalink
refactor: improve cran version parser
Browse files Browse the repository at this point in the history
  • Loading branch information
G-Rath committed Dec 19, 2023
1 parent 9f655d9 commit 4578201
Showing 1 changed file with 13 additions and 5 deletions.
18 changes: 13 additions & 5 deletions pkg/semantic/version-cran.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,13 @@ import (
"strings"
)

// CRANVersion is the representation of a version of a package that is held
// in the CRAN ecosystem (https://cran.r-project.org/).
//
// A version is a sequence of at least two non-negative integers separated by
// either a period or a dash.
//
// See https://astrostatistics.psu.edu/su07/R/html/base/html/package_version.html
type CRANVersion struct {
components Components
}
Expand All @@ -14,6 +21,8 @@ func (v CRANVersion) Compare(w CRANVersion) int {
return diff
}

// versions are only equal if they also have the same number of components,
// otherwise the longer one is considered greater
if len(v.components) == len(w.components) {
return 0
}
Expand All @@ -30,13 +39,12 @@ func (v CRANVersion) CompareStr(str string) int {
}

func parseCRANVersion(str string) CRANVersion {
str = strings.ReplaceAll(str, "-", ".")
// dashes and periods have the same weight, so we can just normalize to periods
parts := strings.Split(strings.ReplaceAll(str, "-", "."), ".")

splitted := strings.Split(str, ".")
components := make(Components, 0, len(parts))

components := make(Components, 0, len(splitted))

for _, s := range splitted {
for _, s := range parts {
v, _ := new(big.Int).SetString(s, 10)

components = append(components, v)
Expand Down

0 comments on commit 4578201

Please sign in to comment.