github.com/bir3/gocompiler@v0.9.2202/src/xvendor/golang.org/x/tools/internal/versions/versions.go (about) 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. 4 5 package versions 6 7 // Note: If we use build tags to use go/versions when go >=1.22, 8 // we run into go.dev/issue/53737. Under some operations users would see an 9 // import of "go/versions" even if they would not compile the file. 10 // For example, during `go get -u ./...` (go.dev/issue/64490) we do not try to include 11 // For this reason, this library just a clone of go/versions for the moment. 12 13 // Lang returns the Go language version for version x. 14 // If x is not a valid version, Lang returns the empty string. 15 // For example: 16 // 17 // Lang("go1.21rc2") = "go1.21" 18 // Lang("go1.21.2") = "go1.21" 19 // Lang("go1.21") = "go1.21" 20 // Lang("go1") = "go1" 21 // Lang("bad") = "" 22 // Lang("1.21") = "" 23 func Lang(x string) string { 24 v := lang(stripGo(x)) 25 if v == "" { 26 return "" 27 } 28 return x[:2+len(v)] // "go"+v without allocation 29 } 30 31 // Compare returns -1, 0, or +1 depending on whether 32 // x < y, x == y, or x > y, interpreted as Go versions. 33 // The versions x and y must begin with a "go" prefix: "go1.21" not "1.21". 34 // Invalid versions, including the empty string, compare less than 35 // valid versions and equal to each other. 36 // The language version "go1.21" compares less than the 37 // release candidate and eventual releases "go1.21rc1" and "go1.21.0". 38 // Custom toolchain suffixes are ignored during comparison: 39 // "go1.21.0" and "go1.21.0-bigcorp" are equal. 40 func Compare(x, y string) int { return compare(stripGo(x), stripGo(y)) } 41 42 // IsValid reports whether the version x is valid. 43 func IsValid(x string) bool { return isValid(stripGo(x)) } 44 45 // stripGo converts from a "go1.21" version to a "1.21" version. 46 // If v does not start with "go", stripGo returns the empty string (a known invalid version). 47 func stripGo(v string) string { 48 if len(v) < 2 || v[:2] != "go" { 49 return "" 50 } 51 return v[2:] 52 }