Skip to content

Commit

Permalink
Merge pull request #26 from ianwilkes/master
Browse files Browse the repository at this point in the history
Add FormatBuffer
  • Loading branch information
lestrrat authored Jul 14, 2021
2 parents 09329cc + 50bb69c commit 1186c65
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 8 deletions.
17 changes: 12 additions & 5 deletions bench/bench_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import (
"testing"
"time"

jehiah "github.com/jehiah/go-strftime"
fastly "github.com/fastly/go-utils/strftime"
jehiah "github.com/jehiah/go-strftime"
lestrrat "github.com/lestrrat-go/strftime"
tebeka "github.com/tebeka/strftime"
)
Expand Down Expand Up @@ -67,12 +67,19 @@ func BenchmarkLestrratCachedWriter(b *testing.B) {
b.ResetTimer()

// This benchmark does not take into effect the compilation time
// nor the buffer reset time
for i := 0; i < b.N; i++ {
b.StopTimer()
buf.Reset()
b.StartTimer()
f.Format(&buf, t)
f.FormatString(t)
}
}

func BenchmarkLestrratCachedFormatBuffer(b *testing.B) {
var t time.Time
f, _ := lestrrat.New(benchfmt)
b.ResetTimer()

var buf []byte
for i := 0; i < b.N; i++ {
buf = f.FormatBuffer(buf[:0], t)
}
}
7 changes: 7 additions & 0 deletions strftime.go
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,13 @@ func (f *Strftime) Format(dst io.Writer, t time.Time) error {
return nil
}

// FormatBuffer is equivalent to Format, but appends the result directly to
// supplied slice dst, returning the updated slice. This avoids any internal
// memory allocation.
func (f *Strftime) FormatBuffer(dst []byte, t time.Time) []byte {
return f.format(dst, t)
}

// Dump outputs the internal structure of the formatter, for debugging purposes.
// Please do NOT assume the output format to be fixed: it is expected to change
// in the future.
Expand Down
46 changes: 43 additions & 3 deletions strftime_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,20 +42,60 @@ func TestInvalid(t *testing.T) {
}
}

func TestFormat(t *testing.T) {
func TestFormatMethods(t *testing.T) {
l := envload.New()
defer l.Restore()

os.Setenv("LC_ALL", "C")

s, err := strftime.Format(`%A %a %B %b %C %c %D %d %e %F %H %h %I %j %k %l %M %m %n %p %R %r %S %T %t %U %u %V %v %W %w %X %x %Y %y %Z %z`, ref)
formatString := `%A %a %B %b %C %c %D %d %e %F %H %h %I %j %k %l %M %m %n %p %R %r %S %T %t %U %u %V %v %W %w %X %x %Y %y %Z %z`
resultString := "Monday Mon January Jan 20 Mon Jan 2 22:04:05 2006 01/02/06 02 2 2006-01-02 22 Jan 10 002 22 10 04 01 \n PM 22:04 10:04:05 PM 05 22:04:05 \t 01 1 01 2-Jan-2006 01 1 22:04:05 01/02/06 2006 06 UTC +0000"

s, err := strftime.Format(formatString, ref)
if !assert.NoError(t, err, `strftime.Format succeeds`) {
return
}

if !assert.Equal(t, "Monday Mon January Jan 20 Mon Jan 2 22:04:05 2006 01/02/06 02 2 2006-01-02 22 Jan 10 002 22 10 04 01 \n PM 22:04 10:04:05 PM 05 22:04:05 \t 01 1 01 2-Jan-2006 01 1 22:04:05 01/02/06 2006 06 UTC +0000", s, `formatted result matches`) {
if !assert.Equal(t, resultString, s, `formatted result matches`) {
return
}

formatter, err := strftime.New(formatString)
if !assert.NoError(t, err, `strftime.New succeeds`) {
return
}

if !assert.Equal(t, resultString, formatter.FormatString(ref), `formatted result matches`) {
return
}

var buf bytes.Buffer
err = formatter.Format(&buf, ref)
if !assert.NoError(t, err, `Format method succeeds`) {
return
}
if !assert.Equal(t, resultString, buf.String(), `formatted result matches`) {
return
}

var dst []byte
dst = formatter.FormatBuffer(dst, ref)
if !assert.Equal(t, resultString, string(dst), `formatted result matches`) {
return
}

dst = []byte("nonsense")
dst = formatter.FormatBuffer(dst[:0], ref)
if !assert.Equal(t, resultString, string(dst), `overwritten result matches`) {
return
}

dst = []byte("nonsense")
dst = formatter.FormatBuffer(dst, ref)
if !assert.Equal(t, "nonsense"+resultString, string(dst), `appended result matches`) {
return
}

}

func TestFormatBlanks(t *testing.T) {
Expand Down

0 comments on commit 1186c65

Please sign in to comment.