Skip to content

Commit

Permalink
add FormatBuffer method
Browse files Browse the repository at this point in the history
  • Loading branch information
ianwilkes committed Jul 13, 2021
1 parent 09329cc commit 2cf2db9
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 3 deletions.
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 2cf2db9

Please sign in to comment.