{-# LANGUAGE OverloadedStrings #-}
module Data.MIME.Internal
(
hexEncode
, parseHex
) where
import Data.Bits ((.&.), shiftR)
import Data.Word (Word8)
import qualified Data.ByteString as B
parseHex :: Word8 -> Maybe Word8
parseHex :: Word8 -> Maybe Word8
parseHex Word8
c = do
let
c' :: Word8
c' = if Word8
c Word8 -> Word8 -> Bool
forall a. Ord a => a -> a -> Bool
>= Word8
0x61 Bool -> Bool -> Bool
&& Word8
c Word8 -> Word8 -> Bool
forall a. Ord a => a -> a -> Bool
<= Word8
0x7a then Word8
c Word8 -> Word8 -> Word8
forall a. Num a => a -> a -> a
- Word8
0x20 else Word8
c
Int -> Word8
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Int -> Word8) -> Maybe Int -> Maybe Word8
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (Word8 -> Bool) -> ByteString -> Maybe Int
B.findIndex (Word8 -> Word8 -> Bool
forall a. Eq a => a -> a -> Bool
== Word8
c') ByteString
hexAlphabet
hexAlphabet :: B.ByteString
hexAlphabet :: ByteString
hexAlphabet = ByteString
"0123456789ABCDEF"
hexEncode :: Word8 -> (Word8, Word8)
hexEncode :: Word8 -> (Word8, Word8)
hexEncode Word8
c =
let
lkup :: a -> Word8
lkup a
i = ByteString -> Int -> Word8
B.index ByteString
hexAlphabet (a -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral a
i)
in
( Word8 -> Word8
forall a. Integral a => a -> Word8
lkup (Word8
c Word8 -> Int -> Word8
forall a. Bits a => a -> Int -> a
`shiftR` Int
4)
, Word8 -> Word8
forall a. Integral a => a -> Word8
lkup (Word8
c Word8 -> Word8 -> Word8
forall a. Bits a => a -> a -> a
.&. Word8
0x0f)
)