diff options
Diffstat (limited to 'src/Data/Torrent/InfoHash.hs')
-rw-r--r-- | src/Data/Torrent/InfoHash.hs | 13 |
1 files changed, 6 insertions, 7 deletions
diff --git a/src/Data/Torrent/InfoHash.hs b/src/Data/Torrent/InfoHash.hs index 2e3946eb..e15fdbb3 100644 --- a/src/Data/Torrent/InfoHash.hs +++ b/src/Data/Torrent/InfoHash.hs | |||
@@ -19,11 +19,9 @@ module Data.Torrent.InfoHash | |||
19 | -- * Rendering | 19 | -- * Rendering |
20 | , longHex | 20 | , longHex |
21 | , shortHex | 21 | , shortHex |
22 | , ppInfoHash | ||
23 | 22 | ||
24 | , addHashToURI | 23 | , addHashToURI |
25 | 24 | ||
26 | |||
27 | , Data.Torrent.InfoHash.hash | 25 | , Data.Torrent.InfoHash.hash |
28 | , Data.Torrent.InfoHash.hashlazy | 26 | , Data.Torrent.InfoHash.hashlazy |
29 | ) where | 27 | ) where |
@@ -52,6 +50,7 @@ import Network.URI | |||
52 | import Numeric | 50 | import Numeric |
53 | import Text.ParserCombinators.ReadP as P | 51 | import Text.ParserCombinators.ReadP as P |
54 | import Text.PrettyPrint | 52 | import Text.PrettyPrint |
53 | import Text.PrettyPrint.Class | ||
55 | 54 | ||
56 | 55 | ||
57 | -- | Exactly 20 bytes long SHA1 hash of the info part of torrent file. | 56 | -- | Exactly 20 bytes long SHA1 hash of the info part of torrent file. |
@@ -60,7 +59,7 @@ newtype InfoHash = InfoHash { getInfoHash :: BS.ByteString } | |||
60 | 59 | ||
61 | -- | for hex encoded strings | 60 | -- | for hex encoded strings |
62 | instance Show InfoHash where | 61 | instance Show InfoHash where |
63 | show = render . ppInfoHash | 62 | show = render . pretty |
64 | 63 | ||
65 | -- | for hex encoded strings | 64 | -- | for hex encoded strings |
66 | instance Read InfoHash where | 65 | instance Read InfoHash where |
@@ -106,6 +105,10 @@ instance FromJSON InfoHash where | |||
106 | instance URLShow InfoHash where | 105 | instance URLShow InfoHash where |
107 | urlShow = show | 106 | urlShow = show |
108 | 107 | ||
108 | -- | base16 encoded. | ||
109 | instance Pretty InfoHash where | ||
110 | pretty = text . BC.unpack . ppHex . getInfoHash | ||
111 | |||
109 | -- | Tries both base16 and base32 while decoding info hash. | 112 | -- | Tries both base16 and base32 while decoding info hash. |
110 | textToInfoHash :: Text -> Maybe InfoHash | 113 | textToInfoHash :: Text -> Maybe InfoHash |
111 | textToInfoHash text | 114 | textToInfoHash text |
@@ -127,10 +130,6 @@ longHex = T.decodeUtf8 . Base16.encode . getInfoHash | |||
127 | shortHex :: InfoHash -> Text | 130 | shortHex :: InfoHash -> Text |
128 | shortHex = T.take 7 . longHex | 131 | shortHex = T.take 7 . longHex |
129 | 132 | ||
130 | -- | Pretty print info hash in hexadecimal format. | ||
131 | ppInfoHash :: InfoHash -> Doc | ||
132 | ppInfoHash = text . BC.unpack . ppHex . getInfoHash | ||
133 | |||
134 | ppHex :: BS.ByteString -> BS.ByteString | 133 | ppHex :: BS.ByteString -> BS.ByteString |
135 | ppHex = BL.toStrict . B.toLazyByteString . B.byteStringHexFixed | 134 | ppHex = BL.toStrict . B.toLazyByteString . B.byteStringHexFixed |
136 | 135 | ||