{-# LANGUAGE OverloadedStrings #-}
module Data.ByteString.Base16
( encodeBase16
, encodeBase16'
, decodeBase16
, decodeBase16'
, decodeBase16Untyped
, decodeBase16Lenient
, isBase16
, isValidBase16
) where
import Prelude hiding (all, elem)
import Data.Base16.Types
import Data.ByteString (ByteString, all, elem)
import Data.ByteString.Base16.Internal.Head
import Data.Either
import Data.Text (Text)
import qualified Data.Text.Encoding as T
encodeBase16 :: ByteString -> Base16 Text
encodeBase16 :: ByteString -> Base16 Text
encodeBase16 = (ByteString -> Text) -> Base16 ByteString -> Base16 Text
forall a b. (a -> b) -> Base16 a -> Base16 b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap ByteString -> Text
T.decodeUtf8 (Base16 ByteString -> Base16 Text)
-> (ByteString -> Base16 ByteString) -> ByteString -> Base16 Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> Base16 ByteString
encodeBase16'
{-# INLINE encodeBase16 #-}
encodeBase16' :: ByteString -> Base16 ByteString
encodeBase16' :: ByteString -> Base16 ByteString
encodeBase16' = ByteString -> Base16 ByteString
forall a. a -> Base16 a
assertBase16 (ByteString -> Base16 ByteString)
-> (ByteString -> ByteString) -> ByteString -> Base16 ByteString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> ByteString
encodeBase16_
{-# INLINE encodeBase16' #-}
decodeBase16 :: Base16 ByteString -> ByteString
decodeBase16 :: Base16 ByteString -> ByteString
decodeBase16 = Base16 ByteString -> ByteString
decodeBase16Typed_
{-# INLINE decodeBase16 #-}
decodeBase16' :: Base16 Text -> ByteString
decodeBase16' :: Base16 Text -> ByteString
decodeBase16' = Base16 ByteString -> ByteString
decodeBase16Typed_ (Base16 ByteString -> ByteString)
-> (Base16 Text -> Base16 ByteString) -> Base16 Text -> ByteString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Text -> ByteString) -> Base16 Text -> Base16 ByteString
forall a b. (a -> b) -> Base16 a -> Base16 b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Text -> ByteString
T.encodeUtf8
{-# INLINE decodeBase16' #-}
decodeBase16Untyped :: ByteString -> Either Text ByteString
decodeBase16Untyped :: ByteString -> Either Text ByteString
decodeBase16Untyped = ByteString -> Either Text ByteString
decodeBase16_
{-# INLINE decodeBase16Untyped #-}
decodeBase16Lenient :: ByteString -> ByteString
decodeBase16Lenient :: ByteString -> ByteString
decodeBase16Lenient = ByteString -> ByteString
decodeBase16Lenient_
{-# INLINE decodeBase16Lenient #-}
isBase16 :: ByteString -> Bool
isBase16 :: ByteString -> Bool
isBase16 ByteString
bs = ByteString -> Bool
isValidBase16 ByteString
bs Bool -> Bool -> Bool
&& Either Text ByteString -> Bool
forall a b. Either a b -> Bool
isRight (ByteString -> Either Text ByteString
decodeBase16Untyped ByteString
bs)
{-# INLINE isBase16 #-}
isValidBase16 :: ByteString -> Bool
isValidBase16 :: ByteString -> Bool
isValidBase16 = (Word8 -> Bool) -> ByteString -> Bool
all (Word8 -> ByteString -> Bool
`elem` ByteString
"0123456789abcdefABCDEF")
{-# INLINE isValidBase16 #-}