From 31fa13f7ca8158ab5d421316dcc1b6ee204bf02f Mon Sep 17 00:00:00 2001 From: Leon Mergen Date: Tue, 21 Apr 2015 10:21:07 +0700 Subject: [PATCH] Implements functions to convert to and from raw bytes --- src/Data/HexString.hs | 30 +++++++++++++++++++++--------- test/Data/HexStringSpec.hs | 15 ++++++++------- 2 files changed, 29 insertions(+), 16 deletions(-) diff --git a/src/Data/HexString.hs b/src/Data/HexString.hs index a530a7f..9568130 100644 --- a/src/Data/HexString.hs +++ b/src/Data/HexString.hs @@ -1,8 +1,10 @@ module Data.HexString ( HexString , hexString - , toHex - , fromHex - , asText ) where + , fromBinary + , toBinary + , fromBytes + , toBytes + , toText ) where import Data.Word (Word8) @@ -36,13 +38,23 @@ hexString bs = else error ("Not a valid hex string: " ++ show bs) -- | Converts a 'B.Binary' to a 'HexString' value -toHex :: B.Binary a => a -> HexString -toHex = hexString . BS16.encode . BSL.toStrict . B.encode +fromBinary :: B.Binary a => a -> HexString +fromBinary = hexString . BS16.encode . BSL.toStrict . B.encode -- | Converts a 'HexString' to a 'B.Binary' value -fromHex :: B.Binary a => HexString -> a -fromHex (HexString bs) = B.decode . BSL.fromStrict . fst . BS16.decode $ bs +toBinary :: B.Binary a => HexString -> a +toBinary (HexString bs) = B.decode . BSL.fromStrict . fst . BS16.decode $ bs + +-- | Reads a 'BS.ByteString' as raw bytes and converts to hex representation. We +-- cannot use the instance Binary of 'BS.ByteString' because it provides +-- a leading length, which is not what we want when dealing with raw bytes. +fromBytes :: BS.ByteString -> HexString +fromBytes = hexString . BS16.encode + +-- | Access to the raw bytes in a 'BS.ByteString' format. +toBytes :: HexString -> BS.ByteString +toBytes (HexString bs) = (fst . BS16.decode) bs -- | Access to a 'T.Text' representation of the 'HexString' -asText :: HexString -> T.Text -asText (HexString bs) = TE.decodeUtf8 bs +toText :: HexString -> T.Text +toText (HexString bs) = TE.decodeUtf8 bs diff --git a/test/Data/HexStringSpec.hs b/test/Data/HexStringSpec.hs index c8711eb..93b4b90 100644 --- a/test/Data/HexStringSpec.hs +++ b/test/Data/HexStringSpec.hs @@ -1,15 +1,10 @@ module Data.HexStringSpec where import Data.HexString ( hexString - , toHex - , fromHex - , asText ) + , fromBytes + , toBytes ) -import qualified Data.ByteString as BS import qualified Data.ByteString.Char8 as BS8 -import qualified Data.Text as T - -import qualified Data.Binary as B ( encode ) import Test.Hspec @@ -24,3 +19,9 @@ spec = do putStrLn (show (hexString (BS8.pack ":"))) `shouldThrow` anyErrorCall putStrLn (show (hexString (BS8.pack "`"))) `shouldThrow` anyErrorCall putStrLn (show (hexString (BS8.pack "g"))) `shouldThrow` anyErrorCall + + describe "when interpreting a hex string" $ do + it "should convert the hex string properly when interpreting as bytes" $ + toBytes (hexString (BS8.pack "ffff")) `shouldBe` BS8.pack "\255\255" + it "should convert bytes to the proper hex string" $ + fromBytes (BS8.pack "\255\255") `shouldBe` hexString (BS8.pack "ffff")