From: Ramakrishnan Muthukrishnan Date: Thu, 12 Feb 2015 06:34:55 +0000 (+0530) Subject: WIP: impement benc encoding, needed for infohash computation X-Git-Url: https://git.rkrishnan.org/pf/content/en/seg/about/job.html?a=commitdiff_plain;h=d2570fdf01c6b7f1453c393aa11c680d7706540a;p=functorrent.git WIP: impement benc encoding, needed for infohash computation --- diff --git a/src/Bencode.hs b/src/Bencode.hs index 4bbfa62..62627a6 100644 --- a/src/Bencode.hs +++ b/src/Bencode.hs @@ -108,3 +108,20 @@ bencVal = Bstr <$> bencStr <|> decode :: BC.ByteString -> Either ParseError BVal decode = parse bencVal "BVal" + +-- given an input dict or int or string, encode +-- it into a bencoded bytestring. +-- | encode bencoded-values +-- +-- >>> encode (Bstr (BC.pack "")) +-- "0:" +-- >>> encode (Bstr (BC.pack "spam")) +-- "4:spam" +-- >>> encode (Bint 0) +-- "i0e" +-- >>> encode (Bint 42) +-- "i42e" +encode :: BVal -> String +encode (Bstr bs) = let s = BC.unpack bs + in show (length s) ++ ":" ++ s +encode (Bint i) = "i" ++ show i ++ "e"