hexstring (empty) → 0.9.0
raw patch · 8 files changed
+167/−0 lines, 8 filesdep +basedep +base16-bytestringdep +bytestringsetup-changed
Dependencies added: base, base16-bytestring, bytestring, hexstring, hspec, text
Files
- LICENSE +22/−0
- README.md +9/−0
- Setup.hs +3/−0
- hexstring.cabal +56/−0
- src/Data/HexString.hs +45/−0
- test/Data/HexStringSpec.hs +23/−0
- test/Main.hs +8/−0
- test/Spec.hs +1/−0
+ LICENSE view
@@ -0,0 +1,22 @@+The MIT License (MIT) + +Copyright (c) 2015 Leon Mergen + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. +
+ README.md view
@@ -0,0 +1,9 @@+haskell-hexstring +================= + +[](https://travis-ci.org/solatis/haskell-hexstring) +[](https://coveralls.io/r/solatis/haskell-hexstring?branch=master) +[](http://en.wikipedia.org/wiki/MIT_License) +[](http://haskell.org) + +Fast and safe representation of a hex string
+ Setup.hs view
@@ -0,0 +1,3 @@+import Distribution.Simple + +main = defaultMain
+ hexstring.cabal view
@@ -0,0 +1,56 @@+name: hexstring +category: Data +version: 0.9.0 +license: MIT +license-file: LICENSE +copyright: (c) 2015 Leon Mergen +author: Leon Mergen +maintainer: leon@solatis.com +homepage: http://www.leonmergen.com/opensource.html +bug-reports: http://github.com/solatis/haskell-hexstring/issues +stability: experimental +synopsis: Fast and safe representation of a hex string +description: + Provides an interface for representing a HexString. It uses fast conversion + functions to convert to-and-from String or Text formats. Internally, the + HexString is represented by a ByteString. + +build-type: Simple +data-files: LICENSE, README.md +cabal-version: >= 1.10 +tested-with: GHC == 7.6, GHC == 7.8, GHC == 7.10 + +library + hs-source-dirs: src + ghc-options: -Wall -ferror-spans + default-language: Haskell2010 + + exposed-modules: Data.HexString + + build-depends: base >= 4.3 && < 5 + , text + , bytestring + , base16-bytestring + +test-suite test-suite + type: exitcode-stdio-1.0 + ghc-options: -Wall -ferror-spans -threaded -auto-all -caf-all -fno-warn-type-defaults + default-language: Haskell2010 + hs-source-dirs: test + main-is: Main.hs + + other-modules: Data.HexStringSpec + Spec + Main + + build-depends: base >= 4.3 && < 5 + , hspec + , text + + , bytestring + , hexstring + +source-repository head + type: git + location: git://github.com/solatis/haskell-bitcoin-script.git + branch: master
+ src/Data/HexString.hs view
@@ -0,0 +1,45 @@+module Data.HexString where + +import qualified Data.ByteString as BS +import qualified Data.ByteString.Char8 as BS8 +import qualified Data.ByteString.Base16 as BS16 + +import qualified Data.Text as T +import qualified Data.Text.Encoding as TE + +-- | Data type representing a HexString. +data HexString + = HexString !BS.ByteString + deriving ( Show, Eq, Ord ) + +-- | Access to the raw binary data this HexString represents +getBinary :: HexString -> BS.ByteString +getBinary (HexString bs) = bs + +-- | Create new HexString based on raw binary data +setBinary :: BS.ByteString -> HexString +setBinary = HexString + +-- | Converts `BS.ByteString` to a `HexString` +decodeByteString :: BS.ByteString -> HexString +decodeByteString = HexString . fst . BS16.decode + +-- | Converts a `T.Text` representation to a `HexString` +decodeText :: T.Text -> HexString +decodeText = decodeByteString . TE.encodeUtf8 + +-- | Converts a `String` representation to a `HexString` +decodeString :: String -> HexString +decodeString = decodeByteString . BS8.pack + +-- | Converts a `HexString` to a `BS.ByteString` +encodeByteString :: HexString -> BS.ByteString +encodeByteString = BS16.encode . getBinary + +-- | Converts a `HexString` to a `T.Text` representation +encodeText :: HexString -> T.Text +encodeText = TE.decodeUtf8 . encodeByteString + +-- | Converts a `HexString` to a `String` representation +encodeString :: HexString -> String +encodeString = BS8.unpack . encodeByteString
+ test/Data/HexStringSpec.hs view
@@ -0,0 +1,23 @@+module Data.HexStringSpec where + +import Data.HexString + +import qualified Data.ByteString.Char8 as BS8 +import qualified Data.Text as T + +import Test.Hspec + +spec :: Spec +spec = do + describe "when decoding hex data" $ do + it "should be able to parse basic hex data" $ do + (getBinary . decodeByteString) (BS8.pack "ffff") `shouldBe` BS8.pack "\255\255" + (getBinary . decodeString) "ffff" `shouldBe` BS8.pack "\255\255" + (getBinary . decodeText) (T.pack "ffff") `shouldBe` BS8.pack "\255\255" + + it "should be able to recode basic hex data to different formats" $ + let hex = BS8.pack "ffff" + in do + (encodeText . decodeByteString) hex `shouldBe` T.pack "ffff" + (encodeString . decodeByteString) hex `shouldBe` "ffff" + (encodeByteString . decodeByteString) hex `shouldBe` BS8.pack "ffff"
+ test/Main.hs view
@@ -0,0 +1,8 @@+module Main where + +import Test.Hspec.Runner +import qualified Spec + +main :: IO () +main = + hspecWith defaultConfig Spec.spec
+ test/Spec.hs view
@@ -0,0 +1,1 @@+{-# OPTIONS_GHC -F -pgmF hspec-discover -optF --module-name=Spec #-}