{-# LANGUAGE CPP #-}{-# LANGUAGE ScopedTypeVariables, ForeignFunctionInterface #-}#if __GLASGOW_HASKELL__ >= 701 {-# LANGUAGE Trustworthy #-}#endif -- | Copyright : (c) 2010 Jasper Van der Jeugt-- (c) 2010 - 2011 Simon Meier-- License : BSD3-style (see LICENSE)---- Maintainer : Simon Meier <iridcode@gmail.com>-- Portability : GHC---- Encodings using ASCII encoded Unicode characters.--moduleData.ByteString.Builder.Prim.ASCII(-- *** ASCIIchar7 -- **** Decimal numbers-- | Decimal encoding of numbers using ASCII encoded characters.,int8Dec ,int16Dec ,int32Dec ,int64Dec ,intDec ,word8Dec ,word16Dec ,word32Dec ,word64Dec ,wordDec {- -- These are the functions currently provided by Bryan O'Sullivans -- double-conversion library. -- -- , float -- , floatWith -- , double -- , doubleWith -}-- **** Hexadecimal numbers-- | Encoding positive integers as hexadecimal numbers using lower-case-- ASCII characters. The shortest possible representation is used. For-- example,---- > toLazyByteString (primBounded word16Hex 0x0a10) = "a10"---- Note that there is no support for using upper-case characters. Please-- contact the maintainer if your application cannot work without-- hexadecimal encodings that use upper-case characters.--,word8Hex ,word16Hex ,word32Hex ,word64Hex ,wordHex -- **** Fixed-width hexadecimal numbers---- | Encoding the bytes of fixed-width types as hexadecimal-- numbers using lower-case ASCII characters. For example,---- > toLazyByteString (primFixed word16HexFixed 0x0a10) = "0a10"--,int8HexFixed ,int16HexFixed ,int32HexFixed ,int64HexFixed ,word8HexFixed ,word16HexFixed ,word32HexFixed ,word64HexFixed ,floatHexFixed ,doubleHexFixed )whereimportData.ByteString.Builder.Prim.Binary importData.ByteString.Builder.Prim.Internal importData.ByteString.Builder.Prim.Internal.Floating importData.ByteString.Builder.Prim.Internal.Base16 importData.ByteString.Builder.Prim.Internal.UncheckedShifts importData.Char(ord)importForeignimportForeign.C.Types-- | Encode the least 7-bits of a 'Char' using the ASCII encoding.{-# INLINEchar7#-}char7::FixedPrim Charchar7 =(\c ->fromIntegral$ordc .&.0x7f)>$< word8 -------------------------------------------------------------------------------- Decimal Encoding-------------------------------------------------------------------------------- Signed integers------------------foreignimportccallunsafe"static _hs_bytestring_int_dec"c_int_dec::CInt->PtrWord8->IO(PtrWord8)foreignimportccallunsafe"static _hs_bytestring_long_long_int_dec"c_long_long_int_dec::CLLong->PtrWord8->IO(PtrWord8){-# INLINEencodeIntDecimal#-}encodeIntDecimal::Integrala =>Int->BoundedPrim a encodeIntDecimal bound =boudedPrim bound $c_int_dec .fromIntegral-- | Decimal encoding of an 'Int8'.{-# INLINEint8Dec#-}int8Dec::BoundedPrim Int8int8Dec =encodeIntDecimal 4-- | Decimal encoding of an 'Int16'.{-# INLINEint16Dec#-}int16Dec::BoundedPrim Int16int16Dec =encodeIntDecimal 6-- | Decimal encoding of an 'Int32'.{-# INLINEint32Dec#-}int32Dec::BoundedPrim Int32int32Dec =encodeIntDecimal 11-- | Decimal encoding of an 'Int64'.{-# INLINEint64Dec#-}int64Dec::BoundedPrim Int64int64Dec =boudedPrim 20$c_long_long_int_dec .fromIntegral-- | Decimal encoding of an 'Int'.{-# INLINEintDec#-}intDec::BoundedPrim IntintDec =caseWordSize_32_64 (fromIntegral>$< int32Dec )(fromIntegral>$< int64Dec )-- Unsigned integers--------------------foreignimportccallunsafe"static _hs_bytestring_uint_dec"c_uint_dec::CUInt->PtrWord8->IO(PtrWord8)foreignimportccallunsafe"static _hs_bytestring_long_long_uint_dec"c_long_long_uint_dec::CULLong->PtrWord8->IO(PtrWord8){-# INLINEencodeWordDecimal#-}encodeWordDecimal::Integrala =>Int->BoundedPrim a encodeWordDecimal bound =boudedPrim bound $c_uint_dec .fromIntegral-- | Decimal encoding of a 'Word8'.{-# INLINEword8Dec#-}word8Dec::BoundedPrim Word8word8Dec =encodeWordDecimal 3-- | Decimal encoding of a 'Word16'.{-# INLINEword16Dec#-}word16Dec::BoundedPrim Word16word16Dec =encodeWordDecimal 5-- | Decimal encoding of a 'Word32'.{-# INLINEword32Dec#-}word32Dec::BoundedPrim Word32word32Dec =encodeWordDecimal 10-- | Decimal encoding of a 'Word64'.{-# INLINEword64Dec#-}word64Dec::BoundedPrim Word64word64Dec =boudedPrim 20$c_long_long_uint_dec .fromIntegral-- | Decimal encoding of a 'Word'.{-# INLINEwordDec#-}wordDec::BoundedPrim WordwordDec =caseWordSize_32_64 (fromIntegral>$< word32Dec )(fromIntegral>$< word64Dec )-------------------------------------------------------------------------------- Hexadecimal Encoding-------------------------------------------------------------------------------- without lead---------------foreignimportccallunsafe"static _hs_bytestring_uint_hex"c_uint_hex::CUInt->PtrWord8->IO(PtrWord8)foreignimportccallunsafe"static _hs_bytestring_long_long_uint_hex"c_long_long_uint_hex::CULLong->PtrWord8->IO(PtrWord8){-# INLINEencodeWordHex#-}encodeWordHex::foralla .(Storablea ,Integrala )=>BoundedPrim a encodeWordHex =boudedPrim (2*sizeOf(undefined::a ))$c_uint_hex .fromIntegral-- | Hexadecimal encoding of a 'Word8'.{-# INLINEword8Hex#-}word8Hex::BoundedPrim Word8word8Hex =encodeWordHex -- | Hexadecimal encoding of a 'Word16'.{-# INLINEword16Hex#-}word16Hex::BoundedPrim Word16word16Hex =encodeWordHex -- | Hexadecimal encoding of a 'Word32'.{-# INLINEword32Hex#-}word32Hex::BoundedPrim Word32word32Hex =encodeWordHex -- | Hexadecimal encoding of a 'Word64'.{-# INLINEword64Hex#-}word64Hex::BoundedPrim Word64word64Hex =boudedPrim 16$c_long_long_uint_hex .fromIntegral-- | Hexadecimal encoding of a 'Word'.{-# INLINEwordHex#-}wordHex::BoundedPrim WordwordHex =caseWordSize_32_64 (fromIntegral>$< word32Hex )(fromIntegral>$< word64Hex )-- fixed width; leading zeroes-------------------------------- | Encode a 'Word8' using 2 nibbles (hexadecimal digits).{-# INLINEword8HexFixed#-}word8HexFixed::FixedPrim Word8word8HexFixed =fixedPrim 2$\x op ->poke(castPtrop )=<<encode8_as_16h lowerTable x -- | Encode a 'Word16' using 4 nibbles.{-# INLINEword16HexFixed#-}word16HexFixed::FixedPrim Word16word16HexFixed =(\x ->(fromIntegral$x `shiftr_w16 `8,fromIntegralx ))>$< pairF word8HexFixed word8HexFixed -- | Encode a 'Word32' using 8 nibbles.{-# INLINEword32HexFixed#-}word32HexFixed::FixedPrim Word32word32HexFixed =(\x ->(fromIntegral$x `shiftr_w32 `16,fromIntegralx ))>$< pairF word16HexFixed word16HexFixed -- | Encode a 'Word64' using 16 nibbles.{-# INLINEword64HexFixed#-}word64HexFixed::FixedPrim Word64word64HexFixed =(\x ->(fromIntegral$x `shiftr_w64 `32,fromIntegralx ))>$< pairF word32HexFixed word32HexFixed -- | Encode a 'Int8' using 2 nibbles (hexadecimal digits).{-# INLINEint8HexFixed#-}int8HexFixed::FixedPrim Int8int8HexFixed =fromIntegral>$< word8HexFixed -- | Encode a 'Int16' using 4 nibbles.{-# INLINEint16HexFixed#-}int16HexFixed::FixedPrim Int16int16HexFixed =fromIntegral>$< word16HexFixed -- | Encode a 'Int32' using 8 nibbles.{-# INLINEint32HexFixed#-}int32HexFixed::FixedPrim Int32int32HexFixed =fromIntegral>$< word32HexFixed -- | Encode a 'Int64' using 16 nibbles.{-# INLINEint64HexFixed#-}int64HexFixed::FixedPrim Int64int64HexFixed =fromIntegral>$< word64HexFixed -- | Encode an IEEE 'Float' using 8 nibbles.{-# INLINEfloatHexFixed#-}floatHexFixed::FixedPrim FloatfloatHexFixed =encodeFloatViaWord32F word32HexFixed -- | Encode an IEEE 'Double' using 16 nibbles.{-# INLINEdoubleHexFixed#-}doubleHexFixed::FixedPrim DoubledoubleHexFixed =encodeDoubleViaWord64F word64HexFixed