hex-text 0.1.0.8 → 0.1.0.9
raw patch · 4 files changed
+85/−95 lines, 4 filesdep ~basedep ~bytestringdep ~hspecPVP ok
version bump matches the API change (PVP)
Dependency ranges changed: base, bytestring, hspec, text
API changes (from Hackage documentation)
Files
- changelog.md +4/−0
- hex-text.cabal +10/−8
- src/Text/Hex.hs +39/−57
- test/Main.hs +32/−30
changelog.md view
@@ -1,3 +1,7 @@+### 0.1.0.9 (2023-06-21)++Miscellaneous cleanup and updates+ ### 0.1.0.8 (2022-12-31) Switch testing to `hspec`
hex-text.cabal view
@@ -1,7 +1,7 @@ cabal-version: 3.0 name: hex-text-version: 0.1.0.8+version: 0.1.0.9 category: Text synopsis: ByteString-Text hexidecimal conversions@@ -26,13 +26,15 @@ location: https://github.com/typeclasses/hex-text common base- default-language: Haskell2010- default-extensions: NoImplicitPrelude+ default-language: GHC2021+ default-extensions:+ BlockArguments+ NoImplicitPrelude build-depends:- base ^>= 4.14 || ^>= 4.15 || ^>= 4.16 || ^>= 4.17+ , base ^>= 4.16 || ^>= 4.17 || ^>= 4.18 , base16-bytestring ^>= 1.0.2- , bytestring ^>= 0.10.12 || ^>= 0.11- , text ^>= 1.2.4 || ^>= 2.0+ , bytestring ^>= 0.11.4+ , text ^>= 1.2.5 || ^>= 2.0 library import: base@@ -46,5 +48,5 @@ type: exitcode-stdio-1.0 main-is: Main.hs build-depends:- hex-text- , hspec ^>= 2.8.5 || ^>= 2.9 || ^>= 2.10+ , hex-text+ , hspec ^>= 2.9.7 || ^>= 2.10 || ^>= 2.11
src/Text/Hex.hs view
@@ -1,62 +1,47 @@ module Text.Hex- (- -- * Encoding and decoding- encodeHex- , decodeHex- , lazilyEncodeHex+ ( -- * Encoding and decoding+ encodeHex,+ decodeHex,+ lazilyEncodeHex, -- * Types- , Text- , LazyText- , ByteString- , LazyByteString+ Text,+ LazyText,+ ByteString,+ LazyByteString, -- * Type conversions- , lazyText- , strictText- , lazyByteString- , strictByteString-- ) where--import Prelude (either, Maybe (..), const)---- base16-bytestring-import qualified Data.ByteString.Base16 as Base16-import qualified Data.ByteString.Base16.Lazy as LazyBase16---- bytestring-import qualified Data.ByteString as ByteString-import qualified Data.ByteString.Lazy as LazyByteString+ lazyText,+ strictText,+ lazyByteString,+ strictByteString,+ )+where --- text-import qualified Data.Text as Text-import qualified Data.Text.Encoding as Text-import qualified Data.Text.Lazy as LazyText-import qualified Data.Text.Lazy.Encoding as LazyText+import Data.ByteString qualified as ByteString+import Data.ByteString.Base16 qualified as Base16+import Data.ByteString.Base16.Lazy qualified as LazyBase16+import Data.ByteString.Lazy qualified as LazyByteString+import Data.Text qualified as Text+import Data.Text.Encoding qualified as Text+import Data.Text.Lazy qualified as LazyText+import Data.Text.Lazy.Encoding qualified as LazyText+import Prelude (Maybe (..), const, either) -- | Strict byte string--type ByteString =- ByteString.ByteString+type ByteString = ByteString.ByteString -- | Lazy byte string--type LazyByteString =- LazyByteString.ByteString+type LazyByteString = LazyByteString.ByteString -- | Strict text--type Text =- Text.Text+type Text = Text.Text -- | Lazy text--type LazyText =- LazyText.Text+type LazyText = LazyText.Text --- |--- Encodes a byte string as hexidecimal number represented in text.+-- | Encodes a byte string as hexadecimal number represented in text+-- -- Each byte of the input is converted into two characters in the -- resulting text. --@@ -73,16 +58,15 @@ -- 'ByteString' using 'decodeHex'. -- -- The lazy variant of @encodeHex@ is 'lazilyEncodeHex'.- encodeHex :: ByteString -> Text encodeHex bs =- Text.decodeUtf8 (Base16.encode bs)+ Text.decodeUtf8 (Base16.encode bs) --- |--- Decodes hexidecimal text as a byte string. If the text contains--- an even number of characters and consists only of the digits @0@--- through @9@ and letters @a@ through @f@, then the result is a--- 'Just' value.+-- | Decodes hexadecimal text as a byte string++-- If the text contains an even number of characters and consists+-- only of the digits @0@ through @9@ and letters @a@ through @f@,+-- then the result is a 'Just' value. -- -- Unpacking the ByteString in the following examples allows for -- prettier printing in the REPL.@@ -96,7 +80,7 @@ -- >>> (fmap ByteString.unpack . decodeHex . Text.pack) "c0a8010" -- Nothing ----- If the text contains non-hexidecimal characters, decoding fails+-- If the text contains non-hexadecimal characters, decoding fails -- and produces 'Nothing'. -- -- >>> (fmap ByteString.unpack . decodeHex . Text.pack) "x0a80102"@@ -110,20 +94,18 @@ decodeHex :: Text -> Maybe ByteString decodeHex txt =- either (const Nothing) Just (Base16.decode (Text.encodeUtf8 txt))+ either (const Nothing) Just (Base16.decode (Text.encodeUtf8 txt)) --- |--- @lazilyEncodeHex@ is the lazy variant of 'encodeHex'.+-- | The lazy variant of 'encodeHex' -- -- With laziness, it is possible to encode byte strings of -- infinite length: -- -- >>> (LazyText.take 8 . lazilyEncodeHex . LazyByteString.pack . cycle) [1, 2, 3] -- "01020301"- lazilyEncodeHex :: LazyByteString -> LazyText lazilyEncodeHex bs =- LazyText.decodeUtf8 (LazyBase16.encode bs)+ LazyText.decodeUtf8 (LazyBase16.encode bs) lazyText :: Text -> LazyText lazyText = LazyText.fromStrict
test/Main.hs view
@@ -1,43 +1,45 @@-import Text.Hex (decodeHex, encodeHex, lazilyEncodeHex)--import Test.Hspec (hspec, describe, it, shouldBe, Spec)+module Main (main) where -import Prelude ((.), ($), cycle, IO, fmap, Maybe (..))+import Data.ByteString qualified as ByteString+import Data.ByteString.Lazy qualified as LazyByteString import Data.String (fromString)--import qualified Data.ByteString as ByteString-import qualified Data.ByteString.Lazy as LazyByteString-import qualified Data.Text as Text-import qualified Data.Text.Lazy as LazyText+import Data.Text qualified as Text+import Data.Text.Lazy qualified as LazyText+import Test.Hspec (Spec, describe, hspec, it, shouldBe)+import Text.Hex (decodeHex, encodeHex, lazilyEncodeHex)+import Prelude (IO, Maybe (..), cycle, fmap, ($), (.)) main :: IO ()-main = hspec $ do- encodeHexTests- decodeHexTests- lazilyEncodeHexTests+main = hspec do+ encodeHexTests+ decodeHexTests+ lazilyEncodeHexTests encodeHexTests :: Spec-encodeHexTests = describe "encodeHex" $ do- it "can encode a single byte" $ do- let f = encodeHex . ByteString.singleton- f 192 `shouldBe` fromString "c0"- f 168 `shouldBe` fromString "a8"- it "can encode many bytes" $ do- let f = encodeHex . ByteString.pack- f [192, 168, 1, 2] `shouldBe` fromString "c0a80102"+encodeHexTests =+ describe "encodeHex" do+ it "can encode a single byte" do+ let f = encodeHex . ByteString.singleton+ f 192 `shouldBe` fromString "c0"+ f 168 `shouldBe` fromString "a8"+ it "can encode many bytes" do+ let f = encodeHex . ByteString.pack+ f [192, 168, 1, 2] `shouldBe` fromString "c0a80102" decodeHexTests :: Spec-decodeHexTests = describe "decodeHex" $ do+decodeHexTests =+ describe "decodeHex" do let f = fmap ByteString.unpack . decodeHex . Text.pack- it "can decode" $ do- f "c0a80102" `shouldBe` Just [192,168,1,2]- f "C0A80102" `shouldBe` Just [192,168,1,2]- it "can fail" $ do- f "c0a8010" `shouldBe` Nothing- f "x0a80102" `shouldBe` Nothing+ it "can decode" do+ f "c0a80102" `shouldBe` Just [192, 168, 1, 2]+ f "C0A80102" `shouldBe` Just [192, 168, 1, 2]+ it "can fail" do+ f "c0a8010" `shouldBe` Nothing+ f "x0a80102" `shouldBe` Nothing lazilyEncodeHexTests :: Spec-lazilyEncodeHexTests = describe "lazilyEncodeHex" $ do+lazilyEncodeHexTests =+ describe "lazilyEncodeHex" do let f = lazilyEncodeHex . LazyByteString.pack it "can decode part of an infinite list" $- (LazyText.take 8 . f . cycle) [1, 2, 3] `shouldBe` fromString "01020301"+ (LazyText.take 8 . f . cycle) [1, 2, 3] `shouldBe` fromString "01020301"