diff options
author | Sam Truzjan <pxqr.sta@gmail.com> | 2013-09-29 08:37:41 +0400 |
---|---|---|
committer | Sam Truzjan <pxqr.sta@gmail.com> | 2013-09-29 08:37:41 +0400 |
commit | aec496291de09c0387d51a3ee1fbdb5927b19c15 (patch) | |
tree | 85895e2182431b97cac41fe221798d93c63ffec9 | |
parent | bb73c6aa23a05ad55432b869649c89e9eb656d6c (diff) |
Rename decode and encode to mimic Binary package
This lead to more intuitive API.
-rw-r--r-- | bench/Main.hs | 26 | ||||
-rw-r--r-- | bencoding.cabal | 9 | ||||
-rw-r--r-- | src/Data/BEncode.hs | 10 | ||||
-rw-r--r-- | src/Data/BEncode/Internal.hs | 12 | ||||
-rw-r--r-- | tests/properties.hs | 2 |
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 | |||
21 | import Data.AttoBencode as B | 21 | import Data.AttoBencode as B |
22 | import Data.AttoBencode.Parser as B | 22 | import Data.AttoBencode.Parser as B |
23 | import "bencoding" Data.BEncode as C | 23 | import "bencoding" Data.BEncode as C |
24 | 24 | import "bencoding" Data.BEncode.Internal as C | |
25 | 25 | ||
26 | instance NFData A.BEncode where | 26 | instance 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. |
703 | decoded :: BEncode a => ByteString -> Result a | 701 | decode :: BEncode a => ByteString -> Result a |
704 | decoded = decode >=> fromBEncode | 702 | decode = parse >=> fromBEncode |
705 | 703 | ||
706 | -- | The same as 'encode' but takes any bencodable value. | 704 | -- | The same as 'encode' but takes any bencodable value. |
707 | encoded :: BEncode a => a -> Lazy.ByteString | 705 | encode :: BEncode a => a -> Lazy.ByteString |
708 | encoded = encode . toBEncode | 706 | encode = 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 | -- |
11 | module Data.BEncode.Internal | 11 | module 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. |
62 | encode :: BValue -> Lazy.ByteString | 62 | build :: BValue -> Lazy.ByteString |
63 | encode = B.toLazyByteString . builder | 63 | build = 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. |
116 | decode :: ByteString -> Either String BValue | 116 | parse :: ByteString -> Either String BValue |
117 | decode = P.parseOnly parser | 117 | parse = 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 | |||
54 | data T a = T | 54 | data T a = T |
55 | 55 | ||
56 | prop_bencodable :: Eq a => BEncode a => T a -> a -> Bool | 56 | prop_bencodable :: Eq a => BEncode a => T a -> a -> Bool |
57 | prop_bencodable _ x = decoded (L.toStrict (encoded x)) == Right x | 57 | prop_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) |
60 | main :: IO () | 60 | main :: IO () |