MCPcopy Create free account
hub / github.com/golang/snappy / Encode

Function Encode

encode.go:20–43  ·  view source on GitHub ↗

Encode 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. The dst and src must not overlap. It is valid to pass a nil dst. Encode handles the Snappy block form

(dst, src []byte)

Source from the content-addressed store, hash-verified

18//
19// Encode handles the Snappy block format, not the Snappy stream format.
20func Encode(dst, src []byte) []byte {
21 if n := MaxEncodedLen(len(src)); n < 0 {
22 panic(ErrTooLarge)
23 } else if len(dst) < n {
24 dst = make([]byte, n)
25 }
26
27 // The block starts with the varint-encoded length of the decompressed bytes.
28 d := binary.PutUvarint(dst, uint64(len(src)))
29
30 for len(src) > 0 {
31 p := src
32 src = nil
33 if len(p) > maxBlockSize {
34 p, src = p[:maxBlockSize], p[maxBlockSize:]
35 }
36 if len(p) < minNonLiteralBlockSize {
37 d += emitLiteral(dst[d:], p)
38 } else {
39 d += encodeBlock(dst[d:], p)
40 }
41 }
42 return dst[:d]
43}
44
45// inputMargin is the minimum number of extra input bytes to keep, inside
46// encodeBlock's inner loop. On some architectures, this margin lets us

Callers 8

runFunction · 0.92
roundtripFunction · 0.85
TestEncodeGoldenInputFunction · 0.85
runTestSameEncodingAsCppFunction · 0.85
benchDecodeFunction · 0.85
benchEncodeFunction · 0.85
writeMethod · 0.85

Calls 3

MaxEncodedLenFunction · 0.85
emitLiteralFunction · 0.70
encodeBlockFunction · 0.70

Tested by 6

roundtripFunction · 0.68
TestEncodeGoldenInputFunction · 0.68
runTestSameEncodingAsCppFunction · 0.68
benchDecodeFunction · 0.68
benchEncodeFunction · 0.68

Used in the wild real call sites across dependent graphs

searching dependent graphs…