MCPcopy Index your code
hub / github.com/devspace-sh/devspace / Compare

Method Compare

pkg/util/constraint/version.go:114–181  ·  view source on GitHub ↗

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)

Source from the content-addressed store, hash-verified

112// If you want boolean results, use the LessThan, Equal,
113// GreaterThan, GreaterThanOrEqual or LessThanOrEqual methods.
114func (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

Callers 13

TestVersionCompareFunction · 0.95
TestComparePreReleasesFunction · 0.95
EqualMethod · 0.95
GreaterThanMethod · 0.95
GreaterThanOrEqualMethod · 0.95
LessThanMethod · 0.95
LessThanOrEqualMethod · 0.95
NewerVersionAvailableFunction · 0.80
constraintGreaterThanFunction · 0.80
constraintLessThanFunction · 0.80

Calls 5

StringMethod · 0.95
Segments64Method · 0.95
PrereleaseMethod · 0.95
comparePrereleasesFunction · 0.85
allZeroFunction · 0.85

Tested by 3

TestVersionCompareFunction · 0.76
TestComparePreReleasesFunction · 0.76