|
| 1 | +package semver |
| 2 | + |
| 3 | +// ComparePrecedence compares this Version to another Version according to the canonical precedence rules. It |
| 4 | +// returns -1 if v has lower precedence than other, 1 if v has higher precedence, or 0 if the same. |
| 5 | +func (v Version) ComparePrecedence(other Version) int { |
| 6 | + if v.major < other.major { |
| 7 | + return -1 |
| 8 | + } |
| 9 | + if v.major > other.major { |
| 10 | + return 1 |
| 11 | + } |
| 12 | + if v.minor < other.minor { |
| 13 | + return -1 |
| 14 | + } |
| 15 | + if v.minor > other.minor { |
| 16 | + return 1 |
| 17 | + } |
| 18 | + if v.patch < other.patch { |
| 19 | + return -1 |
| 20 | + } |
| 21 | + if v.patch > other.patch { |
| 22 | + return 1 |
| 23 | + } |
| 24 | + if v.prerelease == "" && other.prerelease == "" { |
| 25 | + return 0 |
| 26 | + } |
| 27 | + // *no* prerelease component always has higher precedence than *any* prerelease component |
| 28 | + if v.prerelease == "" { |
| 29 | + return 1 |
| 30 | + } |
| 31 | + if other.prerelease == "" { |
| 32 | + return -1 |
| 33 | + } |
| 34 | + // compare prerelease components - the build component, if any, has no effect on precedence |
| 35 | + return comparePrereleaseIdentifiers(v.prerelease, other.prerelease) |
| 36 | +} |
| 37 | + |
| 38 | +func comparePrereleaseIdentifiers(prerel1, prerel2 string) int { |
| 39 | + // The parser has already validated the syntax of both of these strings, so we know that they both |
| 40 | + // contain one or more identifiers separated by a period, and that they contain only alphanumerics. |
| 41 | + // If an identifier contains only digits, and does not have a leading zero, then we treat it as a |
| 42 | + // number. |
| 43 | + |
| 44 | + scanner1 := newSimpleASCIIScanner(prerel1) |
| 45 | + scanner2 := newSimpleASCIIScanner(prerel2) |
| 46 | + |
| 47 | + for { |
| 48 | + // all components up to this point have been determined to be equal |
| 49 | + if scanner1.eof() { |
| 50 | + if scanner2.eof() { |
| 51 | + return 0 |
| 52 | + } |
| 53 | + return -1 // x.y is always less than x.y.z |
| 54 | + } else { |
| 55 | + if scanner2.eof() { |
| 56 | + return 1 |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + identifier1, _ := scanner1.readUntil(dotTerminator) |
| 61 | + identifier2, _ := scanner2.readUntil(dotTerminator) |
| 62 | + |
| 63 | + // each sub-identifier is compared numerically if both are numeric; if both are non-numeric, |
| 64 | + // they're compared as strings; otherwise, the numeric one is the lesser one |
| 65 | + var n1, n2, d int |
| 66 | + var isNum1, isNum2 bool |
| 67 | + n1, isNum1 = parsePositiveNumericString(identifier1) |
| 68 | + n2, isNum2 = parsePositiveNumericString(identifier2) |
| 69 | + if isNum1 && isNum2 { |
| 70 | + if n1 < n2 { |
| 71 | + d = -1 |
| 72 | + } else if n1 > n2 { |
| 73 | + d = 1 |
| 74 | + } |
| 75 | + } else { |
| 76 | + if isNum1 { |
| 77 | + d = -1 |
| 78 | + } else if isNum2 { |
| 79 | + d = 1 |
| 80 | + } else { // string comparison |
| 81 | + if identifier1 < identifier2 { |
| 82 | + d = -1 |
| 83 | + } else if identifier1 > identifier2 { |
| 84 | + d = 1 |
| 85 | + } |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + if d != 0 { |
| 90 | + return d |
| 91 | + } |
| 92 | + } |
| 93 | +} |
0 commit comments