Skip to content

Commit 4907abe

Browse files
committed
Inlined some function calls
1 parent 97e6bec commit 4907abe

File tree

3 files changed

+128
-45
lines changed

3 files changed

+128
-45
lines changed

benchmark_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,6 @@ func BenchmarkVersionComparator(b *testing.B) {
112112
// Results for v0.11.0:
113113
// BenchmarkVersionComparator-12 74793 17347 ns/op 0 B/op 0 allocs/op
114114

115-
// Results for v0.12.0: :-(
116-
// BenchmarkVersionComparator-12 66262 18340 ns/op 0 B/op 0 allocs/op
115+
// Results for v0.12.0: :-|
116+
// BenchmarkVersionComparator-12 74320 16223 ns/op 0 B/op 0 allocs/op
117117
}

version.go

Lines changed: 121 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ func (v *Version) Normalize() {
7070
}
7171
}
7272

73-
func compareNumberRelaxed(a, b string) int {
73+
func compareNumber(a, b string) int {
7474
la := len(a)
7575
lb := len(b)
7676
if la == lb {
@@ -85,28 +85,10 @@ func compareNumberRelaxed(a, b string) int {
8585
}
8686
return 0
8787
}
88-
89-
// la != lb...
90-
91-
// in relaxed semver, a missing number is considered 0
92-
if la == 0 {
93-
if b[0] == '0' {
94-
return 0 // ("","0")
95-
}
96-
return -1 // ("","N")
97-
}
98-
if lb == 0 {
99-
if a[0] == '0' {
100-
return 0 // ("0","")
101-
}
102-
return 1 // ("N","")
103-
}
104-
105-
// both a and b are not empty
10688
if la > lb {
107-
return 1 // ("NNN","N")
89+
return 1
10890
}
109-
return -1 // ("N","NNN")
91+
return -1
11092
}
11193

11294
func compareAlpha(a, b []byte) int {
@@ -119,6 +101,8 @@ func compareAlpha(a, b []byte) int {
119101
return 0
120102
}
121103

104+
var zero = "0"
105+
122106
// CompareTo compares the Version with the one passed as parameter.
123107
// Returns -1, 0 or 1 if the version is respectively less than, equal
124108
// or greater than the compared Version
@@ -130,14 +114,92 @@ func (v *Version) CompareTo(u *Version) int {
130114
// comparing each of these identifiers from left to right as follows: Major, minor,
131115
// and patch versions are always compared numerically.
132116
// Example: 1.0.0 < 2.0.0 < 2.1.0 < 2.1.1.
133-
if cmp := compareNumberRelaxed(v.majorString(), u.majorString()); cmp != 0 {
134-
return cmp
117+
vMajor := zero[:]
118+
if v.major > 0 {
119+
vMajor = v.raw[:v.major]
120+
}
121+
uMajor := zero[:]
122+
if u.major > 0 {
123+
uMajor = u.raw[:u.major]
124+
}
125+
{
126+
la := len(vMajor)
127+
lb := len(uMajor)
128+
if la == lb {
129+
for i := range vMajor {
130+
if vMajor[i] == uMajor[i] {
131+
continue
132+
}
133+
if vMajor[i] > uMajor[i] {
134+
return 1
135+
}
136+
return -1
137+
}
138+
} else if la > lb {
139+
return 1
140+
} else {
141+
return -1
142+
}
143+
}
144+
vMinor := zero[:]
145+
if v.minor > v.major {
146+
vMinor = v.raw[v.major+1 : v.minor]
147+
}
148+
uMinor := zero[:]
149+
if u.minor > u.major {
150+
uMinor = u.raw[u.major+1 : u.minor]
151+
}
152+
// if cmp := compareNumber(vMinor, uMinor); cmp != 0 {
153+
// return cmp
154+
// }
155+
{
156+
la := len(vMinor)
157+
lb := len(uMinor)
158+
if la == lb {
159+
for i := range vMinor {
160+
if vMinor[i] == uMinor[i] {
161+
continue
162+
}
163+
if vMinor[i] > uMinor[i] {
164+
return 1
165+
}
166+
return -1
167+
}
168+
} else if la > lb {
169+
return 1
170+
} else {
171+
return -1
172+
}
135173
}
136-
if cmp := compareNumberRelaxed(v.minorString(), u.minorString()); cmp != 0 {
137-
return cmp
174+
vPatch := zero[:]
175+
if v.patch > v.minor {
176+
vPatch = v.raw[v.minor+1 : v.patch]
138177
}
139-
if cmp := compareNumberRelaxed(v.patchString(), u.patchString()); cmp != 0 {
140-
return cmp
178+
uPatch := zero[:]
179+
if u.patch > u.minor {
180+
uPatch = u.raw[u.minor+1 : u.patch]
181+
}
182+
// if cmp := compareNumber(vPatch, uPatch); cmp != 0 {
183+
// return cmp
184+
// }
185+
{
186+
la := len(vPatch)
187+
lb := len(uPatch)
188+
if la == lb {
189+
for i := range vPatch {
190+
if vPatch[i] == uPatch[i] {
191+
continue
192+
}
193+
if vPatch[i] > uPatch[i] {
194+
return 1
195+
}
196+
return -1
197+
}
198+
} else if la > lb {
199+
return 1
200+
} else {
201+
return -1
202+
}
141203
}
142204

143205
// if both versions have no pre-release, they are equal
@@ -189,7 +251,7 @@ func (v *Version) CompareTo(u *Version) int {
189251

190252
if vIdx == vLast || vCurr == '.' {
191253
if uIdx != uLast && uCurr != '.' {
192-
if !uIsAlpha && !isNumeric(uCurr) {
254+
if !uIsAlpha && !(uCurr >= '0' && uCurr <= '9') {
193255
uIsAlpha = true
194256
}
195257
uIsLonger = true
@@ -198,7 +260,7 @@ func (v *Version) CompareTo(u *Version) int {
198260
}
199261
} else if uIdx == uLast || uCurr == '.' {
200262
if vIdx != vLast && vCurr != '.' {
201-
if !vIsAlpha && !isNumeric(vCurr) {
263+
if !vIsAlpha && !(vCurr >= '0' && vCurr <= '9') {
202264
vIsAlpha = true
203265
}
204266
vIsLonger = true
@@ -213,10 +275,10 @@ func (v *Version) CompareTo(u *Version) int {
213275
cmp = -1
214276
}
215277
}
216-
if !vIsAlpha && !isNumeric(vCurr) {
278+
if !vIsAlpha && !(vCurr >= '0' && vCurr <= '9') {
217279
vIsAlpha = true
218280
}
219-
if !uIsAlpha && !isNumeric(uCurr) {
281+
if !uIsAlpha && !(uCurr >= '0' && uCurr <= '9') {
220282
uIsAlpha = true
221283
}
222284
vIdx++
@@ -305,19 +367,43 @@ func (v *Version) CompatibleWith(u *Version) bool {
305367
if !u.GreaterThanOrEqual(v) {
306368
return false
307369
}
308-
majorEquals := compareNumberRelaxed(v.majorString(), u.majorString()) == 0
370+
vMajor := zero[:]
371+
if v.major > 0 {
372+
vMajor = v.raw[:v.major]
373+
}
374+
uMajor := zero[:]
375+
if u.major > 0 {
376+
uMajor = u.raw[:u.major]
377+
}
378+
majorEquals := compareNumber(vMajor, uMajor) == 0
309379
if v.major > 0 && v.raw[0] != '0' {
310380
return majorEquals
311381
}
312382
if !majorEquals {
313383
return false
314384
}
315-
minorEquals := compareNumberRelaxed(v.minorString(), u.minorString()) == 0
316-
if v.minor > v.major && v.raw[v.major+1] != '0' {
385+
vMinor := zero[:]
386+
if v.minor > v.major {
387+
vMinor = v.raw[v.major+1 : v.minor]
388+
}
389+
uMinor := zero[:]
390+
if u.minor > u.major {
391+
uMinor = u.raw[u.major+1 : u.minor]
392+
}
393+
minorEquals := compareNumber(vMinor, uMinor) == 0
394+
if vMinor[0] != '0' {
317395
return minorEquals
318396
}
319397
if !minorEquals {
320398
return false
321399
}
322-
return compareNumberRelaxed(v.patchString(), u.patchString()) == 0
400+
vPatch := zero[:]
401+
if v.patch > v.minor {
402+
vPatch = v.raw[v.minor+1 : v.patch]
403+
}
404+
uPatch := zero[:]
405+
if u.patch > u.minor {
406+
uPatch = u.raw[u.minor+1 : u.patch]
407+
}
408+
return compareNumber(vPatch, uPatch) == 0
323409
}

version_test.go

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -221,17 +221,14 @@ func TestNilVersionString(t *testing.T) {
221221

222222
func TestCompareNumbers(t *testing.T) {
223223
// ==
224-
require.Zero(t, compareNumberRelaxed("", ""))
225-
require.Zero(t, compareNumberRelaxed("0", ""))
226-
require.Zero(t, compareNumberRelaxed("", "0"))
227-
require.Zero(t, compareNumberRelaxed("0", "0"))
228-
require.Zero(t, compareNumberRelaxed("5", "5"))
229-
require.Zero(t, compareNumberRelaxed("15", "15"))
224+
require.Zero(t, compareNumber("0", "0"))
225+
require.Zero(t, compareNumber("5", "5"))
226+
require.Zero(t, compareNumber("15", "15"))
230227

231228
// >
232229
testGreater := func(a, b string) {
233-
require.Positive(t, compareNumberRelaxed(a, b), `compareNumber("%s","%s") is not positive`, a, b)
234-
require.Negative(t, compareNumberRelaxed(b, a), `compareNumber("%s","%s") is not negative`, b, a)
230+
require.Positive(t, compareNumber(a, b), `compareNumber("%s","%s") is not positive`, a, b)
231+
require.Negative(t, compareNumber(b, a), `compareNumber("%s","%s") is not negative`, b, a)
235232
}
236233
testGreater("1", "")
237234
testGreater("1", "0")

0 commit comments

Comments
 (0)