Skip to content

Commit

Permalink
Merge pull request #10 from lestrrat-go/topic/issue-9
Browse files Browse the repository at this point in the history
Fix issue with optimization on verbatim value
  • Loading branch information
lestrrat authored Dec 30, 2019
2 parents 76be872 + 7464b8b commit 6b63b6d
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 5 deletions.
34 changes: 33 additions & 1 deletion appenders.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package strftime

import (
"bytes"
"fmt"
"io"
"strconv"
"strings"
"time"
Expand Down Expand Up @@ -61,7 +64,7 @@ type Appender interface {
Append([]byte, time.Time) []byte
}

// AppendFunc is an utility type to allow users to create a
// AppendFunc is an utility type to allow users to create a
// function-only version of an Appender
type AppendFunc func([]byte, time.Time) []byte

Expand All @@ -71,6 +74,27 @@ func (af AppendFunc) Append(b []byte, t time.Time) []byte {

type appenderList []Appender

type dumper interface {
dump(io.Writer)
}

func (l appenderList) dump(out io.Writer) {
var buf bytes.Buffer
ll := len(l)
for i, a := range l {
if dumper, ok := a.(dumper); ok {
dumper.dump(&buf)
} else {
fmt.Fprintf(&buf, "%#v", a)
}

if i < ll-1 {
fmt.Fprintf(&buf, ",\n")
}
}
buf.WriteTo(out)
}

// does the time.Format thing
type stdlibFormat struct {
s string
Expand Down Expand Up @@ -104,6 +128,10 @@ func (v stdlibFormat) combine(w combiner) Appender {
return StdlibFormat(v.s + w.str())
}

func (v stdlibFormat) dump(out io.Writer) {
fmt.Fprintf(out, "stdlib: %s", v.s)
}

type verbatimw struct {
s string
}
Expand Down Expand Up @@ -135,6 +163,10 @@ func (v verbatimw) str() string {
return v.s
}

func (v verbatimw) dump(out io.Writer) {
fmt.Fprintf(out, "verbatim: %s", v.s)
}

// These words below, as well as any decimal character
var combineExclusion = []string{
"Mon",
Expand Down
2 changes: 1 addition & 1 deletion specifications.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,14 +72,14 @@ func populateDefaultSpecifications(ds SpecificationSet) {
ds.Set('a', abbrvWeekDayName)
ds.Set('B', fullMonthName)
ds.Set('b', abbrvMonthName)
ds.Set('h', abbrvMonthName)
ds.Set('C', centuryDecimal)
ds.Set('c', timeAndDate)
ds.Set('D', mdy)
ds.Set('d', dayOfMonthZeroPad)
ds.Set('e', dayOfMonthSpacePad)
ds.Set('F', ymd)
ds.Set('H', twentyFourHourClockZeroPad)
ds.Set('h', abbrvMonthName)
ds.Set('I', twelveHourClockZeroPad)
ds.Set('j', dayOfYear)
ds.Set('k', twentyFourHourClockSpacePad)
Expand Down
14 changes: 11 additions & 3 deletions strftime.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,11 @@ func (ae *appenderExecutor) handle(a Appender) {
}

func compile(handler compileHandler, p string, ds SpecificationSet) error {
// This is a really tight loop, so we don't even calls to
// Verbatim() to cuase extra stuff
var verbatim verbatimw
for l := len(p); l > 0; l = len(p) {
// This is a really tight loop, so we don't even calls to
// Verbatim() to cuase extra stuff
var verbatim verbatimw

i := strings.IndexByte(p, '%')
if i < 0 {
verbatim.s = p
Expand Down Expand Up @@ -182,6 +183,13 @@ func (f *Strftime) Format(dst io.Writer, t time.Time) error {
return nil
}

// 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.
func (f *Strftime) Dump(out io.Writer) {
f.compiled.dump(out)
}

func (f *Strftime) format(b []byte, t time.Time) []byte {
for _, w := range f.compiled {
b = w.Append(b, t)
Expand Down
14 changes: 14 additions & 0 deletions strftime_test.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package strftime_test

import (
"bytes"
"fmt"
"os"
"strings"
"testing"
"time"

Expand Down Expand Up @@ -213,3 +215,15 @@ func Example_CustomSpecifications() {
// Daisuke Maki
// Daisuke Maki
}

func TestGHIssue9(t *testing.T) {
pattern, _ := strftime.New("/longer/path/to/see/where/this/leads//test99%Y%m%d.log")

var buf bytes.Buffer
pattern.Format(&buf, time.Now())

if !assert.True(t, strings.Contains(buf.String(), "/")) {
t.Logf("---> %s", buf.String())
return
}
}

0 comments on commit 6b63b6d

Please sign in to comment.