From d6c9d39c26722d0fe7437bd4ee414fa88d9cf2a3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=B8=96=E7=95=8C?= Date: Fri, 15 Nov 2024 13:52:13 +0800 Subject: [PATCH] Add buf.MergeMulti --- common/buf/multi.go | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/common/buf/multi.go b/common/buf/multi.go index 2d74ab5c..575ddb57 100644 --- a/common/buf/multi.go +++ b/common/buf/multi.go @@ -10,6 +10,22 @@ func LenMulti(buffers []*Buffer) int { return n } +func MergeMulti(buffers []*Buffer) *Buffer { + switch len(buffers) { + case 0: + panic("empty buffers") + case 1: + return buffers[0] + default: + newBuffer := NewSize(LenMulti(buffers)) + for _, buffer := range buffers { + common.Must1(newBuffer.Write(buffer.Bytes())) + buffer.Release() + } + return newBuffer + } +} + func ToSliceMulti(buffers []*Buffer) [][]byte { return common.Map(buffers, func(it *Buffer) []byte { return it.Bytes()