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)
| 18 | // |
| 19 | // Encode handles the Snappy block format, not the Snappy stream format. |
| 20 | func 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 |
searching dependent graphs…