-
Notifications
You must be signed in to change notification settings - Fork 0
/
chain.go
208 lines (177 loc) · 4.96 KB
/
chain.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
package bytebuf
import (
"reflect"
"unsafe"
"github.com/koykov/bytealg"
"github.com/koykov/byteconv"
"github.com/koykov/x2bytes"
)
// Chain is a primitive byte buffer with chain call support.
type Chain []byte
// Bytes returns contents of the buffer.
func (b *Chain) Bytes() []byte {
return *b
}
// BytesCopy returns copy of the buffer.
func (b *Chain) BytesCopy() []byte {
return bytealg.Copy(*b)
}
// Get contents of the buffer as string.
func (b *Chain) String() string {
return byteconv.B2S(*b)
}
// StringCopy returns copy of the buffer contents as string.
func (b *Chain) StringCopy() string {
return bytealg.Copy[string](byteconv.B2S(*b))
}
// Write bytes to the buffer.
func (b *Chain) Write(p []byte) *Chain {
*b = append(*b, p...)
return b
}
// WriteByte writes single byte.
func (b *Chain) WriteByte(p byte) *Chain {
*b = append(*b, p)
return b
}
// WriteStr writes string to the buffer.
// DEPRECATED: use WriteString() instead.
func (b *Chain) WriteStr(s string) *Chain {
*b = append(*b, s...)
return b
}
// WriteString writes string to the buffer.
func (b *Chain) WriteString(s string) *Chain {
*b = append(*b, s...)
return b
}
// WriteInt writes integer value to the buffer.
func (b *Chain) WriteInt(i int64) *Chain {
*b, _ = x2bytes.IntToBytes(*b, i)
return b
}
// WriteUint writes unsigned integer value to the buffer.
func (b *Chain) WriteUint(u uint64) *Chain {
*b, _ = x2bytes.UintToBytes(*b, u)
return b
}
// WriteFloat writes float value to the buffer.
func (b *Chain) WriteFloat(f float64) *Chain {
*b, _ = x2bytes.FloatToBytes(*b, f)
return b
}
// WriteBool writes boolean value to the buffer.
func (b *Chain) WriteBool(v bool) *Chain {
*b, _ = x2bytes.BoolToBytes(*b, v)
return b
}
// WriteX write x with arbitrary type to the buffer.
func (b *Chain) WriteX(x any) *Chain {
*b, _ = x2bytes.ToBytes(*b, x)
return b
}
// WriteMarshallerTo marshal data of struct implemented MarshallerTo interface and write it to the buffer.
func (b *Chain) WriteMarshallerTo(m MarshallerTo) *Chain {
if m == nil {
return b
}
n := b.Len()
d := m.Size()
b.GrowDelta(d)
_, _ = m.MarshalTo(b.Bytes()[n:])
return b
}
// WriteApplyFn applies fn to p and write result to the buffer.
func (b *Chain) WriteApplyFn(p []byte, fn func(dst, p []byte) []byte) *Chain {
*b = fn(*b, p)
return b
}
// WriteApplyFnStr applies fn to s and write result to the buffer.
// DEPRECATED: use WriteApplyFnString() instead.
func (b *Chain) WriteApplyFnStr(s string, fn func(dst, p []byte) []byte) *Chain {
*b = fn(*b, byteconv.S2B(s))
return b
}
// WriteApplyFnString applies fn to s and write result to the buffer.
func (b *Chain) WriteApplyFnString(s string, fn func(dst, p []byte) []byte) *Chain {
*b = fn(*b, byteconv.S2B(s))
return b
}
// Replace replaces old bytes to new in buffer.
func (b *Chain) Replace(old, new []byte, n int) *Chain {
if b.Len() == 0 || n == 0 {
return b
}
var i, at, c int
// Use the same byte buffer to make replacement and avoid alloc.
dst := (*b)[b.Len():]
for {
if i = bytealg.IndexAt(*b, old, at); i < 0 || c == n {
dst = append(dst, (*b)[at:]...)
break
}
dst = append(dst, (*b)[at:i]...)
dst = append(dst, new...)
at = i + len(old)
c++
}
// Move result to the beginning of buffer.
b.Reset().Write(dst)
return b
}
// ReplaceStr replace old to new substrings in buffer.
// DEPRECATED: use ReplaceString() instead.
func (b *Chain) ReplaceStr(old, new string, n int) *Chain {
return b.ReplaceString(old, new, n)
}
// ReplaceString replace old to new substrings in buffer.
func (b *Chain) ReplaceString(old, new string, n int) *Chain {
return b.Replace(byteconv.S2B(old), byteconv.S2B(new), n)
}
// ReplaceAll replace all occurrences of old bytes to new in buffer.
func (b *Chain) ReplaceAll(old, new []byte) *Chain {
return b.Replace(old, new, -1)
}
// ReplaceStrAll replaces all occurrences of old substrings to new in buffer.
// DEPRECATED: use ReplaceStringAll() instead.
func (b *Chain) ReplaceStrAll(old, new string) *Chain {
return b.ReplaceStringAll(old, new)
}
// ReplaceStringAll replaces all occurrences of old substrings to new in buffer.
func (b *Chain) ReplaceStringAll(old, new string) *Chain {
return b.Replace(byteconv.S2B(old), byteconv.S2B(new), -1)
}
func (b *Chain) Len() int {
return len(*b)
}
func (b *Chain) Cap() int {
return cap(*b)
}
// Grow increases length of the buffer.
func (b *Chain) Grow(newLen int) *Chain {
if newLen <= 0 {
return b
}
// Get buffer header.
h := *(*reflect.SliceHeader)(unsafe.Pointer(b))
if newLen < h.Cap {
// Just increase header's length if capacity allows
h.Len = newLen
// .. and restore the buffer from the header.
*b = *(*[]byte)(unsafe.Pointer(&h))
} else {
// Append necessary space.
*b = append(*b, make([]byte, newLen-b.Len())...)
}
return b
}
// GrowDelta increases length of the buffer to actual length + delta.
//
// See Grow().
func (b *Chain) GrowDelta(delta int) *Chain {
return b.Grow(b.Len() + delta)
}
func (b *Chain) Reset() *Chain {
*b = (*b)[:0]
return b
}