From 25f9315c34326942d1c39fee3627d355085d9a96 Mon Sep 17 00:00:00 2001 From: andri lim Date: Fri, 2 Nov 2018 20:36:21 +0700 Subject: [PATCH] add compress/uncompress alias --- readme.md | 2 ++ snappy.nim | 10 ++++++++-- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/readme.md b/readme.md index e9ea9ee..d71da3b 100644 --- a/readme.md +++ b/readme.md @@ -12,6 +12,8 @@ no stream compression support at all. ## API * proc encode*(src: openArray[byte]): seq[byte] * proc decode*(src: openArray[byte]): seq[byte] +* template compress --- an alias to encode +* template uncompress --- an alias to decode ## Examples ```Nim diff --git a/snappy.nim b/snappy.nim index c060b5b..6088662 100644 --- a/snappy.nim +++ b/snappy.nim @@ -452,7 +452,7 @@ const # Otherwise, a newly allocated slice will be returned. # # The dst and src must not overlap. It is valid to pass a nil dst. -proc encode*(src: openArray[byte]): seq[byte] = +func encode*(src: openArray[byte]): seq[byte] = let n = maxEncodedLen(src.len) if n == 0: return var dst = newSeq[byte](n) @@ -483,7 +483,7 @@ proc encode*(src: openArray[byte]): seq[byte] = # decodedLen returns the length of the decoded block and the number of bytes # that the length header occupied. -proc decode*(src: openArray[byte]): seq[byte] = +func decode*(src: openArray[byte]): seq[byte] = let (len, bytesRead) = uvarint(src) if bytesRead <= 0 or len > 0xffffffff'u64: return @@ -496,3 +496,9 @@ proc decode*(src: openArray[byte]): seq[byte] = result = newSeq[byte](len) let errCode = decode(result, src[bytesRead..^1]) if errCode != 0: result = @[] + +template compress*(src: openArray[byte]): seq[byte] = + snappy.encode(src) + +template uncompress*(src: openArray[byte]): seq[byte] = + snappy.decode(src)