gover.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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. // This is a fork of internal/gover for use by x/tools until
  5. // go1.21 and earlier are no longer supported by x/tools.
  6. package versions
  7. import "strings"
  8. // A gover is a parsed Go gover: major[.Minor[.Patch]][kind[pre]]
  9. // The numbers are the original decimal strings to avoid integer overflows
  10. // and since there is very little actual math. (Probably overflow doesn't matter in practice,
  11. // but at the time this code was written, there was an existing test that used
  12. // go1.99999999999, which does not fit in an int on 32-bit platforms.
  13. // The "big decimal" representation avoids the problem entirely.)
  14. type gover struct {
  15. major string // decimal
  16. minor string // decimal or ""
  17. patch string // decimal or ""
  18. kind string // "", "alpha", "beta", "rc"
  19. pre string // decimal or ""
  20. }
  21. // compare returns -1, 0, or +1 depending on whether
  22. // x < y, x == y, or x > y, interpreted as toolchain versions.
  23. // The versions x and y must not begin with a "go" prefix: just "1.21" not "go1.21".
  24. // Malformed versions compare less than well-formed versions and equal to each other.
  25. // The language version "1.21" compares less than the release candidate and eventual releases "1.21rc1" and "1.21.0".
  26. func compare(x, y string) int {
  27. vx := parse(x)
  28. vy := parse(y)
  29. if c := cmpInt(vx.major, vy.major); c != 0 {
  30. return c
  31. }
  32. if c := cmpInt(vx.minor, vy.minor); c != 0 {
  33. return c
  34. }
  35. if c := cmpInt(vx.patch, vy.patch); c != 0 {
  36. return c
  37. }
  38. if c := strings.Compare(vx.kind, vy.kind); c != 0 { // "" < alpha < beta < rc
  39. return c
  40. }
  41. if c := cmpInt(vx.pre, vy.pre); c != 0 {
  42. return c
  43. }
  44. return 0
  45. }
  46. // lang returns the Go language version. For example, lang("1.2.3") == "1.2".
  47. func lang(x string) string {
  48. v := parse(x)
  49. if v.minor == "" || v.major == "1" && v.minor == "0" {
  50. return v.major
  51. }
  52. return v.major + "." + v.minor
  53. }
  54. // isValid reports whether the version x is valid.
  55. func isValid(x string) bool {
  56. return parse(x) != gover{}
  57. }
  58. // parse parses the Go version string x into a version.
  59. // It returns the zero version if x is malformed.
  60. func parse(x string) gover {
  61. var v gover
  62. // Parse major version.
  63. var ok bool
  64. v.major, x, ok = cutInt(x)
  65. if !ok {
  66. return gover{}
  67. }
  68. if x == "" {
  69. // Interpret "1" as "1.0.0".
  70. v.minor = "0"
  71. v.patch = "0"
  72. return v
  73. }
  74. // Parse . before minor version.
  75. if x[0] != '.' {
  76. return gover{}
  77. }
  78. // Parse minor version.
  79. v.minor, x, ok = cutInt(x[1:])
  80. if !ok {
  81. return gover{}
  82. }
  83. if x == "" {
  84. // Patch missing is same as "0" for older versions.
  85. // Starting in Go 1.21, patch missing is different from explicit .0.
  86. if cmpInt(v.minor, "21") < 0 {
  87. v.patch = "0"
  88. }
  89. return v
  90. }
  91. // Parse patch if present.
  92. if x[0] == '.' {
  93. v.patch, x, ok = cutInt(x[1:])
  94. if !ok || x != "" {
  95. // Note that we are disallowing prereleases (alpha, beta, rc) for patch releases here (x != "").
  96. // Allowing them would be a bit confusing because we already have:
  97. // 1.21 < 1.21rc1
  98. // But a prerelease of a patch would have the opposite effect:
  99. // 1.21.3rc1 < 1.21.3
  100. // We've never needed them before, so let's not start now.
  101. return gover{}
  102. }
  103. return v
  104. }
  105. // Parse prerelease.
  106. i := 0
  107. for i < len(x) && (x[i] < '0' || '9' < x[i]) {
  108. if x[i] < 'a' || 'z' < x[i] {
  109. return gover{}
  110. }
  111. i++
  112. }
  113. if i == 0 {
  114. return gover{}
  115. }
  116. v.kind, x = x[:i], x[i:]
  117. if x == "" {
  118. return v
  119. }
  120. v.pre, x, ok = cutInt(x)
  121. if !ok || x != "" {
  122. return gover{}
  123. }
  124. return v
  125. }
  126. // cutInt scans the leading decimal number at the start of x to an integer
  127. // and returns that value and the rest of the string.
  128. func cutInt(x string) (n, rest string, ok bool) {
  129. i := 0
  130. for i < len(x) && '0' <= x[i] && x[i] <= '9' {
  131. i++
  132. }
  133. if i == 0 || x[0] == '0' && i != 1 { // no digits or unnecessary leading zero
  134. return "", "", false
  135. }
  136. return x[:i], x[i:], true
  137. }
  138. // cmpInt returns cmp.Compare(x, y) interpreting x and y as decimal numbers.
  139. // (Copied from golang.org/x/mod/semver's compareInt.)
  140. func cmpInt(x, y string) int {
  141. if x == y {
  142. return 0
  143. }
  144. if len(x) < len(y) {
  145. return -1
  146. }
  147. if len(x) > len(y) {
  148. return +1
  149. }
  150. if x < y {
  151. return -1
  152. } else {
  153. return +1
  154. }
  155. }