semver.go 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  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.
  4. // Package semver implements comparison of semantic version strings.
  5. // In this package, semantic version strings must begin with a leading "v",
  6. // as in "v1.0.0".
  7. //
  8. // The general form of a semantic version string accepted by this package is
  9. //
  10. // vMAJOR[.MINOR[.PATCH[-PRERELEASE][+BUILD]]]
  11. //
  12. // where square brackets indicate optional parts of the syntax;
  13. // MAJOR, MINOR, and PATCH are decimal integers without extra leading zeros;
  14. // PRERELEASE and BUILD are each a series of non-empty dot-separated identifiers
  15. // using only alphanumeric characters and hyphens; and
  16. // all-numeric PRERELEASE identifiers must not have leading zeros.
  17. //
  18. // This package follows Semantic Versioning 2.0.0 (see semver.org)
  19. // with two exceptions. First, it requires the "v" prefix. Second, it recognizes
  20. // vMAJOR and vMAJOR.MINOR (with no prerelease or build suffixes)
  21. // as shorthands for vMAJOR.0.0 and vMAJOR.MINOR.0.
  22. package semver
  23. import (
  24. "slices"
  25. "strings"
  26. )
  27. // parsed returns the parsed form of a semantic version string.
  28. type parsed struct {
  29. major string
  30. minor string
  31. patch string
  32. short string
  33. prerelease string
  34. build string
  35. }
  36. // IsValid reports whether v is a valid semantic version string.
  37. func IsValid(v string) bool {
  38. _, ok := parse(v)
  39. return ok
  40. }
  41. // Canonical returns the canonical formatting of the semantic version v.
  42. // It fills in any missing .MINOR or .PATCH and discards build metadata.
  43. // Two semantic versions compare equal only if their canonical formattings
  44. // are identical strings.
  45. // The canonical invalid semantic version is the empty string.
  46. func Canonical(v string) string {
  47. p, ok := parse(v)
  48. if !ok {
  49. return ""
  50. }
  51. if p.build != "" {
  52. return v[:len(v)-len(p.build)]
  53. }
  54. if p.short != "" {
  55. return v + p.short
  56. }
  57. return v
  58. }
  59. // Major returns the major version prefix of the semantic version v.
  60. // For example, Major("v2.1.0") == "v2".
  61. // If v is an invalid semantic version string, Major returns the empty string.
  62. func Major(v string) string {
  63. pv, ok := parse(v)
  64. if !ok {
  65. return ""
  66. }
  67. return v[:1+len(pv.major)]
  68. }
  69. // MajorMinor returns the major.minor version prefix of the semantic version v.
  70. // For example, MajorMinor("v2.1.0") == "v2.1".
  71. // If v is an invalid semantic version string, MajorMinor returns the empty string.
  72. func MajorMinor(v string) string {
  73. pv, ok := parse(v)
  74. if !ok {
  75. return ""
  76. }
  77. i := 1 + len(pv.major)
  78. if j := i + 1 + len(pv.minor); j <= len(v) && v[i] == '.' && v[i+1:j] == pv.minor {
  79. return v[:j]
  80. }
  81. return v[:i] + "." + pv.minor
  82. }
  83. // Prerelease returns the prerelease suffix of the semantic version v.
  84. // For example, Prerelease("v2.1.0-pre+meta") == "-pre".
  85. // If v is an invalid semantic version string, Prerelease returns the empty string.
  86. func Prerelease(v string) string {
  87. pv, ok := parse(v)
  88. if !ok {
  89. return ""
  90. }
  91. return pv.prerelease
  92. }
  93. // Build returns the build suffix of the semantic version v.
  94. // For example, Build("v2.1.0+meta") == "+meta".
  95. // If v is an invalid semantic version string, Build returns the empty string.
  96. func Build(v string) string {
  97. pv, ok := parse(v)
  98. if !ok {
  99. return ""
  100. }
  101. return pv.build
  102. }
  103. // Compare returns an integer comparing two versions according to
  104. // semantic version precedence.
  105. // The result will be 0 if v == w, -1 if v < w, or +1 if v > w.
  106. //
  107. // An invalid semantic version string is considered less than a valid one.
  108. // All invalid semantic version strings compare equal to each other.
  109. func Compare(v, w string) int {
  110. pv, ok1 := parse(v)
  111. pw, ok2 := parse(w)
  112. if !ok1 && !ok2 {
  113. return 0
  114. }
  115. if !ok1 {
  116. return -1
  117. }
  118. if !ok2 {
  119. return +1
  120. }
  121. if c := compareInt(pv.major, pw.major); c != 0 {
  122. return c
  123. }
  124. if c := compareInt(pv.minor, pw.minor); c != 0 {
  125. return c
  126. }
  127. if c := compareInt(pv.patch, pw.patch); c != 0 {
  128. return c
  129. }
  130. return comparePrerelease(pv.prerelease, pw.prerelease)
  131. }
  132. // Max canonicalizes its arguments and then returns the version string
  133. // that compares greater.
  134. //
  135. // Deprecated: use [Compare] instead. In most cases, returning a canonicalized
  136. // version is not expected or desired.
  137. func Max(v, w string) string {
  138. v = Canonical(v)
  139. w = Canonical(w)
  140. if Compare(v, w) > 0 {
  141. return v
  142. }
  143. return w
  144. }
  145. // ByVersion implements [sort.Interface] for sorting semantic version strings.
  146. type ByVersion []string
  147. func (vs ByVersion) Len() int { return len(vs) }
  148. func (vs ByVersion) Swap(i, j int) { vs[i], vs[j] = vs[j], vs[i] }
  149. func (vs ByVersion) Less(i, j int) bool { return compareVersion(vs[i], vs[j]) < 0 }
  150. // Sort sorts a list of semantic version strings using [Compare] and falls back
  151. // to use [strings.Compare] if both versions are considered equal.
  152. func Sort(list []string) {
  153. slices.SortFunc(list, compareVersion)
  154. }
  155. func compareVersion(a, b string) int {
  156. cmp := Compare(a, b)
  157. if cmp != 0 {
  158. return cmp
  159. }
  160. return strings.Compare(a, b)
  161. }
  162. func parse(v string) (p parsed, ok bool) {
  163. if v == "" || v[0] != 'v' {
  164. return
  165. }
  166. p.major, v, ok = parseInt(v[1:])
  167. if !ok {
  168. return
  169. }
  170. if v == "" {
  171. p.minor = "0"
  172. p.patch = "0"
  173. p.short = ".0.0"
  174. return
  175. }
  176. if v[0] != '.' {
  177. ok = false
  178. return
  179. }
  180. p.minor, v, ok = parseInt(v[1:])
  181. if !ok {
  182. return
  183. }
  184. if v == "" {
  185. p.patch = "0"
  186. p.short = ".0"
  187. return
  188. }
  189. if v[0] != '.' {
  190. ok = false
  191. return
  192. }
  193. p.patch, v, ok = parseInt(v[1:])
  194. if !ok {
  195. return
  196. }
  197. if len(v) > 0 && v[0] == '-' {
  198. p.prerelease, v, ok = parsePrerelease(v)
  199. if !ok {
  200. return
  201. }
  202. }
  203. if len(v) > 0 && v[0] == '+' {
  204. p.build, v, ok = parseBuild(v)
  205. if !ok {
  206. return
  207. }
  208. }
  209. if v != "" {
  210. ok = false
  211. return
  212. }
  213. ok = true
  214. return
  215. }
  216. func parseInt(v string) (t, rest string, ok bool) {
  217. if v == "" {
  218. return
  219. }
  220. if v[0] < '0' || '9' < v[0] {
  221. return
  222. }
  223. i := 1
  224. for i < len(v) && '0' <= v[i] && v[i] <= '9' {
  225. i++
  226. }
  227. if v[0] == '0' && i != 1 {
  228. return
  229. }
  230. return v[:i], v[i:], true
  231. }
  232. func parsePrerelease(v string) (t, rest string, ok bool) {
  233. // "A pre-release version MAY be denoted by appending a hyphen and
  234. // a series of dot separated identifiers immediately following the patch version.
  235. // Identifiers MUST comprise only ASCII alphanumerics and hyphen [0-9A-Za-z-].
  236. // Identifiers MUST NOT be empty. Numeric identifiers MUST NOT include leading zeroes."
  237. if v == "" || v[0] != '-' {
  238. return
  239. }
  240. i := 1
  241. start := 1
  242. for i < len(v) && v[i] != '+' {
  243. if !isIdentChar(v[i]) && v[i] != '.' {
  244. return
  245. }
  246. if v[i] == '.' {
  247. if start == i || isBadNum(v[start:i]) {
  248. return
  249. }
  250. start = i + 1
  251. }
  252. i++
  253. }
  254. if start == i || isBadNum(v[start:i]) {
  255. return
  256. }
  257. return v[:i], v[i:], true
  258. }
  259. func parseBuild(v string) (t, rest string, ok bool) {
  260. if v == "" || v[0] != '+' {
  261. return
  262. }
  263. i := 1
  264. start := 1
  265. for i < len(v) {
  266. if !isIdentChar(v[i]) && v[i] != '.' {
  267. return
  268. }
  269. if v[i] == '.' {
  270. if start == i {
  271. return
  272. }
  273. start = i + 1
  274. }
  275. i++
  276. }
  277. if start == i {
  278. return
  279. }
  280. return v[:i], v[i:], true
  281. }
  282. func isIdentChar(c byte) bool {
  283. return 'A' <= c && c <= 'Z' || 'a' <= c && c <= 'z' || '0' <= c && c <= '9' || c == '-'
  284. }
  285. func isBadNum(v string) bool {
  286. i := 0
  287. for i < len(v) && '0' <= v[i] && v[i] <= '9' {
  288. i++
  289. }
  290. return i == len(v) && i > 1 && v[0] == '0'
  291. }
  292. func isNum(v string) bool {
  293. i := 0
  294. for i < len(v) && '0' <= v[i] && v[i] <= '9' {
  295. i++
  296. }
  297. return i == len(v)
  298. }
  299. func compareInt(x, y string) int {
  300. if x == y {
  301. return 0
  302. }
  303. if len(x) < len(y) {
  304. return -1
  305. }
  306. if len(x) > len(y) {
  307. return +1
  308. }
  309. if x < y {
  310. return -1
  311. } else {
  312. return +1
  313. }
  314. }
  315. func comparePrerelease(x, y string) int {
  316. // "When major, minor, and patch are equal, a pre-release version has
  317. // lower precedence than a normal version.
  318. // Example: 1.0.0-alpha < 1.0.0.
  319. // Precedence for two pre-release versions with the same major, minor,
  320. // and patch version MUST be determined by comparing each dot separated
  321. // identifier from left to right until a difference is found as follows:
  322. // identifiers consisting of only digits are compared numerically and
  323. // identifiers with letters or hyphens are compared lexically in ASCII
  324. // sort order. Numeric identifiers always have lower precedence than
  325. // non-numeric identifiers. A larger set of pre-release fields has a
  326. // higher precedence than a smaller set, if all of the preceding
  327. // identifiers are equal.
  328. // Example: 1.0.0-alpha < 1.0.0-alpha.1 < 1.0.0-alpha.beta <
  329. // 1.0.0-beta < 1.0.0-beta.2 < 1.0.0-beta.11 < 1.0.0-rc.1 < 1.0.0."
  330. if x == y {
  331. return 0
  332. }
  333. if x == "" {
  334. return +1
  335. }
  336. if y == "" {
  337. return -1
  338. }
  339. for x != "" && y != "" {
  340. x = x[1:] // skip - or .
  341. y = y[1:] // skip - or .
  342. var dx, dy string
  343. dx, x = nextIdent(x)
  344. dy, y = nextIdent(y)
  345. if dx != dy {
  346. ix := isNum(dx)
  347. iy := isNum(dy)
  348. if ix != iy {
  349. if ix {
  350. return -1
  351. } else {
  352. return +1
  353. }
  354. }
  355. if ix {
  356. if len(dx) < len(dy) {
  357. return -1
  358. }
  359. if len(dx) > len(dy) {
  360. return +1
  361. }
  362. }
  363. if dx < dy {
  364. return -1
  365. } else {
  366. return +1
  367. }
  368. }
  369. }
  370. if x == "" {
  371. return -1
  372. } else {
  373. return +1
  374. }
  375. }
  376. func nextIdent(x string) (dx, rest string) {
  377. i := 0
  378. for i < len(x) && x[i] != '.' {
  379. i++
  380. }
  381. return x[:i], x[i:]
  382. }