forked from elastic/beats
-
Notifications
You must be signed in to change notification settings - Fork 0
/
elems.go
175 lines (143 loc) · 3.12 KB
/
elems.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
package dtfmt
import (
"errors"
"fmt"
"unicode/utf8"
)
type element interface {
requires(c *ctxConfig) error
estimateSize() int
compile() (prog, error)
}
type runeLiteral struct {
r rune
}
type stringLiteral struct {
s []byte
}
type unpaddedNumber struct {
ft fieldType
maxDigits int
signed bool
}
type paddedNumber struct {
ft fieldType
minDigits, maxDigits int
signed bool
}
type textField struct {
ft fieldType
short bool
}
type twoDigitYear struct {
ft fieldType
}
func (runeLiteral) requires(*ctxConfig) error { return nil }
func (runeLiteral) estimateSize() int { return 1 }
func (stringLiteral) requires(*ctxConfig) error { return nil }
func (s stringLiteral) estimateSize() int { return len(s.s) }
func (n unpaddedNumber) requires(c *ctxConfig) error {
return numRequires(c, n.ft)
}
func (n unpaddedNumber) estimateSize() int {
return numSize(n.maxDigits, n.signed)
}
func (n paddedNumber) requires(c *ctxConfig) error {
return numRequires(c, n.ft)
}
func (n paddedNumber) estimateSize() int {
return numSize(n.maxDigits, n.signed)
}
func (n twoDigitYear) requires(c *ctxConfig) error {
return numRequires(c, n.ft)
}
func (twoDigitYear) estimateSize() int { return 2 }
func numSize(digits int, signed bool) int {
if signed {
return digits + 1
}
return digits
}
func numRequires(c *ctxConfig, ft fieldType) error {
switch ft {
case ftYear, ftMonthOfYear, ftDayOfMonth:
c.enableDate()
case ftWeekyear, ftWeekOfWeekyear:
c.enableISO()
case ftDayOfYear:
c.enableYearday()
case ftDayOfWeek:
c.enableWeekday()
case ftHalfdayOfDay,
ftHourOfHalfday,
ftClockhourOfHalfday,
ftClockhourOfDay,
ftHourOfDay,
ftMinuteOfDay,
ftMinuteOfHour,
ftSecondOfDay,
ftSecondOfMinute,
ftMillisOfDay,
ftMillisOfSecond:
c.enableClock()
}
return nil
}
func (f textField) requires(c *ctxConfig) error {
switch f.ft {
case ftHalfdayOfDay:
c.enableClock()
case ftMonthOfYear:
c.enableDate()
case ftDayOfWeek:
c.enableWeekday()
default:
return fmt.Errorf("time field %v not supported by text", f.ft)
}
return nil
}
func (f textField) estimateSize() int {
switch f.ft {
case ftHalfdayOfDay:
return 2
case ftDayOfWeek:
if f.short {
return 3
}
return 9 // max(weekday) = len(Wednesday)
case ftMonthOfYear:
if f.short {
return 6
}
return 9 // max(month) = len(September)
default:
return 0
}
}
func (r runeLiteral) compile() (prog, error) {
switch utf8.RuneLen(r.r) {
case -1:
return prog{}, errors.New("invalid rune")
}
var tmp [8]byte
l := utf8.EncodeRune(tmp[:], r.r)
return makeCopy(tmp[:l])
}
func (s stringLiteral) compile() (prog, error) {
return makeCopy([]byte(s.s))
}
func (n unpaddedNumber) compile() (prog, error) {
return makeProg(opNum, byte(n.ft))
}
func (n paddedNumber) compile() (prog, error) {
return makeProg(opNumPadded, byte(n.ft), byte(n.maxDigits))
}
func (n twoDigitYear) compile() (prog, error) {
return makeProg(opTwoDigit, byte(n.ft))
}
func (f textField) compile() (prog, error) {
if f.short {
return makeProg(opTextShort, byte(f.ft))
}
return makeProg(opTextLong, byte(f.ft))
}