1 // Copyright 2023 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 gover implements support for Go toolchain versions like 1.21.0 and 1.21rc1.
6 // (For historical reasons, Go does not use semver for its toolchains.)
7 // This package provides the same basic analysis that golang.org/x/mod/semver does for semver.
8 // It also provides some helpers for extracting versions from go.mod files
9 // and for dealing with module.Versions that may use Go versions or semver
10 // depending on the module path.
17 // A version is a parsed Go version: major[.minor[.patch]][kind[pre]]
18 // The numbers are the original decimal strings to avoid integer overflows
19 // and since there is very little actual math. (Probably overflow doesn't matter in practice,
20 // but at the time this code was written, there was an existing test that used
21 // go1.99999999999, which does not fit in an int on 32-bit platforms.
22 // The "big decimal" representation avoids the problem entirely.)
24 major string // decimal
25 minor string // decimal or ""
26 patch string // decimal or ""
27 kind string // "", "alpha", "beta", "rc"
28 pre string // decimal or ""
31 // Compare returns -1, 0, or +1 depending on whether
32 // x < y, x == y, or x > y, interpreted as toolchain versions.
33 // The versions x and y must not begin with a "go" prefix: just "1.21" not "go1.21".
34 // Malformed versions compare less than well-formed versions and equal to each other.
35 // The language version "1.21" compares less than the release candidate and eventual releases "1.21rc1" and "1.21.0".
36 func Compare(x, y string) int {
40 if c := cmpInt(vx.major, vy.major); c != 0 {
43 if c := cmpInt(vx.minor, vy.minor); c != 0 {
46 if c := cmpInt(vx.patch, vy.patch); c != 0 {
49 if c := cmp.Compare(vx.kind, vy.kind); c != 0 { // "" < alpha < beta < rc
52 if c := cmpInt(vx.pre, vy.pre); c != 0 {
58 // IsLang reports whether v denotes the overall Go language version
59 // and not a specific release. Starting with the Go 1.21 release, "1.x" denotes
60 // the overall language version; the first release is "1.x.0".
61 // The distinction is important because the relative ordering is
63 // 1.21 < 1.21rc1 < 1.21.0
65 // meaning that Go 1.21rc1 and Go 1.21.0 will both handle go.mod files that
66 // say "go 1.21", but Go 1.21rc1 will not handle files that say "go 1.21.0".
67 func IsLang(x string) bool {
69 return v != version{} && v.patch == "" && v.kind == "" && v.pre == ""
72 // Lang returns the Go language version. For example, Lang("1.2.3") == "1.2".
73 func Lang(x string) string {
78 return v.major + "." + v.minor
81 // IsPrerelease reports whether v denotes a Go prerelease version.
82 func IsPrerelease(x string) bool {
83 return parse(x).kind != ""
86 // Prev returns the Go major release immediately preceding v,
87 // or v itself if v is the first Go major release (1.0) or not a supported
92 // Prev("1.2") = "1.1"
93 // Prev("1.3rc4") = "1.2"
95 func Prev(x string) string {
97 if cmpInt(v.minor, "1") <= 0 {
100 return v.major + "." + decInt(v.minor)
103 // IsValid reports whether the version x is valid.
104 func IsValid(x string) bool {
105 return parse(x) != version{}
108 // parse parses the Go version string x into a version.
109 // It returns the zero version if x is malformed.
110 func parse(x string) version {
113 // Parse major version.
115 v.major, x, ok = cutInt(x)
120 // Interpret "1" as "1.0.0".
126 // Parse . before minor version.
131 // Parse minor version.
132 v.minor, x, ok = cutInt(x[1:])
137 // Patch missing is same as "0" for older versions.
138 // Starting in Go 1.21, patch missing is different from explicit .0.
139 if cmpInt(v.minor, "21") < 0 {
145 // Parse patch if present.
147 v.patch, x, ok = cutInt(x[1:])
149 // Note that we are disallowing prereleases (alpha, beta, rc) for patch releases here (x != "").
150 // Allowing them would be a bit confusing because we already have:
152 // But a prerelease of a patch would have the opposite effect:
153 // 1.21.3rc1 < 1.21.3
154 // We've never needed them before, so let's not start now.
162 for i < len(x) && (x[i] < '0' || '9' < x[i]) {
168 v.kind, x = x[:i], x[i:]
172 v.pre, x, ok = cutInt(x)
180 // cutInt scans the leading decimal number at the start of x to an integer
181 // and returns that value and the rest of the string.
182 func cutInt(x string) (n, rest string, ok bool) {
184 for i < len(x) && '0' <= x[i] && x[i] <= '9' {
187 if i == 0 || x[0] == '0' && i != 1 {
190 return x[:i], x[i:], true
193 // cmpInt returns cmp.Compare(x, y) interpreting x and y as decimal numbers.
194 // (Copied from golang.org/x/mod/semver's compareInt.)
195 func cmpInt(x, y string) int {
212 // decInt returns the decimal string decremented by 1, or the empty string
213 // if the decimal is all zeroes.
214 // (Copied from golang.org/x/mod/module's decDecimal.)
215 func decInt(decimal string) string {
216 // Scan right to left turning 0s to 9s until you find a digit to decrement.
217 digits := []byte(decimal)
219 for ; i >= 0 && digits[i] == '0'; i-- {
223 // decimal is all zeros
226 if i == 0 && digits[i] == '1' && len(digits) > 1 {
231 return string(digits)