Skip to content

Commit

Permalink
Add multi buf funcs
Browse files Browse the repository at this point in the history
  • Loading branch information
nekohasekai committed Nov 15, 2024
1 parent 07f7dca commit cc74a33
Showing 1 changed file with 32 additions and 1 deletion.
33 changes: 32 additions & 1 deletion common/buf/multi.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package buf

import "github.com/sagernet/sing/common"
import (
"github.com/sagernet/sing/common"
"io"
)

func LenMulti(buffers []*Buffer) int {
var n int
Expand All @@ -10,6 +13,34 @@ func LenMulti(buffers []*Buffer) int {
return n
}

func WriteMulti(writer io.Writer, buffers []*Buffer) error {
for _, buffer := range buffers {
_, err := writer.Write(buffer.Bytes())
if err != nil {
ReleaseMulti(buffers)
return err
}
buffer.Release()
}
return nil
}

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()
Expand Down

0 comments on commit cc74a33

Please sign in to comment.