This commit is contained in:
Carlo Alberto Ferraris
2020-02-28 19:33:23 +09:00
parent 8edcc4e51f
commit 6f81292b37
2 changed files with 18 additions and 2 deletions
+17 -1
View File
@@ -113,6 +113,14 @@ func (b *Buffer) AppendByte(data byte) {
// AppendBytes appends a byte slice to buffer.
func (b *Buffer) AppendBytes(data []byte) {
if len(data) <= cap(b.Buf)-len(b.Buf) {
b.Buf = append(b.Buf, data...) // fast path
} else {
b.appendBytesSlow(data)
}
}
func (b *Buffer) appendBytesSlow(data []byte) {
for len(data) > 0 {
if cap(b.Buf) == len(b.Buf) { // EnsureSpace won't be inlined.
b.EnsureSpace(1)
@@ -128,8 +136,16 @@ func (b *Buffer) AppendBytes(data []byte) {
}
}
// AppendBytes appends a string to buffer.
// AppendString appends a string to buffer.
func (b *Buffer) AppendString(data string) {
if len(data) <= cap(b.Buf)-len(b.Buf) {
b.Buf = append(b.Buf, data...) // fast path
} else {
b.appendStringSlow(data)
}
}
func (b *Buffer) appendStringSlow(data string) {
for len(data) > 0 {
if cap(b.Buf) == len(b.Buf) { // EnsureSpace won't be inlined.
b.EnsureSpace(1)
+1 -1
View File
@@ -42,7 +42,7 @@ func TestAppendString(t *testing.T) {
s := "test"
for i := 0; i < 1000; i++ {
b.AppendBytes([]byte(s))
b.AppendString(s)
want = append(want, s...)
}