CompareSemver returns an integer comparing two versions according to according to semantic version precedence. The result will be 0 if v == w, -1 if v < w, or +1 if v > w. An invalid semantic version string is considered less than a valid one. All invalid semantic version strings compare equal to e
(v, w string)
| 51 | // An invalid semantic version string is considered less than a valid one. |
| 52 | // All invalid semantic version strings compare equal to each other. |
| 53 | func CompareSemver(v, w string) int { |
| 54 | pv, ok1 := semParse(v) |
| 55 | pw, ok2 := semParse(w) |
| 56 | if !ok1 && !ok2 { |
| 57 | return 0 |
| 58 | } |
| 59 | if !ok1 { |
| 60 | return -1 |
| 61 | } |
| 62 | if !ok2 { |
| 63 | return +1 |
| 64 | } |
| 65 | if c := compareInt(pv.major, pw.major); c != 0 { |
| 66 | return c |
| 67 | } |
| 68 | if c := compareInt(pv.minor, pw.minor); c != 0 { |
| 69 | return c |
| 70 | } |
| 71 | if c := compareInt(pv.patch, pw.patch); c != 0 { |
| 72 | return c |
| 73 | } |
| 74 | return comparePrerelease(pv.prerelease, pw.prerelease) |
| 75 | } |
| 76 | |
| 77 | func semParse(v string) (p parsed, ok bool) { |
| 78 | if v == "" || v[0] != 'v' { |
no test coverage detected