1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2025-12-29 13:35:20 -05:00

Save 1 allocation in shadowsocks

This commit is contained in:
v2ray
2016-01-31 21:37:00 +01:00
parent 97a85f04ce
commit 6c7a9586d0
2 changed files with 22 additions and 19 deletions

View File

@@ -63,13 +63,8 @@ func (b *Buffer) Append(data []byte) *Buffer {
// Prepend prepends bytes in front of the buffer. Caller must ensure total bytes prepended is
// no more than 16 bytes.
func (b *Buffer) Prepend(data []byte) *Buffer {
newoffset := b.offset - len(data)
if newoffset < 0 {
newoffset = 0
}
copy(b.head[newoffset:], data)
b.Value = b.head[newoffset : b.offset+len(b.Value)]
b.offset = newoffset
b.SliceBack(len(data))
copy(b.Value, data)
return b
}
@@ -89,6 +84,16 @@ func (b *Buffer) SliceFrom(from int) *Buffer {
return b
}
func (b *Buffer) SliceBack(offset int) *Buffer {
newoffset := b.offset - offset
if newoffset < 0 {
newoffset = 0
}
b.Value = b.head[newoffset : b.offset+len(b.Value)]
b.offset = newoffset
return b
}
// Len returns the length of the buffer content.
func (b *Buffer) Len() int {
return len(b.Value)