12 // The compiled regular expression used to test the validity of a version.
14 versionRegexp *regexp.Regexp
15 semverRegexp *regexp.Regexp
18 // The raw regular expression string used for testing the validity
21 VersionRegexpRaw string = `v?([0-9]+(\.[0-9]+)*?)` +
22 `(-([0-9]+[0-9A-Za-z\-~]*(\.[0-9A-Za-z\-~]+)*)|(-?([A-Za-z\-~]+[0-9A-Za-z\-~]*(\.[0-9A-Za-z\-~]+)*)))?` +
23 `(\+([0-9A-Za-z\-~]+(\.[0-9A-Za-z\-~]+)*))?` +
26 // SemverRegexpRaw requires a separator between version and prerelease
27 SemverRegexpRaw string = `v?([0-9]+(\.[0-9]+)*?)` +
28 `(-([0-9]+[0-9A-Za-z\-~]*(\.[0-9A-Za-z\-~]+)*)|(-([A-Za-z\-~]+[0-9A-Za-z\-~]*(\.[0-9A-Za-z\-~]+)*)))?` +
29 `(\+([0-9A-Za-z\-~]+(\.[0-9A-Za-z\-~]+)*))?` +
33 // Version represents a single version.
43 versionRegexp = regexp.MustCompile("^" + VersionRegexpRaw + "$")
44 semverRegexp = regexp.MustCompile("^" + SemverRegexpRaw + "$")
47 // NewVersion parses the given version and returns a new
49 func NewVersion(v string) (*Version, error) {
50 return newVersion(v, versionRegexp)
53 // NewSemver parses the given version and returns a new
54 // Version that adheres strictly to SemVer specs
55 // https://semver.org/
56 func NewSemver(v string) (*Version, error) {
57 return newVersion(v, semverRegexp)
60 func newVersion(v string, pattern *regexp.Regexp) (*Version, error) {
61 matches := pattern.FindStringSubmatch(v)
63 return nil, fmt.Errorf("Malformed version: %s", v)
65 segmentsStr := strings.Split(matches[1], ".")
66 segments := make([]int64, len(segmentsStr))
68 for i, str := range segmentsStr {
69 val, err := strconv.ParseInt(str, 10, 64)
71 return nil, fmt.Errorf(
72 "Error parsing version: %s", err)
75 segments[i] = int64(val)
79 // Even though we could support more than three segments, if we
80 // got less than three, pad it with 0s. This is to cover the basic
81 // default usecase of semver, which is MAJOR.MINOR.PATCH at the minimum
82 for i := len(segments); i < 3; i++ {
83 segments = append(segments, 0)
92 metadata: matches[10],
100 // Must is a helper that wraps a call to a function returning (*Version, error)
101 // and panics if error is non-nil.
102 func Must(v *Version, err error) *Version {
110 // Compare compares this version to another version. This
111 // returns -1, 0, or 1 if this version is smaller, equal,
112 // or larger than the other version, respectively.
114 // If you want boolean results, use the LessThan, Equal,
115 // or GreaterThan methods.
116 func (v *Version) Compare(other *Version) int {
117 // A quick, efficient equality check
118 if v.String() == other.String() {
122 segmentsSelf := v.Segments64()
123 segmentsOther := other.Segments64()
125 // If the segments are the same, we must compare on prerelease info
126 if reflect.DeepEqual(segmentsSelf, segmentsOther) {
127 preSelf := v.Prerelease()
128 preOther := other.Prerelease()
129 if preSelf == "" && preOther == "" {
139 return comparePrereleases(preSelf, preOther)
142 // Get the highest specificity (hS), or if they're equal, just use segmentSelf length
143 lenSelf := len(segmentsSelf)
144 lenOther := len(segmentsOther)
146 if lenSelf < lenOther {
149 // Compare the segments
150 // Because a constraint could have more/less specificity than the version it's
151 // checking, we need to account for a lopsided or jagged comparison
152 for i := 0; i < hS; i++ {
154 // This means Self had the lower specificity
155 // Check to see if the remaining segments in Other are all zeros
156 if !allZero(segmentsOther[i:]) {
157 // if not, it means that Other has to be greater than Self
161 } else if i > lenOther-1 {
162 // this means Other had the lower specificity
163 // Check to see if the remaining segments in Self are all zeros -
164 if !allZero(segmentsSelf[i:]) {
165 //if not, it means that Self has to be greater than Other
170 lhs := segmentsSelf[i]
171 rhs := segmentsOther[i]
174 } else if lhs < rhs {
177 // Otherwis, rhs was > lhs, they're not equal
181 // if we got this far, they're equal
185 func allZero(segs []int64) bool {
186 for _, s := range segs {
194 func comparePart(preSelf string, preOther string) int {
195 if preSelf == preOther {
201 selfInt, err := strconv.ParseInt(preSelf, 10, 64)
208 otherInt, err = strconv.ParseInt(preOther, 10, 64)
213 // if a part is empty, we use the other to decide
228 if selfNumeric && !otherNumeric {
230 } else if !selfNumeric && otherNumeric {
232 } else if !selfNumeric && !otherNumeric && preSelf > preOther {
234 } else if selfInt > otherInt {
241 func comparePrereleases(v string, other string) int {
242 // the same pre release!
247 // split both pre releases for analyse their parts
248 selfPreReleaseMeta := strings.Split(v, ".")
249 otherPreReleaseMeta := strings.Split(other, ".")
251 selfPreReleaseLen := len(selfPreReleaseMeta)
252 otherPreReleaseLen := len(otherPreReleaseMeta)
254 biggestLen := otherPreReleaseLen
255 if selfPreReleaseLen > otherPreReleaseLen {
256 biggestLen = selfPreReleaseLen
259 // loop for parts to find the first difference
260 for i := 0; i < biggestLen; i = i + 1 {
262 if i < selfPreReleaseLen {
263 partSelfPre = selfPreReleaseMeta[i]
267 if i < otherPreReleaseLen {
268 partOtherPre = otherPreReleaseMeta[i]
271 compare := comparePart(partSelfPre, partOtherPre)
272 // if parts are equals, continue the loop
281 // Equal tests if two versions are equal.
282 func (v *Version) Equal(o *Version) bool {
283 return v.Compare(o) == 0
286 // GreaterThan tests if this version is greater than another version.
287 func (v *Version) GreaterThan(o *Version) bool {
288 return v.Compare(o) > 0
291 // LessThan tests if this version is less than another version.
292 func (v *Version) LessThan(o *Version) bool {
293 return v.Compare(o) < 0
296 // Metadata returns any metadata that was part of the version
299 // Metadata is anything that comes after the "+" in the version.
300 // For example, with "1.2.3+beta", the metadata is "beta".
301 func (v *Version) Metadata() string {
305 // Prerelease returns any prerelease data that is part of the version,
306 // or blank if there is no prerelease data.
308 // Prerelease information is anything that comes after the "-" in the
309 // version (but before any metadata). For example, with "1.2.3-beta",
310 // the prerelease information is "beta".
311 func (v *Version) Prerelease() string {
315 // Segments returns the numeric segments of the version as a slice of ints.
317 // This excludes any metadata or pre-release information. For example,
318 // for a version "1.2.3-beta", segments will return a slice of
320 func (v *Version) Segments() []int {
321 segmentSlice := make([]int, len(v.segments))
322 for i, v := range v.segments {
323 segmentSlice[i] = int(v)
328 // Segments64 returns the numeric segments of the version as a slice of int64s.
330 // This excludes any metadata or pre-release information. For example,
331 // for a version "1.2.3-beta", segments will return a slice of
333 func (v *Version) Segments64() []int64 {
334 result := make([]int64, len(v.segments))
335 copy(result, v.segments)
339 // String returns the full version string included pre-release
340 // and metadata information.
342 // This value is rebuilt according to the parsed segments and other
343 // information. Therefore, ambiguities in the version string such as
344 // prefixed zeroes (1.04.0 => 1.4.0), `v` prefix (v1.0.0 => 1.0.0), and
345 // missing parts (1.0 => 1.0.0) will be made into a canonicalized form
346 // as shown in the parenthesized examples.
347 func (v *Version) String() string {
349 fmtParts := make([]string, len(v.segments))
350 for i, s := range v.segments {
351 // We can ignore err here since we've pre-parsed the values in segments
352 str := strconv.FormatInt(s, 10)
355 fmt.Fprintf(&buf, strings.Join(fmtParts, "."))
357 fmt.Fprintf(&buf, "-%s", v.pre)
359 if v.metadata != "" {
360 fmt.Fprintf(&buf, "+%s", v.metadata)
366 // Original returns the original parsed version as-is, including any
367 // potential whitespace, `v` prefix, etc.
368 func (v *Version) Original() string {