Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 16 additions & 10 deletions version.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
package version

import (
"bytes"
"database/sql/driver"
"fmt"
"regexp"
Expand Down Expand Up @@ -382,22 +381,29 @@ func (v *Version) Segments64() []int64 {
// missing parts (1.0 => 1.0.0) will be made into a canonicalized form
// as shown in the parenthesized examples.
func (v *Version) String() string {
var buf bytes.Buffer
fmtParts := make([]string, len(v.segments))
return string(v.bytes())
}

func (v *Version) bytes() []byte {
var buf []byte
for i, s := range v.segments {
// We can ignore err here since we've pre-parsed the values in segments
str := strconv.FormatInt(s, 10)
fmtParts[i] = str
if i > 0 {
buf = append(buf, '.')
}
buf = strconv.AppendInt(buf, s, 10)
}
fmt.Fprintf(&buf, "%s", strings.Join(fmtParts, "."))

if v.pre != "" {
fmt.Fprintf(&buf, "-%s", v.pre)
buf = append(buf, '-')
buf = append(buf, v.pre...)
}

if v.metadata != "" {
fmt.Fprintf(&buf, "+%s", v.metadata)
buf = append(buf, '+')
buf = append(buf, v.metadata...)
}

return buf.String()
return buf
}

// Original returns the original parsed version as-is, including any
Expand Down
21 changes: 21 additions & 0 deletions version_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -739,3 +739,24 @@ func BenchmarkVersionString(b *testing.B) {
_ = v.String()
}
}

func BenchmarkCompareVersionV1(b *testing.B) {
v, _ := NewVersion("3.4.5")

b.ResetTimer()
b.ReportAllocs()
for i := 0; i < b.N; i++ {
v.Compare(v)
}
}

func BenchmarkVersionCompareV2(b *testing.B) {
v, _ := NewVersion("1.2.3")
o, _ := NewVersion("v1.2.3.4")

b.ResetTimer()
b.ReportAllocs()
for i := 0; i < b.N; i++ {
v.Compare(o)
}
}