This repository has been archived by the owner on Aug 3, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.go
324 lines (283 loc) · 6.62 KB
/
util.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
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
318
319
320
321
322
323
324
package log
import (
"strconv"
"time"
"unicode"
"unsafe"
)
const hex = "0123456789abcdef"
func isIdent(s string) bool {
if len(s) == 0 {
return false
}
for i, c := range s {
if !isIdentRune(c, i) {
return false
}
}
return true
}
func isIdentRune(ch rune, i int) bool {
return ch == '_' || ch == '-' || ch == '.' || ch == '#' || ch == '$' || ch == '/' ||
unicode.IsLetter(ch) || unicode.IsDigit(ch)
}
// encoder used to build json with some extra features:
//
// 1. support unqutoed key
// 2. support literal complex, e.g. 1.5+2i, 2i
// 3. support literal duration, e.g. 1s, 1ms
// 4. support literal nil
// 5. support bytes starts with 0x
type encoder struct {
buf []byte
}
// String returns the accumulated string.
func (enc *encoder) String() string {
return *(*string)(unsafe.Pointer(&enc.buf))
}
// Len returns the number of accumulated bytes; enc.Len() == len(enc.String()).
func (enc *encoder) Len() int { return len(enc.buf) }
// Cap returns the capacity of the builder's underlying byte slice. It is the
// total space allocated for the string being built and includes any bytes
// already written.
func (enc *encoder) Cap() int { return cap(enc.buf) }
// Write implements io.Writer Write method
func (enc *encoder) Write(p []byte) (int, error) {
enc.buf = append(enc.buf, p...)
return len(p), nil
}
// grow copies the buffer to a new, larger buffer so that there are at least n
// bytes of capacity beyond len(b.buf).
func (enc *encoder) grow(n int) {
buf := make([]byte, len(enc.buf), 2*cap(enc.buf)+n)
copy(buf, enc.buf)
enc.buf = buf
}
func (enc *encoder) reset() {
enc.buf = enc.buf[:0]
}
func (enc *encoder) writeByte(c byte) {
enc.buf = append(enc.buf, c)
}
func (enc *encoder) writeString(s string) {
enc.buf = append(enc.buf, s...)
}
func (enc *encoder) encodeKey(key string) {
if len(enc.buf) == 0 {
enc.writeByte('{')
} else {
enc.writeByte(',')
}
if isIdent(key) {
enc.buf = append(enc.buf, key...)
} else {
enc.encodeString(key)
}
enc.writeByte(':')
}
func (enc *encoder) finish() {
if len(enc.buf) > 0 {
enc.buf = append(enc.buf, '}', ' ')
}
}
func (enc *encoder) encodeNil() {
enc.buf = append(enc.buf, "nil"...)
}
func (enc *encoder) encodeByte(c byte) {
enc.buf = append(enc.buf, '\'', c, '\'')
}
func (enc *encoder) encodeRune(r rune) {
enc.buf = strconv.AppendQuoteRune(enc.buf, r)
}
func (enc *encoder) encodeString(s string) {
enc.buf = strconv.AppendQuote(enc.buf, s)
}
func (enc *encoder) encodeInt(i int64) {
enc.buf = strconv.AppendInt(enc.buf, i, 10)
}
func (enc *encoder) encodeUint(i uint64) {
enc.buf = strconv.AppendUint(enc.buf, i, 10)
}
func (enc *encoder) encodeFloat(f float64, bits int) {
enc.buf = strconv.AppendFloat(enc.buf, f, 'f', -1, bits)
}
func (enc *encoder) encodeFloat32(f float32) {
enc.encodeFloat(float64(f), 32)
}
func (enc *encoder) encodeFloat64(f float64) {
enc.encodeFloat(f, 64)
}
func (enc *encoder) encodeBool(v bool) {
enc.buf = strconv.AppendBool(enc.buf, v)
}
func (enc *encoder) encodeComplex(r, i float64, bits int) {
if r != 0 {
enc.encodeFloat(r, bits)
}
if i != 0 {
if r != 0 {
enc.buf = append(enc.buf, '+')
}
enc.encodeFloat(i, bits)
enc.buf = append(enc.buf, 'i')
} else if r == 0 {
enc.buf = append(enc.buf, '0')
}
}
func (enc *encoder) encodeComplex64(c complex64) {
r, i := real(c), imag(c)
enc.encodeComplex(float64(r), float64(i), 32)
}
func (enc *encoder) encodeComplex128(c complex128) {
r, i := real(c), imag(c)
enc.encodeComplex(r, i, 64)
}
func (enc *encoder) encodeScalar(value interface{}) bool {
switch x := value.(type) {
case int:
enc.encodeInt(int64(x))
case int8:
enc.encodeInt(int64(x))
case int16:
enc.encodeInt(int64(x))
case int32:
enc.encodeInt(int64(x))
case int64:
enc.encodeInt(x)
case uint:
enc.encodeUint(uint64(x))
case uint8:
enc.encodeUint(uint64(x))
case uint16:
enc.encodeUint(uint64(x))
case uint32:
enc.encodeUint(uint64(x))
case uint64:
enc.encodeUint(x)
case float32:
enc.encodeFloat32(x)
case float64:
enc.encodeFloat64(x)
case bool:
enc.encodeBool(x)
case complex64:
enc.encodeComplex64(x)
case complex128:
enc.encodeComplex128(x)
default:
return false
}
return true
}
// String returns a string representing the duration in the form "72h3m0.5s".
// Leading zero units are omitted. As a special case, durations less than one
// second format use a smaller unit (milli-, micro-, or nanoseconds) to ensure
// that the leading digit is non-zero. The zero duration formats as 0s.
func formatDuration(buf []byte, d time.Duration) int {
// Largest time is 2540400h10m10.000000000s
w := len(buf)
u := uint64(d)
neg := d < 0
if neg {
u = -u
}
if u < uint64(time.Second) {
// Special case: if duration is smaller than a second,
// use smaller units, like 1.2ms
var prec int
w--
buf[w] = 's'
w--
switch {
case u == 0:
buf[0] = '0'
buf[1] = 's'
return 2
case u < uint64(time.Microsecond):
// print nanoseconds
prec = 0
buf[w] = 'n'
case u < uint64(time.Millisecond):
// print microseconds
prec = 3
// U+00B5 'µ' micro sign == 0xC2 0xB5
w-- // Need room for two bytes.
copy(buf[w:], "µ")
default:
// print milliseconds
prec = 6
buf[w] = 'm'
}
w, u = fmtFrac(buf[:w], u, prec)
w = fmtInt(buf[:w], u)
} else {
w--
buf[w] = 's'
w, u = fmtFrac(buf[:w], u, 9)
// u is now integer seconds
w = fmtInt(buf[:w], u%60)
u /= 60
// u is now integer minutes
if u > 0 {
w--
buf[w] = 'm'
w = fmtInt(buf[:w], u%60)
u /= 60
// u is now integer hours
// Stop at hours because days can be different lengths.
if u > 0 {
w--
buf[w] = 'h'
w = fmtInt(buf[:w], u)
}
}
}
if neg {
w--
buf[w] = '-'
}
copy(buf, buf[w:])
return len(buf) - w
}
// fmtFrac formats the fraction of v/10**prec (e.g., ".12345") into the
// tail of buf, omitting trailing zeros. It omits the decimal
// point too when the fraction is 0. It returns the index where the
// output bytes begin and the value v/10**prec.
func fmtFrac(buf []byte, v uint64, prec int) (nw int, nv uint64) {
// Omit trailing zeros up to and including decimal point.
w := len(buf)
print := false
for i := 0; i < prec; i++ {
digit := v % 10
print = print || digit != 0
if print {
w--
buf[w] = byte(digit) + '0'
}
v /= 10
}
if print {
w--
buf[w] = '.'
}
return w, v
}
// fmtInt formats v into the tail of buf.
// It returns the index where the output begins.
func fmtInt(buf []byte, v uint64) int {
w := len(buf)
if v == 0 {
w--
buf[w] = '0'
} else {
for v > 0 {
w--
buf[w] = byte(v%10) + '0'
v /= 10
}
}
return w
}
type appendFormatter interface {
AppendFormat(buf []byte) []byte
}