Debian Codegraph @

version.go

raw
1 package version
2
3 import (
4 "fmt"
5 "math"
6 "reflect"
7 "regexp"
8 "strconv"
9 "strings"
10 "unicode"
11 )
12
13 // alphanumPattern is a regular expression to match all sequences of numeric
14 // characters or alphanumeric characters.
15 var alphanumPattern = regexp.MustCompile("([a-zA-Z]+)|([0-9]+)|(~)")
16
17 // Version represents a package version.
18 type Version struct {
19 epoch int
20 version string
21 release string
22 }
23
24 // NewVersion returns a parsed version
25 func NewVersion(ver string) (version Version) {
26 var err error
27
28 // Parse epoch
29 splitted := strings.SplitN(ver, ":", 2)
30 if len(splitted) == 1 {
31 version.epoch = 0
32 ver = splitted[0]
33 } else {
34 // Trim left space
35 epoch := strings.TrimLeftFunc(splitted[0], unicode.IsSpace)
36
37 version.epoch, err = strconv.Atoi(epoch)
38 if err != nil {
39 version.epoch = 0
40 }
41
42 ver = splitted[1]
43 }
44
45 // Parse version and release
46 index := strings.Index(ver, "-")
47 if index >= 0 {
48 version.version = ver[:index]
49 version.release = ver[index+1:]
50
51 } else {
52 version.version = ver
53 }
54
55 return version
56 }
57
58 // Equal returns whether this version is equal with another version.
59 func (v1 *Version) Equal(v2 Version) bool {
60 return v1.Compare(v2) == 0
61 }
62
63 // GreaterThan returns whether this version is greater than another version.
64 func (v1 *Version) GreaterThan(v2 Version) bool {
65 return v1.Compare(v2) > 0
66 }
67
68 // LessThan returns whether this version is less than another version.
69 func (v1 Version) LessThan(v2 Version) bool {
70 return v1.Compare(v2) < 0
71 }
72
73 // Compare returns an integer comparing two version.
74 // The result will be 0 if v1==v2, -1 if v1 < v2, and +1 if v1 > v2.
75 func (v1 Version) Compare(v2 Version) int {
76 // Equal
77 if reflect.DeepEqual(v1, v2) {
78 return 0
79 }
80
81 // Compare epochs
82 if v1.epoch > v2.epoch {
83 return 1
84 } else if v1.epoch < v2.epoch {
85 return -1
86 }
87
88 // Compare version
89 ret := rpmvercmp(v1.version, v2.version)
90 if ret != 0 {
91 return ret
92 }
93
94 //Compare release
95 return rpmvercmp(v1.release, v2.release)
96 }
97
98 // String returns the full version string
99 func (v1 Version) String() string {
100 version := ""
101 if v1.epoch > 0 {
102 version += fmt.Sprintf("%d:", v1.epoch)
103 }
104 version += v1.version
105
106 if v1.release != "" {
107 version += fmt.Sprintf("-%s", v1.release)
108
109 }
110 return version
111 }
112
113 // rpmcmpver compares two version or release strings.
114 // Lifted from https://github.com/cavaliercoder/go-rpm/blob/master/version.go
115 //
116 // For the original C implementation, see:
117 // https://github.com/rpm-software-management/rpm/blob/master/lib/rpmvercmp.c#L16
118 func rpmvercmp(a, b string) int {
119 // shortcut for equality
120 if a == b {
121 return 0
122 }
123
124 // get alpha/numeric segements
125 segsa := alphanumPattern.FindAllString(a, -1)
126 segsb := alphanumPattern.FindAllString(b, -1)
127 segs := int(math.Min(float64(len(segsa)), float64(len(segsb))))
128
129 // compare each segment
130 for i := 0; i < segs; i++ {
131 a := segsa[i]
132 b := segsb[i]
133
134 // compare tildes
135 if []rune(a)[0] == '~' || []rune(b)[0] == '~' {
136 if []rune(a)[0] != '~' {
137 return 1
138 }
139 if []rune(b)[0] != '~' {
140 return -1
141 }
142 }
143
144 if unicode.IsNumber([]rune(a)[0]) {
145 // numbers are always greater than alphas
146 if !unicode.IsNumber([]rune(b)[0]) {
147 // a is numeric, b is alpha
148 return 1
149 }
150
151 // trim leading zeros
152 a = strings.TrimLeft(a, "0")
153 b = strings.TrimLeft(b, "0")
154
155 // longest string wins without further comparison
156 if len(a) > len(b) {
157 return 1
158 } else if len(b) > len(a) {
159 return -1
160 }
161
162 } else if unicode.IsNumber([]rune(b)[0]) {
163 // a is alpha, b is numeric
164 return -1
165 }
166
167 // string compare
168 if a < b {
169 return -1
170 } else if a > b {
171 return 1
172 }
173 }
174
175 // segments were all the same but separators must have been different
176 if len(segsa) == len(segsb) {
177 return 0
178 }
179
180 // If there is a tilde in a segment past the min number of segments, find it.
181 if len(segsa) > segs && []rune(segsa[segs])[0] == '~' {
182 return -1
183 } else if len(segsb) > segs && []rune(segsb[segs])[0] == '~' {
184 return 1
185 }
186
187 // whoever has the most segments wins
188 if len(segsa) > len(segsb) {
189 return 1
190 }
191 return -1
192 }
193
194 // Version is a getter method that returns the version.
195 func (v *Version) Version() string {
196 return v.version
197 }
198
199 // Release is a getter method that returns the release.
200 func (v *Version) Release() string {
201 return v.release
202 }
203
204 // Epoch is a getter method that returns the epoch.
205 func (v *Version) Epoch() int {
206 return v.epoch
207 }