This repository has been archived by the owner on Dec 27, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
append.go
301 lines (275 loc) · 5.77 KB
/
append.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
package slog
import (
"bytes"
"math"
"strconv"
"unicode/utf8"
)
const (
_hex = "0123456789abcdef"
digits = "0123456789abcdefghijklmnopqrstuvwxyz"
initialFloatSize = 24
)
var shifts = [len(digits) + 1]uint{
1 << 1: 1,
1 << 2: 2,
1 << 3: 3,
1 << 4: 4,
1 << 5: 5,
}
func appendKeyValue(b *bytes.Buffer, k, v []byte) {
b.WriteByte('"')
b.Write(k)
b.WriteByte('"')
b.WriteByte(':')
b.WriteByte('"')
b.Write(v)
b.WriteByte('"')
b.WriteByte(',')
b.WriteByte(' ')
}
func appendString(b *bytes.Buffer, key, val string) {
b.WriteByte('"')
safeAppendString(b, key)
b.WriteByte('"')
b.WriteByte(':')
b.WriteByte('"')
safeAppendString(b, val)
b.WriteByte('"')
b.WriteByte(',')
b.WriteByte(' ')
}
func appendJsonString(b *bytes.Buffer, key, val string) {
b.WriteByte('"')
safeAppendString(b, key)
b.WriteByte('"')
b.WriteByte(':')
b.WriteByte('"')
safeAppendJsonString(b, val)
b.WriteByte('"')
b.WriteByte(',')
b.WriteByte(' ')
}
func appendRaw(b *bytes.Buffer, key string, raw []byte) {
b.WriteByte('"')
safeAppendString(b, key)
b.WriteByte('"')
b.WriteByte(':')
for _, char := range raw {
b.WriteByte(char)
}
b.WriteByte(',')
b.WriteByte(' ')
}
func appendBool(b *bytes.Buffer, key string, val bool) {
b.WriteByte('"')
safeAppendString(b, key)
b.WriteByte('"')
b.WriteByte(':')
b.WriteByte('"')
if val {
b.WriteString("true")
} else {
b.WriteString("false")
}
b.WriteByte('"')
b.WriteByte(',')
b.WriteByte(' ')
}
func appendInt(b *bytes.Buffer, key string, val int) {
appendInt64(b, key, int64(val))
}
func appendInt64(b *bytes.Buffer, key string, val int64) {
b.WriteByte('"')
safeAppendString(b, key)
b.WriteByte('"')
b.WriteByte(':')
formatBits(b, uint64(val), 10, val < 0)
b.WriteByte(',')
b.WriteByte(' ')
}
func appendUint(b *bytes.Buffer, key string, val uint) {
appendUint64(b, key, uint64(val))
}
func appendUint64(b *bytes.Buffer, key string, val uint64) {
b.WriteByte('"')
safeAppendString(b, key)
b.WriteByte('"')
b.WriteByte(':')
formatBits(b, uint64(val), 10, false)
b.WriteByte(',')
b.WriteByte(' ')
}
func appendUintptr(b *bytes.Buffer, key string, val uintptr) {
appendUint64(b, key, uint64(val))
}
func appendFloat64(b *bytes.Buffer, key string, val float64) {
b.WriteByte('"')
safeAppendString(b, key)
b.WriteByte('"')
b.WriteByte(':')
switch {
case math.IsNaN(val):
b.WriteString("NaN")
case math.IsInf(val, 1):
b.WriteString("+Inf")
case math.IsInf(val, -1):
b.WriteString("-Inf")
default:
b.Write(strconv.AppendFloat(make([]byte, 0, initialFloatSize), val, 'f', -1, 64))
}
b.WriteByte(',')
b.WriteByte(' ')
}
func safeAppendString(buf *bytes.Buffer, s string) {
for i := 0; i < len(s); {
if b := s[i]; b < utf8.RuneSelf {
i++
if 0x20 <= b && b != '\\' && b != '"' {
buf.WriteByte(b)
continue
}
switch b {
case '\\', '"':
buf.WriteByte('\\')
buf.WriteByte('b')
case '\n':
buf.WriteByte('\\')
buf.WriteByte('n')
case '\r':
buf.WriteByte('\\')
buf.WriteByte('r')
case '\t':
buf.WriteByte('\\')
buf.WriteByte('t')
default:
// Encode bytes < 0x20, except for the escape sequences above.
buf.WriteString(`\u00`)
buf.WriteByte(_hex[b>>4])
buf.WriteByte(_hex[b&0xF])
}
continue
}
c, size := utf8.DecodeRuneInString(s[i:])
if c == utf8.RuneError && size == 1 {
buf.WriteString(`\ufffd`)
i++
continue
}
buf.WriteString(s[i : i+size])
i += size
}
}
func safeAppendJsonString(buf *bytes.Buffer, s string) {
for i := 0; i < len(s); {
if b := s[i]; b < utf8.RuneSelf {
i++
if 0x20 <= b && b != '\\' && b != '"' {
buf.WriteByte(b)
continue
}
switch b {
case '\\':
buf.WriteByte('\\')
buf.WriteByte('b')
case '"':
buf.WriteByte('\\')
buf.WriteByte('"')
case '\n':
buf.WriteByte('\\')
buf.WriteByte('n')
case '\r':
buf.WriteByte('\\')
buf.WriteByte('r')
case '\t':
buf.WriteByte('\\')
buf.WriteByte('t')
default:
// Encode bytes < 0x20, except for the escape sequences above.
buf.WriteString(`\u00`)
buf.WriteByte(_hex[b>>4])
buf.WriteByte(_hex[b&0xF])
}
continue
}
c, size := utf8.DecodeRuneInString(s[i:])
if c == utf8.RuneError && size == 1 {
buf.WriteString(`\ufffd`)
i++
continue
}
buf.WriteString(s[i : i+size])
i += size
}
}
// From go source code (https://golang.org/src/strconv/itoa.go?s=2995:3022#L60).
func formatBits(buf *bytes.Buffer, u uint64, base int, neg bool) {
if base < 2 || base > len(digits) {
panic("strconv: illegal AppendInt/FormatInt base")
}
// 2 <= base && base <= len(digits)
var a [64 + 1]byte // +1 for sign of 64bit value in base 2
i := len(a)
if neg {
u = -u
}
// convert bits
if base == 10 {
// common case: use constants for / because
// the compiler can optimize it into a multiply+shift
if ^uintptr(0)>>32 == 0 {
for u > uint64(^uintptr(0)) {
q := u / 1e9
us := uintptr(u - q*1e9) // us % 1e9 fits into a uintptr
for j := 9; j > 0; j-- {
i--
qs := us / 10
a[i] = byte(us - qs*10 + '0')
us = qs
}
u = q
}
}
// u guaranteed to fit into a uintptr
us := uintptr(u)
for us >= 10 {
i--
q := us / 10
a[i] = byte(us - q*10 + '0')
us = q
}
// u < 10
i--
a[i] = byte(us + '0')
} else if s := shifts[base]; s > 0 {
// base is power of 2: use shifts and masks instead of / and %
b := uint64(base)
m := uintptr(b) - 1 // == 1<<s - 1
for u >= b {
i--
a[i] = digits[uintptr(u)&m]
u >>= s
}
// u < base
i--
a[i] = digits[uintptr(u)]
} else {
// general case
b := uint64(base)
for u >= b {
i--
q := u / b
a[i] = digits[uintptr(u-q*b)]
u = q
}
// u < base
i--
a[i] = digits[uintptr(u)]
}
// add sign, if any
if neg {
i--
a[i] = '-'
}
buf.Write(a[i:])
}