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.
27
// parsed returns the parsed form of a semantic version string.
37
// IsValid reports whether v is a valid semantic version string.
38
func IsValid(v string) bool {
43
// Canonical returns the canonical formatting of the semantic version v.
44
// It fills in any missing .MINOR or .PATCH and discards build metadata.
45
// Two semantic versions compare equal only if their canonical formattings
46
// are identical strings.
47
// The canonical invalid semantic version is the empty string.
48
func Canonical(v string) string {
54
return v[:len(v)-len(p.build)]
62
// Major returns the major version prefix of the semantic version v.
63
// For example, Major("v2.1.0") == "v2".
64
// If v is an invalid semantic version string, Major returns the empty string.
65
func Major(v string) string {
70
return v[:1+len(pv.major)]
73
// MajorMinor returns the major.minor version prefix of the semantic version v.
74
// For example, MajorMinor("v2.1.0") == "v2.1".
75
// If v is an invalid semantic version string, MajorMinor returns the empty string.
76
func MajorMinor(v string) string {
81
i := 1 + len(pv.major)
82
if j := i + 1 + len(pv.minor); j <= len(v) && v[i] == '.' && v[i+1:j] == pv.minor {
85
return v[:i] + "." + pv.minor
88
// Prerelease returns the prerelease suffix of the semantic version v.
89
// For example, Prerelease("v2.1.0-pre+meta") == "-pre".
90
// If v is an invalid semantic version string, Prerelease returns the empty string.
91
func Prerelease(v string) string {
99
// Build returns the build suffix of the semantic version v.
100
// For example, Build("v2.1.0+meta") == "+meta".
101
// If v is an invalid semantic version string, Build returns the empty string.
102
func Build(v string) string {
110
// Compare returns an integer comparing two versions according to
111
// semantic version precedence.
112
// The result will be 0 if v == w, -1 if v < w, or +1 if v > w.
114
// An invalid semantic version string is considered less than a valid one.
115
// All invalid semantic version strings compare equal to each other.
116
func Compare(v, w string) int {
128
if c := compareInt(pv.major, pw.major); c != 0 {
131
if c := compareInt(pv.minor, pw.minor); c != 0 {
134
if c := compareInt(pv.patch, pw.patch); c != 0 {
137
return comparePrerelease(pv.prerelease, pw.prerelease)
140
// Max canonicalizes its arguments and then returns the version string
141
// that compares greater.
143
// Deprecated: use [Compare] instead. In most cases, returning a canonicalized
144
// version is not expected or desired.
145
func Max(v, w string) string {
148
if Compare(v, w) > 0 {
154
// ByVersion implements [sort.Interface] for sorting semantic version strings.
155
type ByVersion []string
157
func (vs ByVersion) Len() int { return len(vs) }
158
func (vs ByVersion) Swap(i, j int) { vs[i], vs[j] = vs[j], vs[i] }
159
func (vs ByVersion) Less(i, j int) bool {
160
cmp := Compare(vs[i], vs[j])
167
// Sort sorts a list of semantic version strings using [ByVersion].
168
func Sort(list []string) {
169
sort.Sort(ByVersion(list))
172
func parse(v string) (p parsed, ok bool) {
173
if v == "" || v[0] != 'v' {
176
p.major, v, ok = parseInt(v[1:])
190
p.minor, v, ok = parseInt(v[1:])
203
p.patch, v, ok = parseInt(v[1:])
207
if len(v) > 0 && v[0] == '-' {
208
p.prerelease, v, ok = parsePrerelease(v)
213
if len(v) > 0 && v[0] == '+' {
214
p.build, v, ok = parseBuild(v)
227
func parseInt(v string) (t, rest string, ok bool) {
231
if v[0] < '0' || '9' < v[0] {
235
for i < len(v) && '0' <= v[i] && v[i] <= '9' {
238
if v[0] == '0' && i != 1 {
241
return v[:i], v[i:], true
244
func parsePrerelease(v string) (t, rest string, ok bool) {
245
// "A pre-release version MAY be denoted by appending a hyphen and
246
// a series of dot separated identifiers immediately following the patch version.
247
// Identifiers MUST comprise only ASCII alphanumerics and hyphen [0-9A-Za-z-].
248
// Identifiers MUST NOT be empty. Numeric identifiers MUST NOT include leading zeroes."
249
if v == "" || v[0] != '-' {
254
for i < len(v) && v[i] != '+' {
255
if !isIdentChar(v[i]) && v[i] != '.' {
259
if start == i || isBadNum(v[start:i]) {
266
if start == i || isBadNum(v[start:i]) {
269
return v[:i], v[i:], true
272
func parseBuild(v string) (t, rest string, ok bool) {
273
if v == "" || v[0] != '+' {
279
if !isIdentChar(v[i]) && v[i] != '.' {
293
return v[:i], v[i:], true
296
func isIdentChar(c byte) bool {
297
return 'A' <= c && c <= 'Z' || 'a' <= c && c <= 'z' || '0' <= c && c <= '9' || c == '-'
300
func isBadNum(v string) bool {
302
for i < len(v) && '0' <= v[i] && v[i] <= '9' {
305
return i == len(v) && i > 1 && v[0] == '0'
308
func isNum(v string) bool {
310
for i < len(v) && '0' <= v[i] && v[i] <= '9' {
316
func compareInt(x, y string) int {
333
func comparePrerelease(x, y string) int {
334
// "When major, minor, and patch are equal, a pre-release version has
335
// lower precedence than a normal version.
336
// Example: 1.0.0-alpha < 1.0.0.
337
// Precedence for two pre-release versions with the same major, minor,
338
// and patch version MUST be determined by comparing each dot separated
339
// identifier from left to right until a difference is found as follows:
340
// identifiers consisting of only digits are compared numerically and
341
// identifiers with letters or hyphens are compared lexically in ASCII
342
// sort order. Numeric identifiers always have lower precedence than
343
// non-numeric identifiers. A larger set of pre-release fields has a
344
// higher precedence than a smaller set, if all of the preceding
345
// identifiers are equal.
346
// Example: 1.0.0-alpha < 1.0.0-alpha.1 < 1.0.0-alpha.beta <
347
// 1.0.0-beta < 1.0.0-beta.2 < 1.0.0-beta.11 < 1.0.0-rc.1 < 1.0.0."
357
for x != "" && y != "" {
358
x = x[1:] // skip - or .
359
y = y[1:] // skip - or .
374
if len(dx) < len(dy) {
377
if len(dx) > len(dy) {
395
func nextIdent(x string) (dx, rest string) {
397
for i < len(x) && x[i] != '.' {