mirror of
https://github.com/cwinfo/matterbridge.git
synced 2025-07-10 15:06:26 +00:00
54
vendor/github.com/klauspost/compress/s2/encode.go
generated
vendored
54
vendor/github.com/klauspost/compress/s2/encode.go
generated
vendored
@ -58,6 +58,32 @@ func Encode(dst, src []byte) []byte {
|
||||
return dst[:d]
|
||||
}
|
||||
|
||||
// EstimateBlockSize will perform a very fast compression
|
||||
// without outputting the result and return the compressed output size.
|
||||
// The function returns -1 if no improvement could be achieved.
|
||||
// Using actual compression will most often produce better compression than the estimate.
|
||||
func EstimateBlockSize(src []byte) (d int) {
|
||||
if len(src) < 6 || int64(len(src)) > 0xffffffff {
|
||||
return -1
|
||||
}
|
||||
if len(src) <= 1024 {
|
||||
d = calcBlockSizeSmall(src)
|
||||
} else {
|
||||
d = calcBlockSize(src)
|
||||
}
|
||||
|
||||
if d == 0 {
|
||||
return -1
|
||||
}
|
||||
// Size of the varint encoded block size.
|
||||
d += (bits.Len64(uint64(len(src))) + 7) / 7
|
||||
|
||||
if d >= len(src) {
|
||||
return -1
|
||||
}
|
||||
return d
|
||||
}
|
||||
|
||||
// EncodeBetter returns the encoded form of src. The returned slice may be a sub-
|
||||
// slice of dst if dst was large enough to hold the entire encoded block.
|
||||
// Otherwise, a newly allocated slice will be returned.
|
||||
@ -132,7 +158,7 @@ func EncodeBest(dst, src []byte) []byte {
|
||||
d += emitLiteral(dst[d:], src)
|
||||
return dst[:d]
|
||||
}
|
||||
n := encodeBlockBest(dst[d:], src)
|
||||
n := encodeBlockBest(dst[d:], src, nil)
|
||||
if n > 0 {
|
||||
d += n
|
||||
return dst[:d]
|
||||
@ -404,10 +430,11 @@ type Writer struct {
|
||||
buffers sync.Pool
|
||||
pad int
|
||||
|
||||
writer io.Writer
|
||||
randSrc io.Reader
|
||||
writerWg sync.WaitGroup
|
||||
index Index
|
||||
writer io.Writer
|
||||
randSrc io.Reader
|
||||
writerWg sync.WaitGroup
|
||||
index Index
|
||||
customEnc func(dst, src []byte) int
|
||||
|
||||
// wroteStreamHeader is whether we have written the stream header.
|
||||
wroteStreamHeader bool
|
||||
@ -773,6 +800,9 @@ func (w *Writer) EncodeBuffer(buf []byte) (err error) {
|
||||
}
|
||||
|
||||
func (w *Writer) encodeBlock(obuf, uncompressed []byte) int {
|
||||
if w.customEnc != nil {
|
||||
return w.customEnc(obuf, uncompressed)
|
||||
}
|
||||
if w.snappy {
|
||||
switch w.level {
|
||||
case levelFast:
|
||||
@ -790,7 +820,7 @@ func (w *Writer) encodeBlock(obuf, uncompressed []byte) int {
|
||||
case levelBetter:
|
||||
return encodeBlockBetter(obuf, uncompressed)
|
||||
case levelBest:
|
||||
return encodeBlockBest(obuf, uncompressed)
|
||||
return encodeBlockBest(obuf, uncompressed, nil)
|
||||
}
|
||||
return 0
|
||||
}
|
||||
@ -1339,3 +1369,15 @@ func WriterFlushOnWrite() WriterOption {
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
// WriterCustomEncoder allows to override the encoder for blocks on the stream.
|
||||
// The function must compress 'src' into 'dst' and return the bytes used in dst as an integer.
|
||||
// Block size (initial varint) should not be added by the encoder.
|
||||
// Returning value 0 indicates the block could not be compressed.
|
||||
// The function should expect to be called concurrently.
|
||||
func WriterCustomEncoder(fn func(dst, src []byte) int) WriterOption {
|
||||
return func(w *Writer) error {
|
||||
w.customEnc = fn
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user