{-# LANGUAGE MagicHash #-}
{-# LANGUAGE UnboxedTuples #-}
{-# LANGUAGE BangPatterns #-}
module Basement.Base16
( unsafeConvertByte
, hexWord16
, hexWord32
, escapeByte
, Base16Escape(..)
) where
import GHC.Prim
import GHC.Types
import GHC.Word
import Basement.Types.Char7
data Base16Escape = Base16Escape {-# UNPACK #-} !Char7 {-# UNPACK #-} !Char7
unsafeConvertByte :: Word# -> (# Word#, Word# #)
unsafeConvertByte :: Word# -> (# Word#, Word# #)
unsafeConvertByte Word#
b = (# Table -> Word# -> Word#
r Table
tableHi Word#
b, Table -> Word# -> Word#
r Table
tableLo Word#
b #)
where
r :: Table -> Word# -> Word#
r :: Table -> Word# -> Word#
r (Table !Addr#
table) Word#
index = Addr# -> Int# -> Word#
indexWord8OffAddr# Addr#
table (Word# -> Int#
word2Int# Word#
index)
{-# INLINE unsafeConvertByte #-}
escapeByte :: Word8 -> Base16Escape
escapeByte :: Word8 -> Base16Escape
escapeByte !(W8# Word#
b) = Char7 -> Char7 -> Base16Escape
Base16Escape (Table -> Word# -> Char7
r Table
tableHi Word#
b) (Table -> Word# -> Char7
r Table
tableLo Word#
b)
where
r :: Table -> Word# -> Char7
r :: Table -> Word# -> Char7
r (Table !Addr#
table) Word#
index = Word8 -> Char7
Char7 (Word# -> Word8
W8# (Addr# -> Int# -> Word#
indexWord8OffAddr# Addr#
table (Word# -> Int#
word2Int# Word#
index)))
{-# INLINE escapeByte #-}
hexWord16 :: Word16 -> (Char, Char, Char, Char)
hexWord16 :: Word16 -> (Char, Char, Char, Char)
hexWord16 (W16# Word#
w) = (Word# -> Char
toChar Word#
w1,Word# -> Char
toChar Word#
w2,Word# -> Char
toChar Word#
w3,Word# -> Char
toChar Word#
w4)
where
toChar :: Word# -> Char
toChar :: Word# -> Char
toChar Word#
c = Char# -> Char
C# (Int# -> Char#
chr# (Word# -> Int#
word2Int# Word#
c))
!(# Word#
w1, Word#
w2 #) = Word# -> (# Word#, Word# #)
unsafeConvertByte (Word# -> Int# -> Word#
uncheckedShiftRL# Word#
w Int#
8#)
!(# Word#
w3, Word#
w4 #) = Word# -> (# Word#, Word# #)
unsafeConvertByte (Word# -> Word# -> Word#
and# Word#
w Word#
0xff##)
hexWord32 :: Word32 -> (Char, Char, Char, Char, Char, Char, Char, Char)
hexWord32 :: Word32 -> (Char, Char, Char, Char, Char, Char, Char, Char)
hexWord32 (W32# Word#
w) = (Word# -> Char
toChar Word#
w1,Word# -> Char
toChar Word#
w2,Word# -> Char
toChar Word#
w3,Word# -> Char
toChar Word#
w4
,Word# -> Char
toChar Word#
w5,Word# -> Char
toChar Word#
w6,Word# -> Char
toChar Word#
w7,Word# -> Char
toChar Word#
w8)
where
toChar :: Word# -> Char
toChar :: Word# -> Char
toChar Word#
c = Char# -> Char
C# (Int# -> Char#
chr# (Word# -> Int#
word2Int# Word#
c))
!(# Word#
w1, Word#
w2 #) = Word# -> (# Word#, Word# #)
unsafeConvertByte (Word# -> Int# -> Word#
uncheckedShiftRL# Word#
w Int#
24#)
!(# Word#
w3, Word#
w4 #) = Word# -> (# Word#, Word# #)
unsafeConvertByte (Word# -> Word# -> Word#
and# (Word# -> Int# -> Word#
uncheckedShiftRL# Word#
w Int#
16#) Word#
0xff##)
!(# Word#
w5, Word#
w6 #) = Word# -> (# Word#, Word# #)
unsafeConvertByte (Word# -> Word# -> Word#
and# (Word# -> Int# -> Word#
uncheckedShiftRL# Word#
w Int#
8#) Word#
0xff##)
!(# Word#
w7, Word#
w8 #) = Word# -> (# Word#, Word# #)
unsafeConvertByte (Word# -> Word# -> Word#
and# Word#
w Word#
0xff##)
data Table = Table Addr#
tableLo:: Table
tableLo :: Table
tableLo = Addr# -> Table
Table
Addr#
"0123456789abcdef0123456789abcdef\
\0123456789abcdef0123456789abcdef\
\0123456789abcdef0123456789abcdef\
\0123456789abcdef0123456789abcdef\
\0123456789abcdef0123456789abcdef\
\0123456789abcdef0123456789abcdef\
\0123456789abcdef0123456789abcdef\
\0123456789abcdef0123456789abcdef"#
tableHi :: Table
tableHi :: Table
tableHi = Addr# -> Table
Table
Addr#
"00000000000000001111111111111111\
\22222222222222223333333333333333\
\44444444444444445555555555555555\
\66666666666666667777777777777777\
\88888888888888889999999999999999\
\aaaaaaaaaaaaaaaabbbbbbbbbbbbbbbb\
\ccccccccccccccccdddddddddddddddd\
\eeeeeeeeeeeeeeeeffffffffffffffff"#