hexstring 0.10.0 → 0.11.0
raw patch · 3 files changed
+30/−17 lines, 3 filesdep ~base16-bytestringdep ~binarydep ~bytestringPVP ok
version bump matches the API change (PVP)
Dependency ranges changed: base16-bytestring, binary, bytestring, text
API changes (from Hackage documentation)
- Data.HexString: asText :: HexString -> Text
- Data.HexString: fromHex :: Binary a => HexString -> a
- Data.HexString: toHex :: Binary a => a -> HexString
+ Data.HexString: fromBinary :: Binary a => a -> HexString
+ Data.HexString: fromBytes :: ByteString -> HexString
+ Data.HexString: toBinary :: Binary a => HexString -> a
+ Data.HexString: toBytes :: HexString -> ByteString
+ Data.HexString: toText :: HexString -> Text
Files
- hexstring.cabal +1/−1
- src/Data/HexString.hs +21/−9
- test/Data/HexStringSpec.hs +8/−7
hexstring.cabal view
@@ -1,6 +1,6 @@ name: hexstring category: Data -version: 0.10.0 +version: 0.11.0 license: MIT license-file: LICENSE copyright: (c) 2015 Leon Mergen
src/Data/HexString.hs view
@@ -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 @@ 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
test/Data/HexStringSpec.hs view
@@ -1,16 +1,11 @@ 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 spec :: Spec @@ -24,3 +19,9 @@ 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")