-
Notifications
You must be signed in to change notification settings - Fork 0
/
builder.go
232 lines (204 loc) · 7.1 KB
/
builder.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
// Copyright 2017 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// package builder implements Builder. It is similar to strings.Builder,
// but provides more convenient Write methods.
package builder
import (
"strconv"
"unicode/utf8"
"unsafe"
)
// A Builder is used to efficiently build a string using Write methods.
// It minimizes memory copying. The zero value is ready to use.
// Do not copy a non-zero Builder.
type Builder struct {
addr *Builder // of receiver, to detect copies by value
buf []byte
}
// noescape hides a pointer from escape analysis. It is the identity function
// but escape analysis doesn't think the output depends on the input.
// noescape is inlined and currently compiles down to zero instructions.
// USE CAREFULLY!
// This was copied from the runtime; see issues 23382 and 7921.
//
//go:nosplit
//go:nocheckptr
func noescape(p unsafe.Pointer) unsafe.Pointer {
x := uintptr(p)
return unsafe.Pointer(x ^ 0)
}
func (b *Builder) copyCheck() {
if b.addr == nil {
// This hack works around a failing of Go's escape analysis
// that was causing b to escape and be heap allocated.
// See issue 23382.
// TODO: once issue 7921 is fixed, this should be reverted to
// just "b.addr = b".
b.addr = (*Builder)(noescape(unsafe.Pointer(b)))
} else if b != b.addr {
panic("builder: illegal use of non-zero Builder copied by value")
}
}
// String returns the accumulated string.
func (b *Builder) String() string {
return unsafe.String(unsafe.SliceData(b.buf), len(b.buf))
}
// Len returns the number of accumulated bytes; b.Len() == len(b.String()).
func (b *Builder) Len() int { return len(b.buf) }
// Cap returns the capacity of the builder's underlying byte slice. It is the
// total space allocated for the string being built and includes any bytes
// already written.
func (b *Builder) Cap() int { return cap(b.buf) }
// Reset resets the Builder to be empty.
func (b *Builder) Reset() {
b.addr = nil
b.buf = nil
}
// grow copies the buffer to a new, larger buffer so that there are at least n
// bytes of capacity beyond len(b.buf).
func (b *Builder) grow(n int) {
buf := make([]byte, len(b.buf), 2*cap(b.buf)+n)
copy(buf, b.buf)
b.buf = buf
}
// Grow grows b's capacity, if necessary, to guarantee space for
// another n bytes. After Grow(n), at least n bytes can be written to b
// without another allocation. If n is negative, Grow panics.
func (b *Builder) Grow(n int) {
b.copyCheck()
if n < 0 {
panic("builder.Builder.Grow: negative count")
}
if cap(b.buf)-len(b.buf) < n {
b.grow(n)
}
}
// Clip removes unused capacity from b's buffer.
func (b *Builder) Clip() {
b.copyCheck()
b.buf = b.buf[:len(b.buf):len(b.buf)]
}
// Write appends the contents of p to b's buffer.
// Write always returns len(p), nil.
func (b *Builder) Write(p []byte) (int, error) {
b.copyCheck()
b.buf = append(b.buf, p...)
return len(p), nil
}
// WriteByte appends the byte c to b's buffer.
// The returned error is always nil.
func (b *Builder) WriteByte(c byte) error {
b.copyCheck()
b.buf = append(b.buf, c)
return nil
}
// WriteRune appends the UTF-8 encoding of Unicode code point r to b's buffer.
// It returns the length of r and a nil error.
func (b *Builder) WriteRune(r rune) (int, error) {
// Compare as uint32 to correctly handle negative runes.
if uint32(r) < utf8.RuneSelf {
_ = b.WriteByte(byte(r))
return 1, nil
}
b.copyCheck()
n := len(b.buf)
b.buf = utf8.AppendRune(b.buf, r)
return len(b.buf) - n, nil
}
// WriteString appends the contents of s to b's buffer.
// It returns the length of s and a nil error.
func (b *Builder) WriteString(s string) (int, error) {
b.copyCheck()
b.buf = append(b.buf, s...)
return len(s), nil
}
// WriteBool appends "true" or "false", according to the value of v, to b's buffer.
// It returns the length of written and a nil error.
func (b *Builder) WriteBool(v bool) (int, error) {
b.copyCheck()
n := len(b.buf)
b.buf = strconv.AppendBool(b.buf, v)
return len(b.buf) - n, nil
}
// WriteInt appends the string form of the integer i,
// as generated by FormatInt, to b's buffer.
// It returns the length of written and a nil error.
func (b *Builder) WriteInt(i int64, base int) (int, error) {
b.copyCheck()
n := len(b.buf)
b.buf = strconv.AppendInt(b.buf, i, base)
return len(b.buf) - n, nil
}
// WriteUint appends the string form of the unsigned integer i,
// as generated by FormatUint, to b's buffer.
// It returns the length of written and a nil error.
func (b *Builder) WriteUint(i uint64, base int) (int, error) {
b.copyCheck()
n := len(b.buf)
b.buf = strconv.AppendUint(b.buf, i, base)
return len(b.buf) - n, nil
}
// WriteFloat appends the string form of the floating-point number f,
// as generated by FormatFloat, to b's buffer.
// It returns the length of written and a nil error.
func (b *Builder) WriteFloat(f float64, fmt byte, prec, bitSize int) (int, error) {
b.copyCheck()
n := len(b.buf)
b.buf = strconv.AppendFloat(b.buf, f, fmt, prec, bitSize)
return len(b.buf) - n, nil
}
// WriteQuote appends a double-quoted Go string literal representing s,
// as generated by Quote, to b's buffer.
// It returns the length of written and a nil error.
func (b *Builder) WriteQuote(s string) (int, error) {
b.copyCheck()
n := len(b.buf)
b.buf = strconv.AppendQuote(b.buf, s)
return len(b.buf) - n, nil
}
// WriteQuoteRune appends a single-quoted Go character literal representing the rune,
// as generated by QuoteRune, to b's buffer.
// It returns the length of written and a nil error.
func (b *Builder) WriteQuoteRune(r rune) (int, error) {
b.copyCheck()
n := len(b.buf)
b.buf = strconv.AppendQuoteRune(b.buf, r)
return len(b.buf) - n, nil
}
// WriteQuoteRuneToASCII appends a single-quoted Go character literal representing the rune,
// as generated by QuoteRuneToASCII, to b's buffer.
// It returns the length of written and a nil error.
func (b *Builder) WriteQuoteRuneToASCII(r rune) (int, error) {
b.copyCheck()
n := len(b.buf)
b.buf = strconv.AppendQuoteRuneToASCII(b.buf, r)
return len(b.buf) - n, nil
}
// WriteQuoteRuneToGraphic appends a single-quoted Go character literal representing the rune,
// as generated by QuoteRuneToGraphic, to b's buffer.
// It returns the length of written and a nil error.
func (b *Builder) WriteQuoteRuneToGraphic(r rune) (int, error) {
b.copyCheck()
n := len(b.buf)
b.buf = strconv.AppendQuoteRuneToGraphic(b.buf, r)
return len(b.buf) - n, nil
}
// WriteQuoteToASCII appends a double-quoted Go string literal representing s,
// as generated by QuoteToASCII, to b's buffer.
// It returns the length of written and a nil error.
func (b *Builder) WriteQuoteToASCII(s string) (int, error) {
b.copyCheck()
n := len(b.buf)
b.buf = strconv.AppendQuoteToASCII(b.buf, s)
return len(b.buf) - n, nil
}
// WriteQuoteToGraphic appends a double-quoted Go string literal representing s,
// as generated by QuoteToGraphic, to b's buffer.
// It returns the length of written and a nil error.
func (b *Builder) WriteQuoteToGraphic(s string) (int, error) {
b.copyCheck()
n := len(b.buf)
b.buf = strconv.AppendQuoteToGraphic(b.buf, s)
return len(b.buf) - n, nil
}