Skip to content

Commit 6b63b6d

Browse files
authored
Merge pull request #10 from lestrrat-go/topic/issue-9
Fix issue with optimization on verbatim value
2 parents 76be872 + 7464b8b commit 6b63b6d

File tree

4 files changed

+59
-5
lines changed

4 files changed

+59
-5
lines changed

appenders.go

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
package strftime
22

33
import (
4+
"bytes"
5+
"fmt"
6+
"io"
47
"strconv"
58
"strings"
69
"time"
@@ -61,7 +64,7 @@ type Appender interface {
6164
Append([]byte, time.Time) []byte
6265
}
6366

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

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

7275
type appenderList []Appender
7376

77+
type dumper interface {
78+
dump(io.Writer)
79+
}
80+
81+
func (l appenderList) dump(out io.Writer) {
82+
var buf bytes.Buffer
83+
ll := len(l)
84+
for i, a := range l {
85+
if dumper, ok := a.(dumper); ok {
86+
dumper.dump(&buf)
87+
} else {
88+
fmt.Fprintf(&buf, "%#v", a)
89+
}
90+
91+
if i < ll-1 {
92+
fmt.Fprintf(&buf, ",\n")
93+
}
94+
}
95+
buf.WriteTo(out)
96+
}
97+
7498
// does the time.Format thing
7599
type stdlibFormat struct {
76100
s string
@@ -104,6 +128,10 @@ func (v stdlibFormat) combine(w combiner) Appender {
104128
return StdlibFormat(v.s + w.str())
105129
}
106130

131+
func (v stdlibFormat) dump(out io.Writer) {
132+
fmt.Fprintf(out, "stdlib: %s", v.s)
133+
}
134+
107135
type verbatimw struct {
108136
s string
109137
}
@@ -135,6 +163,10 @@ func (v verbatimw) str() string {
135163
return v.s
136164
}
137165

166+
func (v verbatimw) dump(out io.Writer) {
167+
fmt.Fprintf(out, "verbatim: %s", v.s)
168+
}
169+
138170
// These words below, as well as any decimal character
139171
var combineExclusion = []string{
140172
"Mon",

specifications.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,14 +72,14 @@ func populateDefaultSpecifications(ds SpecificationSet) {
7272
ds.Set('a', abbrvWeekDayName)
7373
ds.Set('B', fullMonthName)
7474
ds.Set('b', abbrvMonthName)
75-
ds.Set('h', abbrvMonthName)
7675
ds.Set('C', centuryDecimal)
7776
ds.Set('c', timeAndDate)
7877
ds.Set('D', mdy)
7978
ds.Set('d', dayOfMonthZeroPad)
8079
ds.Set('e', dayOfMonthSpacePad)
8180
ds.Set('F', ymd)
8281
ds.Set('H', twentyFourHourClockZeroPad)
82+
ds.Set('h', abbrvMonthName)
8383
ds.Set('I', twelveHourClockZeroPad)
8484
ds.Set('j', dayOfYear)
8585
ds.Set('k', twentyFourHourClockSpacePad)

strftime.go

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,11 @@ func (ae *appenderExecutor) handle(a Appender) {
3333
}
3434

3535
func compile(handler compileHandler, p string, ds SpecificationSet) error {
36-
// This is a really tight loop, so we don't even calls to
37-
// Verbatim() to cuase extra stuff
38-
var verbatim verbatimw
3936
for l := len(p); l > 0; l = len(p) {
37+
// This is a really tight loop, so we don't even calls to
38+
// Verbatim() to cuase extra stuff
39+
var verbatim verbatimw
40+
4041
i := strings.IndexByte(p, '%')
4142
if i < 0 {
4243
verbatim.s = p
@@ -182,6 +183,13 @@ func (f *Strftime) Format(dst io.Writer, t time.Time) error {
182183
return nil
183184
}
184185

186+
// Dump outputs the internal structure of the formatter, for debugging purposes.
187+
// Please do NOT assume the output format to be fixed: it is expected to change
188+
// in the future.
189+
func (f *Strftime) Dump(out io.Writer) {
190+
f.compiled.dump(out)
191+
}
192+
185193
func (f *Strftime) format(b []byte, t time.Time) []byte {
186194
for _, w := range f.compiled {
187195
b = w.Append(b, t)

strftime_test.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
package strftime_test
22

33
import (
4+
"bytes"
45
"fmt"
56
"os"
7+
"strings"
68
"testing"
79
"time"
810

@@ -213,3 +215,15 @@ func Example_CustomSpecifications() {
213215
// Daisuke Maki
214216
// Daisuke Maki
215217
}
218+
219+
func TestGHIssue9(t *testing.T) {
220+
pattern, _ := strftime.New("/longer/path/to/see/where/this/leads//test99%Y%m%d.log")
221+
222+
var buf bytes.Buffer
223+
pattern.Format(&buf, time.Now())
224+
225+
if !assert.True(t, strings.Contains(buf.String(), "/")) {
226+
t.Logf("---> %s", buf.String())
227+
return
228+
}
229+
}

0 commit comments

Comments
 (0)