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

389 lines
8.5KB

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