semver.go 8.8 KB

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