github.com/google/osv-scalibr@v0.4.1/semantic/version-cran.go (about) 1 // Copyright 2025 Google LLC 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package semantic 16 17 import ( 18 "fmt" 19 "math/big" 20 "strings" 21 ) 22 23 // cranVersion is the representation of a version of a package that is held 24 // in the CRAN ecosystem (https://cran.r-project.org/). 25 // 26 // A version is a sequence of at least two non-negative integers separated by 27 // either a period or a dash. 28 // 29 // See https://astrostatistics.psu.edu/su07/R/html/base/html/package_version.html 30 type cranVersion struct { 31 components components 32 } 33 34 func (v cranVersion) compare(w cranVersion) int { 35 if diff := v.components.Cmp(w.components); diff != 0 { 36 return diff 37 } 38 39 // versions are only equal if they also have the same number of components, 40 // otherwise the longer one is considered greater 41 if len(v.components) == len(w.components) { 42 return 0 43 } 44 45 if len(v.components) > len(w.components) { 46 return 1 47 } 48 49 return -1 50 } 51 52 func (v cranVersion) CompareStr(str string) (int, error) { 53 w, err := parseCRANVersion(str) 54 55 if err != nil { 56 return 0, err 57 } 58 59 return v.compare(w), nil 60 } 61 62 func parseCRANVersion(str string) (cranVersion, error) { 63 // for now, treat an empty version string as valid 64 if str == "" { 65 return cranVersion{}, nil 66 } 67 68 // dashes and periods have the same weight, so we can just normalize to periods 69 parts := strings.Split(strings.ReplaceAll(str, "-", "."), ".") 70 71 comps := make(components, 0, len(parts)) 72 73 for _, s := range parts { 74 v, ok := new(big.Int).SetString(s, 10) 75 76 if !ok { 77 return cranVersion{}, fmt.Errorf("%w: '%s' is not allowed", ErrInvalidVersion, str) 78 } 79 80 comps = append(comps, v) 81 } 82 83 return cranVersion{comps}, nil 84 }