forked from lestrrat-go/strftime
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstrftime.go
227 lines (197 loc) · 5.63 KB
/
strftime.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
package strftime
import (
"errors"
"io"
"strings"
"sync"
"time"
)
type compileHandler interface {
handle(Appender)
}
// compile, and create an appender list
type appenderListBuilder struct {
list *combiningAppend
}
func (alb *appenderListBuilder) handle(a Appender) {
alb.list.Append(a)
}
// compile, and execute the appenders on the fly
type appenderExecutor struct {
t time.Time
dst []byte
}
func (ae *appenderExecutor) handle(a Appender) {
ae.dst = a.Append(ae.dst, ae.t)
}
func compile(handler compileHandler, p string, ds SpecificationSet) error {
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
handler.handle(&verbatim)
// this is silly, but I don't trust break keywords when there's a
// possibility of this piece of code being rearranged
p = p[l:]
continue
}
if i == l-1 {
return errors.New(`stray % at the end of pattern`)
}
// we found a '%'. we need the next byte to decide what to do next
// we already know that i < l - 1
// everything up to the i is verbatim
if i > 0 {
verbatim.s = p[:i]
handler.handle(&verbatim)
p = p[i:]
}
specification, err := ds.Lookup(p[1])
if err != nil {
return errors.New(`pattern compilation failed: ` + err.Error())
}
handler.handle(specification)
p = p[2:]
}
return nil
}
func getSpecificationSetFor(options ...Option) (SpecificationSet, error) {
var ds SpecificationSet = defaultSpecificationSet
var extraSpecifications []*optSpecificationPair
for _, option := range options {
switch option.Name() {
case optSpecificationSet:
ds = option.Value().(SpecificationSet)
case optSpecification:
extraSpecifications = append(extraSpecifications, option.Value().(*optSpecificationPair))
}
}
if len(extraSpecifications) > 0 {
// If ds is immutable, we're going to need to create a new
// one. oh what a waste!
if raw, ok := ds.(*specificationSet); ok && !raw.mutable {
ds = NewSpecificationSet()
}
for _, v := range extraSpecifications {
if err := ds.Set(v.name, v.appender); err != nil {
return nil, err
}
}
}
return ds, nil
}
var fmtAppendExecutorPool = sync.Pool{
New: func() interface{} {
var h appenderExecutor
h.dst = make([]byte, 0, 32)
return &h
},
}
func getFmtAppendExecutor() *appenderExecutor {
return fmtAppendExecutorPool.Get().(*appenderExecutor)
}
func releasdeFmtAppendExecutor(v *appenderExecutor) {
// TODO: should we discard the buffer if it's too long?
v.dst = v.dst[:0]
fmtAppendExecutorPool.Put(v)
}
// Format takes the format `s` and the time `t` to produce the
// format date/time. Note that this function re-compiles the
// pattern every time it is called.
//
// If you know beforehand that you will be reusing the pattern
// within your application, consider creating a `Strftime` object
// and reusing it.
func Format(p string, t time.Time, options ...Option) (string, error) {
// TODO: this may be premature optimization
ds, err := getSpecificationSetFor(options...)
if err != nil {
return "", errors.New(`failed to get specification set: ` + err.Error())
}
h := getFmtAppendExecutor()
defer releasdeFmtAppendExecutor(h)
h.t = t
if err := compile(h, p, ds); err != nil {
return "", errors.New(`failed to compile format: ` + err.Error())
}
return string(h.dst), nil
}
// Strftime is the object that represents a compiled strftime pattern
type Strftime struct {
pattern string
compiled appenderList
}
// New creates a new Strftime object. If the compilation fails, then
// an error is returned in the second argument.
func New(p string, options ...Option) (*Strftime, error) {
// TODO: this may be premature optimization
ds, err := getSpecificationSetFor(options...)
if err != nil {
return nil, errors.New(`failed to get specification set: ` + err.Error())
}
var h appenderListBuilder
h.list = &combiningAppend{}
if err := compile(&h, p, ds); err != nil {
return nil, errors.New(`failed to compile format: ` + err.Error())
}
return &Strftime{
pattern: p,
compiled: h.list.list,
}, nil
}
// Pattern returns the original pattern string
func (f *Strftime) Pattern() string {
return f.pattern
}
// Format takes the destination `dst` and time `t`. It formats the date/time
// using the pre-compiled pattern, and outputs the results to `dst`
func (f *Strftime) Format(dst io.Writer, t time.Time) error {
const bufSize = 64
var b []byte
max := len(f.pattern) + 10
if max < bufSize {
var buf [bufSize]byte
b = buf[:0]
} else {
b = make([]byte, 0, max)
}
if _, err := dst.Write(f.format(b, t)); err != nil {
return err
}
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.
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)
}
return b
}
// FormatString takes the time `t` and formats it, returning the
// string containing the formated data.
func (f *Strftime) FormatString(t time.Time) string {
const bufSize = 64
var b []byte
max := len(f.pattern) + 10
if max < bufSize {
var buf [bufSize]byte
b = buf[:0]
} else {
b = make([]byte, 0, max)
}
return string(f.format(b, t))
}