github.com/google/osv-scalibr@v0.4.1/semantic/version-rubygems.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 "strings" 19 ) 20 21 func canonicalizeRubyGemVersion(str string) string { 22 var res strings.Builder 23 24 checkPrevious := false 25 previousWasDigit := true 26 27 for _, c := range str { 28 if c == 46 { 29 checkPrevious = false 30 res.WriteString(".") 31 32 continue 33 } 34 35 isDigit := isASCIIDigit(c) 36 37 if checkPrevious && previousWasDigit != isDigit { 38 res.WriteString(".") 39 } 40 41 res.WriteRune(c) 42 43 previousWasDigit = isDigit 44 checkPrevious = true 45 } 46 47 return res.String() 48 } 49 50 func groupSegments(segs []string) (numbers []string, build []string) { 51 for _, seg := range segs { 52 _, err := convertToBigInt(seg) 53 54 if len(build) > 0 || err != nil { 55 build = append(build, seg) 56 57 continue 58 } 59 60 numbers = append(numbers, seg) 61 } 62 63 return numbers, build 64 } 65 66 func removeZeros(segs []string) []string { 67 i := len(segs) - 1 68 69 for i >= 0 { 70 if segs[i] != "0" { 71 i++ 72 73 break 74 } 75 76 i-- 77 } 78 79 return segs[:max(i, 0)] 80 } 81 82 func canonicalSegments(segs []string) (canSegs []string) { 83 numbers, build := groupSegments(segs) 84 85 return append(removeZeros(numbers), removeZeros(build)...) 86 } 87 88 func compareRubyGemsComponents(a, b []string) int { 89 numberOfComponents := max(len(a), len(b)) 90 91 for i := range numberOfComponents { 92 as := fetch(a, i, "0") 93 bs := fetch(b, i, "0") 94 95 ai, aErr := convertToBigInt(as) 96 bi, bErr := convertToBigInt(bs) 97 98 switch { 99 case aErr == nil && bErr == nil: 100 if diff := ai.Cmp(bi); diff != 0 { 101 return diff 102 } 103 case aErr != nil && bErr != nil: 104 if diff := strings.Compare(as, bs); diff != 0 { 105 return diff 106 } 107 case aErr == nil: 108 return +1 109 default: 110 return -1 111 } 112 } 113 114 return 0 115 } 116 117 type rubyGemsVersion struct { 118 Original string 119 Segments []string 120 } 121 122 func parseRubyGemsVersion(str string) rubyGemsVersion { 123 return rubyGemsVersion{ 124 str, 125 canonicalSegments(strings.Split(canonicalizeRubyGemVersion(str), ".")), 126 } 127 } 128 129 func (v rubyGemsVersion) compare(w rubyGemsVersion) int { 130 return compareRubyGemsComponents(v.Segments, w.Segments) 131 } 132 133 func (v rubyGemsVersion) CompareStr(str string) (int, error) { 134 return v.compare(parseRubyGemsVersion(str)), nil 135 }