1 // Copyright 2018 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
5 // Package semver implements comparison of semantic version strings.
6 // In this package, semantic version strings must begin with a leading "v",
9 // The general form of a semantic version string accepted by this package is
11 // vMAJOR[.MINOR[.PATCH[-PRERELEASE][+BUILD]]]
13 // where square brackets indicate optional parts of the syntax;
14 // MAJOR, MINOR, and PATCH are decimal integers without extra leading zeros;
15 // PRERELEASE and BUILD are each a series of non-empty dot-separated identifiers
16 // using only alphanumeric characters and hyphens; and
17 // all-numeric PRERELEASE identifiers must not have leading zeros.
19 // This package follows Semantic Versioning 2.0.0 (see semver.org)
20 // with two exceptions. First, it requires the "v" prefix. Second, it recognizes
21 // vMAJOR and vMAJOR.MINOR (with no prerelease or build suffixes)
22 // as shorthands for vMAJOR.0.0 and vMAJOR.MINOR.0.
25 // parsed returns the parsed form of a semantic version string.
36 // IsValid reports whether v is a valid semantic version string.
37 func IsValid(v string) bool {
42 // Canonical returns the canonical formatting of the semantic version v.
43 // It fills in any missing .MINOR or .PATCH and discards build metadata.
44 // Two semantic versions compare equal only if their canonical formattings
45 // are identical strings.
46 // The canonical invalid semantic version is the empty string.
47 func Canonical(v string) string {
53 return v[:len(v)-len(p.build)]
61 // Major returns the major version prefix of the semantic version v.
62 // For example, Major("v2.1.0") == "v2".
63 // If v is an invalid semantic version string, Major returns the empty string.
64 func Major(v string) string {
69 return v[:1+len(pv.major)]
72 // MajorMinor returns the major.minor version prefix of the semantic version v.
73 // For example, MajorMinor("v2.1.0") == "v2.1".
74 // If v is an invalid semantic version string, MajorMinor returns the empty string.
75 func MajorMinor(v string) string {
80 i := 1 + len(pv.major)
81 if j := i + 1 + len(pv.minor); j <= len(v) && v[i] == '.' && v[i+1:j] == pv.minor {
84 return v[:i] + "." + pv.minor
87 // Prerelease returns the prerelease suffix of the semantic version v.
88 // For example, Prerelease("v2.1.0-pre+meta") == "-pre".
89 // If v is an invalid semantic version string, Prerelease returns the empty string.
90 func Prerelease(v string) string {
98 // Build returns the build suffix of the semantic version v.
99 // For example, Build("v2.1.0+meta") == "+meta".
100 // If v is an invalid semantic version string, Build returns the empty string.
101 func Build(v string) string {
109 // Compare returns an integer comparing two versions according to
110 // semantic version precedence.
111 // The result will be 0 if v == w, -1 if v < w, or +1 if v > w.
113 // An invalid semantic version string is considered less than a valid one.
114 // All invalid semantic version strings compare equal to each other.
115 func Compare(v, w string) int {
127 if c := compareInt(pv.major, pw.major); c != 0 {
130 if c := compareInt(pv.minor, pw.minor); c != 0 {
133 if c := compareInt(pv.patch, pw.patch); c != 0 {
136 return comparePrerelease(pv.prerelease, pw.prerelease)
139 // Max canonicalizes its arguments and then returns the version string
140 // that compares greater.
142 // Deprecated: use Compare instead. In most cases, returning a canonicalized
143 // version is not expected or desired.
144 func Max(v, w string) string {
147 if Compare(v, w) > 0 {
153 func parse(v string) (p parsed, ok bool) {
154 if v == "" || v[0] != 'v' {
155 p.err = "missing v prefix"
158 p.major, v, ok = parseInt(v[1:])
160 p.err = "bad major version"
170 p.err = "bad minor prefix"
174 p.minor, v, ok = parseInt(v[1:])
176 p.err = "bad minor version"
185 p.err = "bad patch prefix"
189 p.patch, v, ok = parseInt(v[1:])
191 p.err = "bad patch version"
194 if len(v) > 0 && v[0] == '-' {
195 p.prerelease, v, ok = parsePrerelease(v)
197 p.err = "bad prerelease"
201 if len(v) > 0 && v[0] == '+' {
202 p.build, v, ok = parseBuild(v)
209 p.err = "junk on end"
217 func parseInt(v string) (t, rest string, ok bool) {
221 if v[0] < '0' || '9' < v[0] {
225 for i < len(v) && '0' <= v[i] && v[i] <= '9' {
228 if v[0] == '0' && i != 1 {
231 return v[:i], v[i:], true
234 func parsePrerelease(v string) (t, rest string, ok bool) {
235 // "A pre-release version MAY be denoted by appending a hyphen and
236 // a series of dot separated identifiers immediately following the patch version.
237 // Identifiers MUST comprise only ASCII alphanumerics and hyphen [0-9A-Za-z-].
238 // Identifiers MUST NOT be empty. Numeric identifiers MUST NOT include leading zeroes."
239 if v == "" || v[0] != '-' {
244 for i < len(v) && v[i] != '+' {
245 if !isIdentChar(v[i]) && v[i] != '.' {
249 if start == i || isBadNum(v[start:i]) {
256 if start == i || isBadNum(v[start:i]) {
259 return v[:i], v[i:], true
262 func parseBuild(v string) (t, rest string, ok bool) {
263 if v == "" || v[0] != '+' {
269 if !isIdentChar(v[i]) && v[i] != '.' {
283 return v[:i], v[i:], true
286 func isIdentChar(c byte) bool {
287 return 'A' <= c && c <= 'Z' || 'a' <= c && c <= 'z' || '0' <= c && c <= '9' || c == '-'
290 func isBadNum(v string) bool {
292 for i < len(v) && '0' <= v[i] && v[i] <= '9' {
295 return i == len(v) && i > 1 && v[0] == '0'
298 func isNum(v string) bool {
300 for i < len(v) && '0' <= v[i] && v[i] <= '9' {
306 func compareInt(x, y string) int {
323 func comparePrerelease(x, y string) int {
324 // "When major, minor, and patch are equal, a pre-release version has
325 // lower precedence than a normal version.
326 // Example: 1.0.0-alpha < 1.0.0.
327 // Precedence for two pre-release versions with the same major, minor,
328 // and patch version MUST be determined by comparing each dot separated
329 // identifier from left to right until a difference is found as follows:
330 // identifiers consisting of only digits are compared numerically and
331 // identifiers with letters or hyphens are compared lexically in ASCII
332 // sort order. Numeric identifiers always have lower precedence than
333 // non-numeric identifiers. A larger set of pre-release fields has a
334 // higher precedence than a smaller set, if all of the preceding
335 // identifiers are equal.
336 // Example: 1.0.0-alpha < 1.0.0-alpha.1 < 1.0.0-alpha.beta <
337 // 1.0.0-beta < 1.0.0-beta.2 < 1.0.0-beta.11 < 1.0.0-rc.1 < 1.0.0."
347 for x != "" && y != "" {
348 x = x[1:] // skip - or .
349 y = y[1:] // skip - or .
364 if len(dx) < len(dy) {
367 if len(dx) > len(dy) {
385 func nextIdent(x string) (dx, rest string) {
387 for i < len(x) && x[i] != '.' {