@@ -115,87 +115,85 @@ func (v *Version) CompareTo(u *Version) int {
115
115
// comparing each of these identifiers from left to right as follows: Major, minor,
116
116
// and patch versions are always compared numerically.
117
117
// Example: 1.0.0 < 2.0.0 < 2.1.0 < 2.1.1.
118
- vMajorValue := zero [:]
118
+ vIdx := 0
119
+ uIdx := 0
119
120
vMajor := v .major
120
- if vMajor > 0 {
121
- vMajorValue = v .bytes [:vMajor ]
122
- }
123
- uMajorValue := zero [:]
124
121
uMajor := u .major
125
- if uMajor > 0 {
126
- uMajorValue = u .bytes [:uMajor ]
127
- }
128
122
{
129
- la := len ( vMajorValue )
130
- lb := len ( uMajorValue )
131
- if la == lb {
132
- for i := range vMajorValue {
133
- if vMajorValue [ i ] == uMajorValue [ i ] {
123
+ if vMajor == uMajor {
124
+ for vIdx < vMajor {
125
+ if v . bytes [ vIdx ] == u . bytes [ uIdx ] {
126
+ vIdx ++
127
+ uIdx ++
134
128
continue
135
129
}
136
- if vMajorValue [ i ] > uMajorValue [ i ] {
130
+ if v . bytes [ vIdx ] > u . bytes [ uIdx ] {
137
131
return 1
138
132
}
139
133
return - 1
140
134
}
141
- } else if la > lb {
135
+ } else if vMajor == 0 && u .bytes [uIdx ] == '0' {
136
+ return 0
137
+ } else if uMajor == 0 && v .bytes [vIdx ] == '0' {
138
+ return 0
139
+ } else if vMajor > uMajor {
142
140
return 1
143
141
} else {
144
142
return - 1
145
143
}
146
144
}
147
- vMinorValue := zero [:]
148
145
vMinor := v .minor
149
- if vMinor > vMajor {
150
- vMinorValue = v .bytes [vMajor + 1 : vMinor ]
151
- }
152
- uMinorValue := zero [:]
153
146
uMinor := u .minor
154
- if uMinor > uMajor {
155
- uMinorValue = u .bytes [uMajor + 1 : uMinor ]
156
- }
147
+ vIdx = vMajor + 1
148
+ uIdx = uMajor + 1
157
149
{
158
- la := len ( vMinorValue )
159
- lb := len ( uMinorValue )
150
+ la := vMinor - vMajor - 1
151
+ lb := uMinor - uMajor - 1
160
152
if la == lb {
161
- for i := range vMinorValue {
162
- if vMinorValue [i ] == uMinorValue [i ] {
153
+ for vIdx < vMinor {
154
+ if v .bytes [vIdx ] == u .bytes [uIdx ] {
155
+ vIdx ++
156
+ uIdx ++
163
157
continue
164
158
}
165
- if vMinorValue [ i ] > uMinorValue [ i ] {
159
+ if v . bytes [ vIdx ] > u . bytes [ uIdx ] {
166
160
return 1
167
161
}
168
162
return - 1
169
163
}
164
+ } else if vMinor == vMajor && u .bytes [uIdx ] == '0' {
165
+ return 0
166
+ } else if uMinor == uMajor && v .bytes [vIdx ] == '0' {
167
+ return 0
170
168
} else if la > lb {
171
169
return 1
172
170
} else {
173
171
return - 1
174
172
}
175
173
}
176
- vPatchValue := zero [:]
177
174
vPatch := v .patch
178
- if vPatch > vMinor {
179
- vPatchValue = v .bytes [vMinor + 1 : vPatch ]
180
- }
181
- uPatchValue := zero [:]
182
175
uPatch := u .patch
183
- if uPatch > uMinor {
184
- uPatchValue = u .bytes [uMinor + 1 : uPatch ]
185
- }
176
+ vIdx = vMinor + 1
177
+ uIdx = uMinor + 1
186
178
{
187
- la := len ( vPatchValue )
188
- lb := len ( uPatchValue )
179
+ la := vPatch - vMinor - 1
180
+ lb := uPatch - uMinor - 1
189
181
if la == lb {
190
- for i := range vPatchValue {
191
- if vPatchValue [i ] == uPatchValue [i ] {
182
+ for vIdx < vPatch {
183
+ if v .bytes [vIdx ] == u .bytes [uIdx ] {
184
+ vIdx ++
185
+ uIdx ++
192
186
continue
193
187
}
194
- if vPatchValue [ i ] > uPatchValue [ i ] {
188
+ if v . bytes [ vIdx ] > u . bytes [ uIdx ] {
195
189
return 1
196
190
}
197
191
return - 1
198
192
}
193
+ } else if vPatch == vMinor && u .bytes [uIdx ] == '0' {
194
+ return 0
195
+ } else if uPatch == uMinor && v .bytes [vIdx ] == '0' {
196
+ return 0
199
197
} else if la > lb {
200
198
return 1
201
199
} else {
@@ -231,8 +229,8 @@ func (v *Version) CompareTo(u *Version) int {
231
229
// if all of the preceding identifiers are equal.
232
230
// Example: 1.0.0-alpha < 1.0.0-alpha.1 < 1.0.0-alpha.beta < 1.0.0-beta <
233
231
// < 1.0.0-beta.2 < 1.0.0-beta.11 < 1.0.0-rc.1 < 1.0.0.
234
- vIdx := v . patch + 1
235
- uIdx := u . patch + 1
232
+ vIdx = vPatch + 1
233
+ uIdx = uPatch + 1
236
234
vLast := v .prerelease
237
235
uLast := u .prerelease
238
236
vIsAlpha := false
0 commit comments