summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSam Truzjan <pxqr.sta@gmail.com>2013-09-29 08:37:41 +0400
committerSam Truzjan <pxqr.sta@gmail.com>2013-09-29 08:37:41 +0400
commitaec496291de09c0387d51a3ee1fbdb5927b19c15 (patch)
tree85895e2182431b97cac41fe221798d93c63ffec9
parentbb73c6aa23a05ad55432b869649c89e9eb656d6c (diff)
Rename decode and encode to mimic Binary package
This lead to more intuitive API.
-rw-r--r--bench/Main.hs26
-rw-r--r--bencoding.cabal9
-rw-r--r--src/Data/BEncode.hs10
-rw-r--r--src/Data/BEncode/Internal.hs12
-rw-r--r--tests/properties.hs2
5 files changed, 30 insertions, 29 deletions
diff --git a/bench/Main.hs b/bench/Main.hs
index 1cd2429..d60c0d7 100644
--- a/bench/Main.hs
+++ b/bench/Main.hs
@@ -21,7 +21,7 @@ import "bencode" Data.BEncode as A
21import Data.AttoBencode as B 21import Data.AttoBencode as B
22import Data.AttoBencode.Parser as B 22import Data.AttoBencode.Parser as B
23import "bencoding" Data.BEncode as C 23import "bencoding" Data.BEncode as C
24 24import "bencoding" Data.BEncode.Internal as C
25 25
26instance NFData A.BEncode where 26instance NFData A.BEncode where
27 rnf (A.BInt i) = rnf i 27 rnf (A.BInt i) = rnf i
@@ -123,21 +123,21 @@ main = do
123 , bench "decode/AttoBencode" $ 123 , bench "decode/AttoBencode" $
124 nf (getRight . Atto.parseOnly bValue) torrentFile 124 nf (getRight . Atto.parseOnly bValue) torrentFile
125 , bench "decode/bencoding" $ 125 , bench "decode/bencoding" $
126 nf (getRight . C.decode) torrentFile 126 nf (getRight . C.parse) torrentFile
127 127
128 , let Just v = A.bRead lazyTorrentFile in 128 , let Just v = A.bRead lazyTorrentFile in
129 bench "encode/bencode" $ nf A.bPack v 129 bench "encode/bencode" $ nf A.bPack v
130 , let Right v = Atto.parseOnly bValue torrentFile in 130 , let Right v = Atto.parseOnly bValue torrentFile in
131 bench "encode/AttoBencode" $ nf B.encode v 131 bench "encode/AttoBencode" $ nf B.encode v
132 , let Right v = C.decode torrentFile in 132 , let Right v = C.parse torrentFile in
133 bench "encode/bencoding" $ nf C.encode v 133 bench "encode/bencoding" $ nf C.build v
134 134
135 , bench "decode+encode/bencode" $ 135 , bench "decode+encode/bencode" $
136 nf (A.bPack . fromJust . A.bRead) lazyTorrentFile 136 nf (A.bPack . fromJust . A.bRead) lazyTorrentFile
137 , bench "decode+encode/AttoBencode" $ 137 , bench "decode+encode/AttoBencode" $
138 nf (B.encode . getRight . Atto.parseOnly bValue) torrentFile 138 nf (B.encode . getRight . Atto.parseOnly bValue) torrentFile
139 , bench "decode+encode/bencoding" $ 139 , bench "decode+encode/bencoding" $
140 nf (C.encode . getRight . C.decode) torrentFile 140 nf (C.build . getRight . C.parse) torrentFile
141 141
142 , bench "list10000int/bencode/encode" $ 142 , bench "list10000int/bencode/encode" $
143 nf (A.bPack . A.BList . L.map (A.BInt . fromIntegral)) 143 nf (A.bPack . A.BList . L.map (A.BInt . fromIntegral))
@@ -146,7 +146,7 @@ main = do
146 , bench "list10000int/attobencode/encode" $ 146 , bench "list10000int/attobencode/encode" $
147 nf B.encode [1..20000 :: Int] 147 nf B.encode [1..20000 :: Int]
148 , bench "list10000int/bencoding/encode" $ 148 , bench "list10000int/bencoding/encode" $
149 nf C.encoded [1..20000 :: Int] 149 nf C.encode [1..20000 :: Int]
150 150
151 151
152 , let d = A.bPack $ A.BList $ 152 , let d = A.bPack $ A.BList $
@@ -154,27 +154,27 @@ main = do
154 in d `seq` (bench "list1000int/bencode/decode" $ nf 154 in d `seq` (bench "list1000int/bencode/decode" $ nf
155 (fromJust . A.bRead :: BL.ByteString -> A.BEncode) d) 155 (fromJust . A.bRead :: BL.ByteString -> A.BEncode) d)
156 156
157 , let d = BL.toStrict (C.encoded (L.replicate 10000 ())) 157 , let d = BL.toStrict (C.encode (L.replicate 10000 ()))
158 in d `seq` (bench "list10000unit/bencoding/decode" $ nf 158 in d `seq` (bench "list10000unit/bencoding/decode" $ nf
159 (C.decoded :: BS.ByteString -> Either String [()]) d) 159 (C.decode :: BS.ByteString -> Either String [()]) d)
160 160
161 , let d = BL.toStrict $ C.encoded $ L.replicate 10000 (0 :: Int) 161 , let d = BL.toStrict $ C.encode $ L.replicate 10000 (0 :: Int)
162 in d `seq` (bench "list10000int/bencoding/decode" $ nf 162 in d `seq` (bench "list10000int/bencoding/decode" $ nf
163 (C.decoded :: BS.ByteString -> Either String [Int]) d) 163 (C.decode :: BS.ByteString -> Either String [Int]) d)
164 164
165 , let d = L.replicate 10000 0 165 , let d = L.replicate 10000 0
166 in bench "list10000int/bencoding/encode>>decode" $ 166 in bench "list10000int/bencoding/encode>>decode" $
167 nf (getRight . C.decoded . BL.toStrict . C.encoded 167 nf (getRight . C.decode . BL.toStrict . C.encode
168 :: [Int] -> [Int] ) 168 :: [Int] -> [Int] )
169 d 169 d
170 170
171 , let d = replicate' 10000 0 171 , let d = replicate' 10000 0
172 in bench "list10000int/bencoding/encode>>decode/generic" $ 172 in bench "list10000int/bencoding/encode>>decode/generic" $
173 nf (getRight . C.decoded . BL.toStrict . C.encoded 173 nf (getRight . C.decode . BL.toStrict . C.encode
174 :: List Int -> List Int) 174 :: List Int -> List Int)
175 d 175 d
176 176
177 , let Right be = C.decode torrentFile 177 , let Right be = C.parse torrentFile
178 id' x = let t = either error id (fromBEncode x) 178 id' x = let t = either error id (fromBEncode x)
179 in toBEncode (t :: Torrent) 179 in toBEncode (t :: Torrent)
180 180
diff --git a/bencoding.cabal b/bencoding.cabal
index bbbf001..916771e 100644
--- a/bencoding.cabal
+++ b/bencoding.cabal
@@ -40,13 +40,16 @@ library
40 hs-source-dirs: src 40 hs-source-dirs: src
41 exposed-modules: Data.BEncode 41 exposed-modules: Data.BEncode
42 , Data.BEncode.BDict 42 , Data.BEncode.BDict
43 43 , Data.BEncode.Internal
44 , Data.BEncode.Types
44 build-depends: base == 4.* 45 build-depends: base == 4.*
45 , ghc-prim 46 , ghc-prim
46 , deepseq == 1.3.* 47 , deepseq == 1.3.*
47 , containers >= 0.4 48 , mtl
48 , bytestring >= 0.10.0.2 49
49 , attoparsec >= 0.10 50 , attoparsec >= 0.10
51 , bytestring >= 0.10.0.2
52 , containers >= 0.4
50 , text >= 0.11 53 , text >= 0.11
51 , pretty 54 , pretty
52 ghc-options: -Wall -fno-warn-unused-do-bind 55 ghc-options: -Wall -fno-warn-unused-do-bind
diff --git a/src/Data/BEncode.hs b/src/Data/BEncode.hs
index 1969595..8dc7c89 100644
--- a/src/Data/BEncode.hs
+++ b/src/Data/BEncode.hs
@@ -73,8 +73,6 @@ module Data.BEncode
73 -- * Serialization 73 -- * Serialization
74 , encode 74 , encode
75 , decode 75 , decode
76 , encoded
77 , decoded
78 76
79 -- ** Dictionaries 77 -- ** Dictionaries
80 -- *** Building 78 -- *** Building
@@ -700,9 +698,9 @@ isDict _ = False
700--------------------------------------------------------------------} 698--------------------------------------------------------------------}
701 699
702-- | The same as 'decode' but returns any bencodable value. 700-- | The same as 'decode' but returns any bencodable value.
703decoded :: BEncode a => ByteString -> Result a 701decode :: BEncode a => ByteString -> Result a
704decoded = decode >=> fromBEncode 702decode = parse >=> fromBEncode
705 703
706-- | The same as 'encode' but takes any bencodable value. 704-- | The same as 'encode' but takes any bencodable value.
707encoded :: BEncode a => a -> Lazy.ByteString 705encode :: BEncode a => a -> Lazy.ByteString
708encoded = encode . toBEncode 706encode = build . toBEncode
diff --git a/src/Data/BEncode/Internal.hs b/src/Data/BEncode/Internal.hs
index 30cfbe7..3552bc9 100644
--- a/src/Data/BEncode/Internal.hs
+++ b/src/Data/BEncode/Internal.hs
@@ -9,8 +9,8 @@
9-- don't need to import this module. 9-- don't need to import this module.
10-- 10--
11module Data.BEncode.Internal 11module Data.BEncode.Internal
12 ( decode 12 ( parse
13 , encode 13 , build
14 , ppBEncode 14 , ppBEncode
15 ) where 15 ) where
16 16
@@ -59,8 +59,8 @@ builder = go
59 59
60-- | Convert bencoded value to raw bytestring according to the 60-- | Convert bencoded value to raw bytestring according to the
61-- specification. 61-- specification.
62encode :: BValue -> Lazy.ByteString 62build :: BValue -> Lazy.ByteString
63encode = B.toLazyByteString . builder 63build = B.toLazyByteString . builder
64 64
65{-------------------------------------------------------------------- 65{--------------------------------------------------------------------
66-- Deserialization 66-- Deserialization
@@ -113,8 +113,8 @@ parser = valueP
113 113
114-- | Try to convert raw bytestring to bencoded value according to 114-- | Try to convert raw bytestring to bencoded value according to
115-- specification. 115-- specification.
116decode :: ByteString -> Either String BValue 116parse :: ByteString -> Either String BValue
117decode = P.parseOnly parser 117parse = P.parseOnly parser
118 118
119{-------------------------------------------------------------------- 119{--------------------------------------------------------------------
120 Pretty Printing 120 Pretty Printing
diff --git a/tests/properties.hs b/tests/properties.hs
index 586e94d..3d08eed 100644
--- a/tests/properties.hs
+++ b/tests/properties.hs
@@ -54,7 +54,7 @@ instance Arbitrary FileInfo where
54data T a = T 54data T a = T
55 55
56prop_bencodable :: Eq a => BEncode a => T a -> a -> Bool 56prop_bencodable :: Eq a => BEncode a => T a -> a -> Bool
57prop_bencodable _ x = decoded (L.toStrict (encoded x)) == Right x 57prop_bencodable _ x = decode (L.toStrict (encode x)) == Right x
58 58
59-- All tests are (encode >>> decode = id) 59-- All tests are (encode >>> decode = id)
60main :: IO () 60main :: IO ()