Skip to content

Commit 0fa6402

Browse files
authored
Create append.go
1 parent 01fb0b7 commit 0fa6402

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

append.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package bytespool
2+
3+
// Append appends the specified bytes to the buf byte slice.
4+
// The buf byte slice passed to Append MUST NOT be used after
5+
// Append is called: only the byte slice returned should be
6+
// used.
7+
func Append(buf []byte, v ...byte) []byte {
8+
if len(v) <= cap(buf)-len(buf) {
9+
return append(buf, v...)
10+
}
11+
return appendSlow(buf, v...)
12+
}
13+
14+
func appendSlow(buf []byte, v ...byte) []byte {
15+
pbuf := GetBytesSlicePtr((len(v)+len(buf))*2)
16+
nbuf := append(append((*pbuf)[:0], buf...), v...)
17+
*pbuf = buf
18+
PutBytesSlicePtr(pbuf)
19+
return nbuf
20+
}

0 commit comments

Comments
 (0)