forked from codnect/logy
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuffer.go
More file actions
114 lines (91 loc) · 1.71 KB
/
buffer.go
File metadata and controls
114 lines (91 loc) · 1.71 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
package logy
import (
"strconv"
"sync"
"time"
)
type buffer []byte
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) WritePadding(n int) {
if n <= 0 {
return
}
for i := 0; i < n; i++ {
*b = append(*b, ' ')
}
}
func (b *buffer) Write(p []byte) (int, error) {
*b = append(*b, p...)
return len(p), nil
}
func (b *buffer) WriteByte(c byte) {
*b = append(*b, c)
}
func (b *buffer) WriteString(s string) {
*b = append(*b, s...)
}
func (b *buffer) WriteInt(i int64) {
*b = strconv.AppendInt(*b, i, 10)
}
func (b *buffer) WriteTime(t time.Time) {
*b = t.AppendFormat(*b, time.RFC3339)
}
func (b *buffer) WriteTimeLayout(t time.Time, layout string) {
*b = t.AppendFormat(*b, layout)
}
func (b *buffer) WriteIntWidth(i, width int) {
if i < 0 {
panic("negative int")
}
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) WriteUint(i uint64) {
*b = strconv.AppendUint(*b, i, 10)
}
func (b *buffer) WriteBool(v bool) {
*b = strconv.AppendBool(*b, v)
}
func (b *buffer) WriteFloat(f float64, bitSize int) {
*b = strconv.AppendFloat(*b, f, 'f', -1, bitSize)
}
func (b *buffer) Len() int {
return len(*b)
}
func (b *buffer) Cap() int {
return cap(*b)
}
func (b *buffer) Bytes() []byte {
return *b
}
func (b *buffer) String() string {
return string(*b)
}
func (b *buffer) Reset() {
*b = (*b)[:0]
}
func (b *buffer) Free() {
const maxBufferSize = 16 << 10
if cap(*b) <= maxBufferSize {
*b = (*b)[:0]
bufPool.Put(b)
}
}