forked from quay/claircore
-
Notifications
You must be signed in to change notification settings - Fork 0
/
version.go
210 lines (182 loc) · 4.82 KB
/
version.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
package ruby
import (
"errors"
"regexp"
"strings"
)
var (
anchoredVersion = regexp.MustCompile(`^\s*([0-9]+(\.[0-9a-zA-Z]+)*(-[0-9A-Za-z-]+(\.[0-9A-Za-z-]+)*)?)?\s*$`)
errInvalidVersion = errors.New("invalid gem version")
)
// Version is a RubyGem version.
// This is based on the official [implementation].
//
// [implementation]: https://github.com/rubygems/rubygems/blob/1a1948424aca90013b37cb8a78f5d1d5576023f1/lib/rubygems/version.rb
type Version struct {
segs []Segment
}
// NewVersion creates a Version out of the given string.
func NewVersion(version string) (Version, error) {
if !anchoredVersion.MatchString(version) {
return Version{}, errInvalidVersion
}
version = strings.TrimSpace(version)
if version == "" {
version = "0"
}
version = strings.ReplaceAll(version, "-", ".pre.")
return Version{
segs: canonicalize(version),
}, nil
}
// Compare returns an integer comparing this version, v, to other.
// The result will be 0 if v == other, -1 if v < other, and +1 if v > other.
func (v Version) Compare(other Version) int {
segs := v.segs
otherSegs := other.segs
leftLen := len(segs)
rightLen := len(otherSegs)
limit := max(leftLen, rightLen)
for i := 0; i < limit; i++ {
left, right := Segment(numericSegment("0")), Segment(numericSegment("0"))
if i < leftLen {
left = segs[i]
}
if i < rightLen {
right = otherSegs[i]
}
if cmp := left.Compare(right); cmp != 0 {
return cmp
}
}
return 0
}
func canonicalize(v string) []Segment {
segs, prerelease := partitionSegments(v)
// Remove trailing zero segments.
i := len(segs) - 1
for ; i >= 0; i-- {
seg, ok := segs[i].(numericSegment)
if !ok || !seg.isZero() {
break
}
}
segs = segs[:i+1]
// Remove all zero segments preceding the first letter in a prerelease version.
if prerelease {
// Find the first letter in the version.
end := -1
for i := 0; i < len(segs); i++ {
if _, ok := segs[i].(stringSegment); ok {
end = i
break
}
}
if end != -1 {
// Find where the preceding zeroes start.
var start int
for i := end - 1; i >= 0; i-- {
seg, ok := segs[i].(numericSegment)
if !ok || !seg.isZero() {
start = i + 1
break
}
}
segs = append(segs[:start], segs[end:]...)
}
}
return segs
}
func partitionSegments(v string) ([]Segment, bool) {
var prerelease bool
splitVersion := strings.Split(v, ".")
segs := make([]Segment, 0, len(splitVersion))
for _, s := range splitVersion {
if s == "" {
continue
}
if onlyDigits(s) {
segs = append(segs, numericSegment(s))
continue
}
// Ruby considers any version with a letter to be a prerelease.
prerelease = true
segs = append(segs, stringSegment(s))
}
return segs, prerelease
}
func onlyDigits(s string) bool {
// I don't know if converting to a []byte does anything
// special here, but [strconv.ParseUint] does it when ranging over a string,
// and this implementation is based on code from [strconv.ParseUint].
for _, c := range []byte(s) {
if c < '0' || c > '9' {
return false
}
}
return true
}
func max(a, b int) int {
if b > a {
return b
}
return a
}
// Segment is a part of a RubyGem version.
type Segment interface {
Compare(other Segment) int
}
var (
_ Segment = (*stringSegment)(nil)
_ Segment = (*numericSegment)(nil)
)
type stringSegment string
// Compare returns an integer comparing this version, s, to other.
// The result will be 0 if v == other, -1 if v < other, and +1 if v > other.
//
// A stringSegment is always less than a numericSegment.
func (s stringSegment) Compare(other Segment) int {
switch seg := other.(type) {
case numericSegment:
return -1
case stringSegment:
return strings.Compare(string(s), string(seg))
default:
panic("Programmer error")
}
}
type numericSegment string
// Compare returns an integer comparing this version, n, to other.
// The result will be 0 if n == other, -1 if n < other, and +1 if n > other.
//
// A numericSegment is always greater than a stringSegment.
func (n numericSegment) Compare(other Segment) int {
switch seg := other.(type) {
case stringSegment:
return +1
case numericSegment:
left, leftLen := string(n), len(n)
right, rightLen := string(seg), len(seg)
// The length of each string must match to compare them properly.
// Pad the shorter string with zeroes.
if leftLen == max(leftLen, rightLen) {
right = strings.Repeat("0", leftLen-rightLen) + right
} else {
left = strings.Repeat("0", rightLen-leftLen) + left
}
return strings.Compare(left, right)
default:
panic("Programmer error")
}
}
func (n numericSegment) isZero() bool {
// Again, I don't know if converting to a []byte does anything
// special here, but [strconv.ParseUint] does it when ranging over a string,
// and this implementation is based on code from [strconv.ParseUint].
for _, c := range []byte(n) {
if c != '0' {
return false
}
}
return true
}