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 // according to 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.
141 func Max(v, w string) string {
144 if Compare(v, w) > 0 {
150 func parse(v string) (p parsed, ok bool) {
151 if v == "" || v[0] != 'v' {
152 p.err = "missing v prefix"
155 p.major, v, ok = parseInt(v[1:])
157 p.err = "bad major version"
167 p.err = "bad minor prefix"
171 p.minor, v, ok = parseInt(v[1:])
173 p.err = "bad minor version"
182 p.err = "bad patch prefix"
186 p.patch, v, ok = parseInt(v[1:])
188 p.err = "bad patch version"
191 if len(v) > 0 && v[0] == '-' {
192 p.prerelease, v, ok = parsePrerelease(v)
194 p.err = "bad prerelease"
198 if len(v) > 0 && v[0] == '+' {
199 p.build, v, ok = parseBuild(v)
206 p.err = "junk on end"
214 func parseInt(v string) (t, rest string, ok bool) {
218 if v[0] < '0' || '9' < v[0] {
222 for i < len(v) && '0' <= v[i] && v[i] <= '9' {
225 if v[0] == '0' && i != 1 {
228 return v[:i], v[i:], true
231 func parsePrerelease(v string) (t, rest string, ok bool) {
232 // "A pre-release version MAY be denoted by appending a hyphen and
233 // a series of dot separated identifiers immediately following the patch version.
234 // Identifiers MUST comprise only ASCII alphanumerics and hyphen [0-9A-Za-z-].
235 // Identifiers MUST NOT be empty. Numeric identifiers MUST NOT include leading zeroes."
236 if v == "" || v[0] != '-' {
241 for i < len(v) && v[i] != '+' {
242 if !isIdentChar(v[i]) && v[i] != '.' {
246 if start == i || isBadNum(v[start:i]) {
253 if start == i || isBadNum(v[start:i]) {
256 return v[:i], v[i:], true
259 func parseBuild(v string) (t, rest string, ok bool) {
260 if v == "" || v[0] != '+' {
266 if !isIdentChar(v[i]) {
280 return v[:i], v[i:], true
283 func isIdentChar(c byte) bool {
284 return 'A' <= c && c <= 'Z' || 'a' <= c && c <= 'z' || '0' <= c && c <= '9' || c == '-'
287 func isBadNum(v string) bool {
289 for i < len(v) && '0' <= v[i] && v[i] <= '9' {
292 return i == len(v) && i > 1 && v[0] == '0'
295 func isNum(v string) bool {
297 for i < len(v) && '0' <= v[i] && v[i] <= '9' {
303 func compareInt(x, y string) int {
320 func comparePrerelease(x, y string) int {
321 // "When major, minor, and patch are equal, a pre-release version has
322 // lower precedence than a normal version.
323 // Example: 1.0.0-alpha < 1.0.0.
324 // Precedence for two pre-release versions with the same major, minor,
325 // and patch version MUST be determined by comparing each dot separated
326 // identifier from left to right until a difference is found as follows:
327 // identifiers consisting of only digits are compared numerically and
328 // identifiers with letters or hyphens are compared lexically in ASCII
329 // sort order. Numeric identifiers always have lower precedence than
330 // non-numeric identifiers. A larger set of pre-release fields has a
331 // higher precedence than a smaller set, if all of the preceding
332 // identifiers are equal.
333 // Example: 1.0.0-alpha < 1.0.0-alpha.1 < 1.0.0-alpha.beta <
334 // 1.0.0-beta < 1.0.0-beta.2 < 1.0.0-beta.11 < 1.0.0-rc.1 < 1.0.0."
344 for x != "" && y != "" {
345 x = x[1:] // skip - or .
346 y = y[1:] // skip - or .
361 if len(dx) < len(dy) {
364 if len(dx) > len(dy) {
382 func nextIdent(x string) (dx, rest string) {
384 for i < len(x) && x[i] != '.' {