-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbuffer.go
More file actions
136 lines (115 loc) · 2.5 KB
/
buffer.go
File metadata and controls
136 lines (115 loc) · 2.5 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
// Package buffer provides a pool-allocated byte buffer with custom methods.
package graylog
import (
"encoding"
"encoding/json"
"math"
"strconv"
"sync"
)
// buffer adapted from go/src/fmt/print.go
type buffer []byte
// Having an initial size gives a dramatic speedup.
var bufPool = sync.Pool{
New: func() any {
b := make([]byte, 0, 1024)
return (*buffer)(&b)
},
}
func newBuffer() *buffer {
return bufPool.Get().(*buffer)
}
func (b *buffer) Free() {
// To reduce peak allocation, return only smaller buffers to the pool.
const maxBufferSize = 16 << 10
if cap(*b) <= maxBufferSize {
*b = (*b)[:0]
bufPool.Put(b)
}
}
func (b *buffer) Reset() {
*b = (*b)[:0]
}
func (b *buffer) Write(p []byte) (int, error) {
*b = append(*b, p...)
return len(p), nil
}
func (b *buffer) WriteString(s string) {
*b = append(*b, s...)
}
func (b *buffer) WriteQuoted(s string) {
*b = strconv.AppendQuote(*b, s)
}
func (b *buffer) WriteByte(c byte) {
*b = append(*b, c)
}
func (b *buffer) WriteBool(v bool) {
c := `"false"`
if v {
c = `"true"`
}
*b = append(*b, c...)
}
func (b *buffer) WriteInt(i int64) {
*b = strconv.AppendInt(*b, i, 10)
}
func (b *buffer) WriteUint(i uint64) {
*b = strconv.AppendUint(*b, i, 10)
}
func (b *buffer) WriteJson(v any) error {
data, err := json.Marshal(v)
if err != nil {
return err
}
_, _ = b.Write(data)
return nil
}
func (b *buffer) WriteText(v encoding.TextMarshaler) error {
data, err := v.MarshalText()
if err != nil {
return err
}
b.WriteQuoted(string(data))
return nil
}
func (b *buffer) WriteFloat(v float64) error {
switch {
case math.IsInf(v, 1):
b.WriteString(`"+Inf"`)
case math.IsInf(v, -1):
b.WriteString(`"-Inf"`)
case math.IsNaN(v):
b.WriteString(`"NaN"`)
default:
return b.WriteJson(v)
}
return nil
}
// func (b *Buffer) WritePosInt(i int) {
// b.WritePosIntWidth(i, 0)
// }
// // WritePosIntWidth writes non-negative integer i to the buffer, padded on the left
// // by zeroes to the given width. Use a width of 0 to omit padding.
// func (b *Buffer) WritePosIntWidth(i, width int) {
// // Cheap integer to fixed-width decimal ASCII.
// // Copied from log/log.go.
// if i < 0 {
// panic("negative int")
// }
// // Assemble decimal in reverse order.
// var bb [20]byte
// bp := len(bb) - 1
// for i >= 10 || width > 1 {
// width--
// q := i / 10
// bb[bp] = byte('0' + i - q*10)
// bp--
// i = q
// }
// // i < 10
// bb[bp] = byte('0' + i)
// b.Write(bb[bp:])
// }
func (b *buffer) String() string {
return string(*b)
}