You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

semver.go 8.8 kB

2 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  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. // parsed returns the parsed form of a semantic version string.
  24. type parsed struct {
  25. major string
  26. minor string
  27. patch string
  28. short string
  29. prerelease string
  30. build string
  31. err 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. func parse(v string) (p parsed, ok bool) {
  143. if v == "" || v[0] != 'v' {
  144. p.err = "missing v prefix"
  145. return
  146. }
  147. p.major, v, ok = parseInt(v[1:])
  148. if !ok {
  149. p.err = "bad major version"
  150. return
  151. }
  152. if v == "" {
  153. p.minor = "0"
  154. p.patch = "0"
  155. p.short = ".0.0"
  156. return
  157. }
  158. if v[0] != '.' {
  159. p.err = "bad minor prefix"
  160. ok = false
  161. return
  162. }
  163. p.minor, v, ok = parseInt(v[1:])
  164. if !ok {
  165. p.err = "bad minor version"
  166. return
  167. }
  168. if v == "" {
  169. p.patch = "0"
  170. p.short = ".0"
  171. return
  172. }
  173. if v[0] != '.' {
  174. p.err = "bad patch prefix"
  175. ok = false
  176. return
  177. }
  178. p.patch, v, ok = parseInt(v[1:])
  179. if !ok {
  180. p.err = "bad patch version"
  181. return
  182. }
  183. if len(v) > 0 && v[0] == '-' {
  184. p.prerelease, v, ok = parsePrerelease(v)
  185. if !ok {
  186. p.err = "bad prerelease"
  187. return
  188. }
  189. }
  190. if len(v) > 0 && v[0] == '+' {
  191. p.build, v, ok = parseBuild(v)
  192. if !ok {
  193. p.err = "bad build"
  194. return
  195. }
  196. }
  197. if v != "" {
  198. p.err = "junk on end"
  199. ok = false
  200. return
  201. }
  202. ok = true
  203. return
  204. }
  205. func parseInt(v string) (t, rest string, ok bool) {
  206. if v == "" {
  207. return
  208. }
  209. if v[0] < '0' || '9' < v[0] {
  210. return
  211. }
  212. i := 1
  213. for i < len(v) && '0' <= v[i] && v[i] <= '9' {
  214. i++
  215. }
  216. if v[0] == '0' && i != 1 {
  217. return
  218. }
  219. return v[:i], v[i:], true
  220. }
  221. func parsePrerelease(v string) (t, rest string, ok bool) {
  222. // "A pre-release version MAY be denoted by appending a hyphen and
  223. // a series of dot separated identifiers immediately following the patch version.
  224. // Identifiers MUST comprise only ASCII alphanumerics and hyphen [0-9A-Za-z-].
  225. // Identifiers MUST NOT be empty. Numeric identifiers MUST NOT include leading zeroes."
  226. if v == "" || v[0] != '-' {
  227. return
  228. }
  229. i := 1
  230. start := 1
  231. for i < len(v) && v[i] != '+' {
  232. if !isIdentChar(v[i]) && v[i] != '.' {
  233. return
  234. }
  235. if v[i] == '.' {
  236. if start == i || isBadNum(v[start:i]) {
  237. return
  238. }
  239. start = i + 1
  240. }
  241. i++
  242. }
  243. if start == i || isBadNum(v[start:i]) {
  244. return
  245. }
  246. return v[:i], v[i:], true
  247. }
  248. func parseBuild(v string) (t, rest string, ok bool) {
  249. if v == "" || v[0] != '+' {
  250. return
  251. }
  252. i := 1
  253. start := 1
  254. for i < len(v) {
  255. if !isIdentChar(v[i]) && v[i] != '.' {
  256. return
  257. }
  258. if v[i] == '.' {
  259. if start == i {
  260. return
  261. }
  262. start = i + 1
  263. }
  264. i++
  265. }
  266. if start == i {
  267. return
  268. }
  269. return v[:i], v[i:], true
  270. }
  271. func isIdentChar(c byte) bool {
  272. return 'A' <= c && c <= 'Z' || 'a' <= c && c <= 'z' || '0' <= c && c <= '9' || c == '-'
  273. }
  274. func isBadNum(v string) bool {
  275. i := 0
  276. for i < len(v) && '0' <= v[i] && v[i] <= '9' {
  277. i++
  278. }
  279. return i == len(v) && i > 1 && v[0] == '0'
  280. }
  281. func isNum(v string) bool {
  282. i := 0
  283. for i < len(v) && '0' <= v[i] && v[i] <= '9' {
  284. i++
  285. }
  286. return i == len(v)
  287. }
  288. func compareInt(x, y string) int {
  289. if x == y {
  290. return 0
  291. }
  292. if len(x) < len(y) {
  293. return -1
  294. }
  295. if len(x) > len(y) {
  296. return +1
  297. }
  298. if x < y {
  299. return -1
  300. } else {
  301. return +1
  302. }
  303. }
  304. func comparePrerelease(x, y string) int {
  305. // "When major, minor, and patch are equal, a pre-release version has
  306. // lower precedence than a normal version.
  307. // Example: 1.0.0-alpha < 1.0.0.
  308. // Precedence for two pre-release versions with the same major, minor,
  309. // and patch version MUST be determined by comparing each dot separated
  310. // identifier from left to right until a difference is found as follows:
  311. // identifiers consisting of only digits are compared numerically and
  312. // identifiers with letters or hyphens are compared lexically in ASCII
  313. // sort order. Numeric identifiers always have lower precedence than
  314. // non-numeric identifiers. A larger set of pre-release fields has a
  315. // higher precedence than a smaller set, if all of the preceding
  316. // identifiers are equal.
  317. // Example: 1.0.0-alpha < 1.0.0-alpha.1 < 1.0.0-alpha.beta <
  318. // 1.0.0-beta < 1.0.0-beta.2 < 1.0.0-beta.11 < 1.0.0-rc.1 < 1.0.0."
  319. if x == y {
  320. return 0
  321. }
  322. if x == "" {
  323. return +1
  324. }
  325. if y == "" {
  326. return -1
  327. }
  328. for x != "" && y != "" {
  329. x = x[1:] // skip - or .
  330. y = y[1:] // skip - or .
  331. var dx, dy string
  332. dx, x = nextIdent(x)
  333. dy, y = nextIdent(y)
  334. if dx != dy {
  335. ix := isNum(dx)
  336. iy := isNum(dy)
  337. if ix != iy {
  338. if ix {
  339. return -1
  340. } else {
  341. return +1
  342. }
  343. }
  344. if ix {
  345. if len(dx) < len(dy) {
  346. return -1
  347. }
  348. if len(dx) > len(dy) {
  349. return +1
  350. }
  351. }
  352. if dx < dy {
  353. return -1
  354. } else {
  355. return +1
  356. }
  357. }
  358. }
  359. if x == "" {
  360. return -1
  361. } else {
  362. return +1
  363. }
  364. }
  365. func nextIdent(x string) (dx, rest string) {
  366. i := 0
  367. for i < len(x) && x[i] != '.' {
  368. i++
  369. }
  370. return x[:i], x[i:]
  371. }