{-# LANGUAGE CPP #-}
{-# LANGUAGE OverloadedStrings #-}
#if __GLASGOW_HASKELL__ >= 702
{-# LANGUAGE Trustworthy #-}
#endif
module Data.ByteString.Base16.Lazy
( encode
, decode
, decodeLenient
) where
import qualified Data.ByteString as BS
import qualified Data.ByteString.Lazy as LBS
import qualified Data.ByteString.Base16 as B16
import Data.ByteString.Base16.Internal
import Data.ByteString.Lazy.Internal (ByteString(..))
encode :: ByteString -> ByteString
encode :: ByteString -> ByteString
encode ByteString
Empty = ByteString
Empty
encode (Chunk ByteString
c ByteString
cs) = ByteString -> ByteString -> ByteString
Chunk (ByteString -> ByteString
B16.encode ByteString
c) (ByteString -> ByteString
encode ByteString
cs)
decode :: ByteString -> Either String ByteString
decode :: ByteString -> Either String ByteString
decode = forall {a}. Either a ByteString -> Either a ByteString
f forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> Either String ByteString
B16.decode forall b c a. (b -> c) -> (a -> b) -> a -> c
. [ByteString] -> ByteString
BS.concat forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> [ByteString]
LBS.toChunks
where
f :: Either a ByteString -> Either a ByteString
f (Left a
t) = forall a b. a -> Either a b
Left a
t
f (Right ByteString
bs') = forall a b. b -> Either a b
Right ([ByteString] -> ByteString
LBS.fromChunks [ByteString
bs'])
decodeLenient :: ByteString -> ByteString
decodeLenient :: ByteString -> ByteString
decodeLenient = [ByteString] -> ByteString
LBS.fromChunks
forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap ByteString -> ByteString
B16.decodeLenient
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [ByteString] -> [ByteString]
reChunk
forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap ((Word8 -> Bool) -> ByteString -> ByteString
BS.filter (forall a b c. (a -> b -> c) -> b -> a -> c
flip Word8 -> ByteString -> Bool
BS.elem ByteString
extendedHex))
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> [ByteString]
LBS.toChunks
where
extendedHex :: ByteString
extendedHex = [Word8] -> ByteString
BS.pack (forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Char -> Word8
c2w String
"0123456789abcdefABCDEF")