module Text.Show.ByteString.Util ( putAscii
, putUTF8
, putAsciiStr
, putUTF8Str
, unsafePutDigit
, unsafePutDigit#
) where
import GHC.Base
import GHC.Word
import Data.Binary
import Data.Bits
import Data.ByteString.Internal (c2w)
putAscii :: Char -> Put
putAscii = putWord8 . c2w
putUTF8 :: Char -> Put
putUTF8 c
| oc <= 0x7f = putWord8 (fromIntegral oc)
| oc <= 0x7ff = do putWord8 . fromIntegral $ 0xc0 + (oc `shiftR` 6)
putWord8 . fromIntegral $ 0x80 + oc .&. 0x3f
| oc <= 0xffff = do putWord8 . fromIntegral $ 0xf0 + (oc `shiftR` 12)
putWord8 . fromIntegral $ 0x80 + ((oc `shiftR` 6) .&. 0x3f)
putWord8 . fromIntegral $ 0x80 + oc .&. 0x3f
| otherwise = do putWord8 . fromIntegral $ 0xf0 + (oc `shiftR` 18)
putWord8 . fromIntegral $ 0x80 + ((oc `shiftR` 12) .&. 0x3f)
putWord8 . fromIntegral $ 0x80 + ((oc `shiftR` 6) .&. 0x3f)
putWord8 . fromIntegral $ 0x80 + oc .&. 0x3f
where
oc = ord c
putAsciiStr :: String -> Put
putAsciiStr = mapM_ putAscii
putUTF8Str :: String -> Put
putUTF8Str = mapM_ putUTF8
unsafePutDigit :: Int -> Put
unsafePutDigit (I# i#) = unsafePutDigit# (int2Word# i#)
unsafePutDigit# :: Word# -> Put
unsafePutDigit# w# = putWord8 (W8# (w# `plusWord#` int2Word# 48#))