Repository navigation
Expand file tree
/
Copy pathstrftime.go
More file actions
317 lines (279 loc) · 8.13 KB
/
Copy pathstrftime.go
File metadata and controls
317 lines (279 loc) · 8.13 KB
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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
package strftime
import (
"errors"
"fmt"
"io"
"strings"
"sync"
"sync/atomic"
"time"
)
type compileHandler interface {
handleVerbatim(string)
handleSpec(Appender)
}
// compile, and create an appender list
type appenderListBuilder struct {
list *combiningAppend
}
func (alb *appenderListBuilder) handleVerbatim(s string) {
alb.list.Append(Verbatim(s))
}
func (alb *appenderListBuilder) handleSpec(a Appender) {
alb.list.Append(a)
}
// compile, and execute the appenders on the fly
type appenderExecutor struct {
t time.Time
dst []byte
}
// handleVerbatim appends the static text directly, avoiding the heap
// allocation that boxing it into a verbatimw Appender would incur on
// this per-call compile path.
func (ae *appenderExecutor) handleVerbatim(s string) {
ae.dst = append(ae.dst, s...)
}
func (ae *appenderExecutor) handleSpec(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) {
i := strings.IndexByte(p, '%')
if i < 0 {
handler.handleVerbatim(p)
// 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 {
handler.handleVerbatim(p[:i])
p = p[i:]
}
// An optional '-' (glibc) or '#' (Windows) flag between the '%' and
// the conversion specifier suppresses padding on numeric fields.
specIdx := 1
var noPad bool
if c := p[1]; c == '-' || c == '#' {
if len(p) < 3 {
return errors.New(`stray % at the end of pattern`)
}
noPad = true
specIdx = 2
}
specification, err := ds.Lookup(p[specIdx])
if err != nil {
return fmt.Errorf("pattern compilation failed: %w", err)
}
if noPad && !isStandardNonNumericSpecification(p[specIdx]) {
specification = unpadded{inner: specification}
}
handler.handleSpec(specification)
p = p[specIdx+1:]
}
return nil
}
func getSpecificationSetFor(options ...Option) (SpecificationSet, error) {
ds := defaultSpecificationSet
var extraSpecifications []*optSpecificationPair
var locale Locale
for _, option := range options {
switch option.Name() {
case optSpecificationSet:
if v, ok := option.Value().(SpecificationSet); ok {
ds = v
}
case optSpecification:
if v, ok := option.Value().(*optSpecificationPair); ok {
extraSpecifications = append(extraSpecifications, v)
}
case optLocale:
if v, ok := option.Value().(Locale); ok {
locale = v
}
}
}
if locale != nil || 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()
}
// Apply the locale first so an explicit WithSpecification can still
// override an individual specifier.
if locale != nil {
if err := applyLocale(ds, locale); err != nil {
return nil, err
}
}
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() any {
var h appenderExecutor
h.dst = make([]byte, 0, 32)
return &h
},
}
func getFmtAppendExecutor() *appenderExecutor {
e, _ := fmtAppendExecutorPool.Get().(*appenderExecutor)
return e
}
func releasdeFmtAppendExecutor(v *appenderExecutor) {
// TODO: should we discard the buffer if it's too long?
v.dst = v.dst[:0]
fmtAppendExecutorPool.Put(v)
}
// formatCacheLimit caps the number of distinct patterns Format will keep
// compiled. The bound keeps memory usage predictable even when patterns are
// derived from untrusted input; once it is reached, additional patterns are
// formatted on the fly without being cached.
const formatCacheLimit = 1024
var (
formatCache sync.Map // pattern string -> *Strftime
formatCacheLen atomic.Int64
)
// cachedStrftime returns a compiled Strftime for the default specification
// set, reusing a previously compiled one when possible. The boolean result is
// false (with no error) when the cache is full and the pattern was not already
// cached, so the caller can fall back to compiling on the fly.
func cachedStrftime(p string) (*Strftime, bool, error) {
if v, ok := formatCache.Load(p); ok {
f, _ := v.(*Strftime)
return f, true, nil
}
if formatCacheLen.Load() >= formatCacheLimit {
return nil, false, nil
}
f, err := New(p)
if err != nil {
return nil, false, err
}
if actual, loaded := formatCache.LoadOrStore(p, f); loaded {
cached, _ := actual.(*Strftime)
return cached, true, nil
}
formatCacheLen.Add(1)
return f, true, nil
}
// Format takes the format `s` and the time `t` to produce the
// format date/time.
//
// When called without options, compiled patterns are cached (up to an
// internal limit) so that repeated calls with the same pattern avoid
// recompilation. Calls that pass options always compile on the fly.
//
// 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) {
if len(options) == 0 {
f, ok, err := cachedStrftime(p)
if err != nil {
return "", fmt.Errorf("failed to compile format: %w", err)
}
if ok {
return f.FormatString(t), nil
}
// cache is full: fall through and format on the fly
}
ds, err := getSpecificationSetFor(options...)
if err != nil {
return "", fmt.Errorf("failed to get specification set: %w", err)
}
h := getFmtAppendExecutor()
defer releasdeFmtAppendExecutor(h)
h.t = t
if err := compile(h, p, ds); err != nil {
return "", fmt.Errorf("failed to compile format: %w", err)
}
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, fmt.Errorf("failed to get specification set: %w", err)
}
var h appenderListBuilder
h.list = &combiningAppend{}
if err := compile(&h, p, ds); err != nil {
return nil, fmt.Errorf("failed to compile format: %w", err)
}
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
bufLen := len(f.pattern) + 10
if bufLen < bufSize {
var buf [bufSize]byte
b = buf[:0]
} else {
b = make([]byte, 0, bufLen)
}
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
bufLen := len(f.pattern) + 10
if bufLen < bufSize {
var buf [bufSize]byte
b = buf[:0]
} else {
b = make([]byte, 0, bufLen)
}
return string(f.format(b, t))
}