-
Notifications
You must be signed in to change notification settings - Fork 1
/
buf.go
296 lines (245 loc) · 7.6 KB
/
buf.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
package gg
import (
"fmt"
"io"
"strconv"
"unicode/utf8"
)
/*
Short for "buffer". Simpler, cleaner, more usable alternative to
`strings.Builder` and `bytes.Buffer`.
*/
type Buf []byte
var (
_ = fmt.Stringer(Zero[Buf]())
_ = AppenderTo(Zero[Buf]())
_ = io.Writer(Zero[*Buf]())
_ = io.StringWriter(Zero[*Buf]())
)
/*
Free cast to a string. Mutation of the original buffer affects the resulting
string.
*/
func (self Buf) String() string { return ToString(self) }
/*
Implement `AppenderTo`. Appends its own content to the given buffer.
If the given buffer has no capacity, returns itself.
*/
func (self Buf) AppendTo(val []byte) []byte {
if !(cap(val) > 0) {
return self
}
return append(val, self...)
}
/*
Implement `io.StringWriter`, appending the input to the buffer.
The error is always nil and may be ignored.
*/
func (self *Buf) WriteString(val string) (int, error) {
*self = append(*self, val...)
return len(val), nil
}
/*
Implement `io.Writer`, appending the input to the buffer.
The error is always nil and may be ignored.
*/
func (self *Buf) Write(val []byte) (int, error) {
*self = append(*self, val...)
return len(val), nil
}
// Appends the given string. Mutates the receiver.
func (self *Buf) AppendString(val string) { *self = append(*self, val...) }
// Appends the given string N times. Mutates the receiver.
func (self *Buf) AppendStringN(val string, count int) {
if len(val) > 0 {
for count > 0 {
count--
self.AppendString(val)
}
}
}
// Appends `Indent`. Mutates the receiver.
func (self *Buf) AppendIndent() { self.AppendString(Indent) }
// Appends `Indent` N times. Mutates the receiver.
func (self *Buf) AppendIndents(lvl int) { self.AppendStringN(Indent, lvl) }
// Appends the given bytes. Mutates the receiver.
func (self *Buf) AppendBytes(val []byte) { *self = append(*self, val...) }
// Appends the given byte. Mutates the receiver.
func (self *Buf) AppendByte(val byte) { *self = append(*self, val) }
// Appends the given rune. Mutates the receiver.
func (self *Buf) AppendRune(val rune) { *self = utf8.AppendRune(*self, val) }
// Appends the given rune N times. Mutates the receiver.
func (self *Buf) AppendRuneN(val rune, count int) {
for count > 0 {
count--
self.AppendRune(val)
}
}
// Appends a single space. Mutates the receiver.
func (self *Buf) AppendSpace() { self.AppendByte(' ') }
// Appends a space N times. Mutates the receiver.
func (self *Buf) AppendSpaces(count int) { self.AppendByteN(' ', count) }
// Appends the given byte N times. Mutates the receiver.
func (self *Buf) AppendByteN(val byte, count int) {
for count > 0 {
count--
self.AppendByte(val)
}
}
// Appends `Newline`. Mutates the receiver.
func (self *Buf) AppendNewline() { self.AppendString(Newline) }
/*
If the buffer is non-empty and doesn't end with a newline, appends a newline.
Otherwise does nothing. Uses `HasNewlineSuffix`. Mutates the receiver.
*/
func (self *Buf) AppendNewlineOpt() {
if self.Len() > 0 && !HasNewlineSuffix(*self) {
self.AppendNewline()
}
}
// Appends `Newline` N times. Mutates the receiver.
func (self *Buf) AppendNewlines(count int) { self.AppendStringN(Newline, count) }
/*
Appends text representation of the numeric value of the given byte in base 16.
Always uses exactly 2 characters, for consistent width, which is the common
convention for printing binary data. Mutates the receiver.
*/
func (self *Buf) AppendByteHex(val byte) {
if val < 16 {
self.AppendByte('0')
}
*self = strconv.AppendUint(*self, uint64(val), 16)
}
// Appends text representation of the input. Mutates the receiver.
func (self *Buf) AppendUint(val uint) {
*self = strconv.AppendUint(*self, uint64(val), 10)
}
// Appends text representation of the input. Mutates the receiver.
func (self *Buf) AppendUint64(val uint64) {
*self = strconv.AppendUint(*self, val, 10)
}
/*
Appends text representation of the input in base 16. Mutates the receiver.
Also see `.AppendByteHex`.
*/
func (self *Buf) AppendUint64Hex(val uint64) {
*self = strconv.AppendUint(*self, val, 16)
}
// Appends text representation of the input. Mutates the receiver.
func (self *Buf) AppendInt(val int) {
*self = strconv.AppendInt(*self, int64(val), 10)
}
// Appends text representation of the input. Mutates the receiver.
func (self *Buf) AppendInt64(val int64) {
*self = strconv.AppendInt(*self, val, 10)
}
// Appends text representation of the input. Mutates the receiver.
func (self *Buf) AppendFloat32(val float32) {
*self = strconv.AppendFloat(*self, float64(val), 'f', -1, 32)
}
// Appends text representation of the input. Mutates the receiver.
func (self *Buf) AppendFloat64(val float64) {
*self = strconv.AppendFloat(*self, val, 'f', -1, 64)
}
// Appends text representation of the input. Mutates the receiver.
func (self *Buf) AppendBool(val bool) { *self = strconv.AppendBool(*self, val) }
/*
Appends the string representation of the given error. If the input is nil, this
is a nop. Mutates the receiver.
*/
func (self *Buf) AppendError(val error) {
if val == nil {
return
}
impl, _ := val.(AppenderTo)
if impl != nil {
*self = impl.AppendTo(*self)
return
}
self.AppendString(val.Error())
}
/*
Same as `buf.Fprintf("%+v", val)` but marginally more efficient for `gg.Err`
or any other error type that implements `StackAppenderTo`.
*/
func (self *Buf) AppendErrorStack(val error) {
if val == nil {
return
}
impl, _ := val.(StackAppenderTo)
if impl != nil {
*self = impl.AppendStackTo(*self)
return
}
self.Fprintf(`%+v`, val)
}
/*
Appends the text representation of the input, using the `AppendTo` function.
Mutates the receiver.
*/
func (self *Buf) AppendAny(val any) { *self = AppendTo(*self, val) }
// Like `(*Buf).AppendAny` but variadic. TODO better name.
func (self *Buf) AppendAnys(val ...any) {
for _, val := range val {
self.AppendAny(val)
}
}
/*
Like `(*Buf).AppendAnys` but ensures a trailing newline in the appended content,
similarly to `fmt.Println`. As a special case, if the buffer was empty and the
appended content is empty, no newline is appended.
TODO better name.
TODO generalize for joining with arbitrary infix.
*/
func (self *Buf) AppendAnysln(val ...any) {
start := self.Len()
self.AppendAnys(val...)
end := self.Len()
if end > start {
self.AppendNewlineOpt()
} else if end > 0 {
self.AppendNewline()
}
}
/*
Appends the text representation of the input, using the `AppendGoString`
function. Mutates the receiver.
*/
func (self *Buf) AppendGoString(val any) { *self = AppendGoString(*self, val) }
// Shortcut for appending a formatted string.
func (self *Buf) Fprintf(pat string, arg ...any) {
_, _ = fmt.Fprintf(self, pat, NoEscUnsafe(arg)...)
}
// Shortcut for appending a formatted string with an idempotent trailing newline.
func (self *Buf) Fprintlnf(pat string, arg ...any) {
prev := self.Len()
self.Fprintf(pat, arg...)
if self.Len() > prev {
self.AppendNewlineOpt()
}
}
// Same as `len(buf)`.
func (self Buf) Len() int { return len(self) }
// Replaces the buffer with the given slice.
func (self *Buf) Reset(src []byte) { *self = src }
// Increases the buffer's length by N zero values. Mutates the receiver.
func (self *Buf) GrowLen(size int) { *self = GrowLen(*self, size) }
/*
Increases the buffer's capacity sufficiently to accommodate N additional
elements. Mutates the receiver.
*/
func (self *Buf) GrowCap(size int) { *self = GrowCap(*self, size) }
/*
Reduces the current length to the given size. If the current length is already
shorter, it's unaffected.
*/
func (self *Buf) TruncLen(size int) { *self = TruncLen(*self, size) }
/*
Truncates the buffer's length, preserving the capacity. Does not modify the
content. Mutates the receiver.
*/
func (self *Buf) Clear() {
if self != nil {
*self = (*self)[:0]
}
}