Compare compares this version to another version. This returns -1, 0, or 1 if this version is smaller, equal, or larger than the other version, respectively. If you want boolean results, use the LessThan, Equal, GreaterThan, GreaterThanOrEqual or LessThanOrEqual methods.
(other *Version)
| 112 | // If you want boolean results, use the LessThan, Equal, |
| 113 | // GreaterThan, GreaterThanOrEqual or LessThanOrEqual methods. |
| 114 | func (v *Version) Compare(other *Version) int { |
| 115 | // A quick, efficient equality check |
| 116 | if v.String() == other.String() { |
| 117 | return 0 |
| 118 | } |
| 119 | |
| 120 | segmentsSelf := v.Segments64() |
| 121 | segmentsOther := other.Segments64() |
| 122 | |
| 123 | // If the segments are the same, we must compare on prerelease info |
| 124 | if reflect.DeepEqual(segmentsSelf, segmentsOther) { |
| 125 | preSelf := v.Prerelease() |
| 126 | preOther := other.Prerelease() |
| 127 | if preSelf == "" && preOther == "" { |
| 128 | return 0 |
| 129 | } |
| 130 | if preSelf == "" { |
| 131 | return 1 |
| 132 | } |
| 133 | if preOther == "" { |
| 134 | return -1 |
| 135 | } |
| 136 | |
| 137 | return comparePrereleases(preSelf, preOther) |
| 138 | } |
| 139 | |
| 140 | // Get the highest specificity (hS), or if they're equal, just use segmentSelf length |
| 141 | lenSelf := len(segmentsSelf) |
| 142 | lenOther := len(segmentsOther) |
| 143 | hS := lenSelf |
| 144 | if lenSelf < lenOther { |
| 145 | hS = lenOther |
| 146 | } |
| 147 | // Compare the segments |
| 148 | // Because a constraint could have more/less specificity than the version it's |
| 149 | // checking, we need to account for a lopsided or jagged comparison |
| 150 | for i := 0; i < hS; i++ { |
| 151 | if i > lenSelf-1 { |
| 152 | // This means Self had the lower specificity |
| 153 | // Check to see if the remaining segments in Other are all zeros |
| 154 | if !allZero(segmentsOther[i:]) { |
| 155 | // if not, it means that Other has to be greater than Self |
| 156 | return -1 |
| 157 | } |
| 158 | break |
| 159 | } else if i > lenOther-1 { |
| 160 | // this means Other had the lower specificity |
| 161 | // Check to see if the remaining segments in Self are all zeros - |
| 162 | if !allZero(segmentsSelf[i:]) { |
| 163 | //if not, it means that Self has to be greater than Other |
| 164 | return 1 |
| 165 | } |
| 166 | break |
| 167 | } |
| 168 | lhs := segmentsSelf[i] |
| 169 | rhs := segmentsOther[i] |
| 170 | if lhs == rhs { |
| 171 | continue |