{-# LANGUAGE CPP #-}-- |-- Copyright : (c) 2011 Simon Meier-- License : BSD3-style (see LICENSE)---- Maintainer : Simon Meier <iridcode@gmail.com>-- Stability : experimental-- Portability : GHC---- Hexadecimal encoding of nibbles (4-bit) and octets (8-bit) as ASCII-- characters.---- The current implementation is based on a table based encoding inspired by-- the code in the 'base64-bytestring' library by Bryan O'Sullivan. In our-- benchmarks on a 32-bit machine it turned out to be the fastest-- implementation option.--moduleData.ByteString.Builder.Prim.Internal.Base16(EncodingTable ,lowerTable ,encode8_as_16h )whereimportForeignimportGHC.Exts(Addr#,Ptr(..)) #if PURE_HASKELL importqualifiedData.ByteString.Internal.PureasPure #else importForeign.C.Types #endif -- Creating the encoding table-------------------------------- | An encoding table for Base16 encoding.dataEncodingTable =EncodingTable Addr#-- | The encoding table for hexadecimal values with lower-case characters;-- e.g., deadbeef.lowerTable ::EncodingTable lowerTable :: EncodingTable lowerTable = #if PURE_HASKELL casePure.lower_hex_tableofPtrp#->EncodingTablep# #else casePtr CChar c_lower_hex_table ofPtrAddr# p# ->Addr# -> EncodingTable EncodingTable Addr# p# foreignimportccall"&hs_bytestring_lower_hex_table"c_lower_hex_table ::PtrCChar #endif -- | Encode an octet as 16bit word comprising both encoded nibbles ordered-- according to the host endianness. Writing these 16bit to memory will write-- the nibbles in the correct order (i.e. big-endian).{-# INLINEencode8_as_16h #-}encode8_as_16h ::EncodingTable ->Word8->IOWord16encode8_as_16h :: EncodingTable -> Word8 -> IO Word16 encode8_as_16h (EncodingTable Addr# table )=Ptr Word16 -> Int -> IO Word16 forall a. Storable a => Ptr a -> Int -> IO a peekElemOff(Addr# -> Ptr Word16 forall a. Addr# -> Ptr a PtrAddr# table )(Int -> IO Word16) -> (Word8 -> Int) -> Word8 -> IO Word16 forall b c a. (b -> c) -> (a -> b) -> a -> c .Word8 -> Int forall a b. (Integral a, Num b) => a -> b fromIntegral