module Data.Double.Conversion.ByteString
(
toExponential
, toFixed
, toPrecision
, toShortest
) where
import Control.Monad (when)
import Foreign.ForeignPtr (withForeignPtr)
import Data.Double.Conversion.FFI
import Data.Word (Word8)
import Data.ByteString.Internal (ByteString(..), mallocByteString)
import Foreign.C.Types (CDouble, CInt)
import Foreign.Ptr (Ptr)
import System.IO.Unsafe (unsafePerformIO)
toExponential :: Int -> Double -> ByteString
toExponential :: Int -> Double -> ByteString
toExponential ndigits :: Int
ndigits = String
-> CInt
-> (CDouble -> Ptr Word8 -> IO CInt)
-> Double
-> ByteString
convert "toExponential" CInt
len ((CDouble -> Ptr Word8 -> IO CInt) -> Double -> ByteString)
-> (CDouble -> Ptr Word8 -> IO CInt) -> Double -> ByteString
forall a b. (a -> b) -> a -> b
$ \val :: CDouble
val mba :: Ptr Word8
mba ->
CDouble -> Ptr Word8 -> CInt -> IO CInt
c_ToExponential CDouble
val Ptr Word8
mba (Int -> CInt
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
ndigits)
where len :: CInt
len = CInt
c_ToExponentialLength
{-# NOINLINE len #-}
toFixed :: Int -> Double -> ByteString
toFixed :: Int -> Double -> ByteString
toFixed ndigits :: Int
ndigits = String
-> CInt
-> (CDouble -> Ptr Word8 -> IO CInt)
-> Double
-> ByteString
convert "toFixed" CInt
len ((CDouble -> Ptr Word8 -> IO CInt) -> Double -> ByteString)
-> (CDouble -> Ptr Word8 -> IO CInt) -> Double -> ByteString
forall a b. (a -> b) -> a -> b
$ \val :: CDouble
val mba :: Ptr Word8
mba ->
CDouble -> Ptr Word8 -> CInt -> IO CInt
c_ToFixed CDouble
val Ptr Word8
mba (Int -> CInt
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
ndigits)
where len :: CInt
len = CInt
c_ToFixedLength
{-# NOINLINE len #-}
toShortest :: Double -> ByteString
toShortest :: Double -> ByteString
toShortest = String
-> CInt
-> (CDouble -> Ptr Word8 -> IO CInt)
-> Double
-> ByteString
convert "toShortest" CInt
len CDouble -> Ptr Word8 -> IO CInt
c_ToShortest
where len :: CInt
len = CInt
c_ToShortestLength
{-# NOINLINE len #-}
toPrecision :: Int -> Double -> ByteString
toPrecision :: Int -> Double -> ByteString
toPrecision ndigits :: Int
ndigits = String
-> CInt
-> (CDouble -> Ptr Word8 -> IO CInt)
-> Double
-> ByteString
convert "toPrecision" CInt
len ((CDouble -> Ptr Word8 -> IO CInt) -> Double -> ByteString)
-> (CDouble -> Ptr Word8 -> IO CInt) -> Double -> ByteString
forall a b. (a -> b) -> a -> b
$ \val :: CDouble
val mba :: Ptr Word8
mba ->
CDouble -> Ptr Word8 -> CInt -> IO CInt
c_ToPrecision CDouble
val Ptr Word8
mba (Int -> CInt
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
ndigits)
where len :: CInt
len = CInt
c_ToPrecisionLength
{-# NOINLINE len #-}
convert :: String -> CInt -> (CDouble -> Ptr Word8 -> IO CInt)
-> Double -> ByteString
convert :: String
-> CInt
-> (CDouble -> Ptr Word8 -> IO CInt)
-> Double
-> ByteString
convert func :: String
func len :: CInt
len act :: CDouble -> Ptr Word8 -> IO CInt
act val :: Double
val = IO ByteString -> ByteString
forall a. IO a -> a
unsafePerformIO (IO ByteString -> ByteString) -> IO ByteString -> ByteString
forall a b. (a -> b) -> a -> b
$ do
ForeignPtr Word8
fp <- Int -> IO (ForeignPtr Word8)
forall a. Int -> IO (ForeignPtr a)
mallocByteString (CInt -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral CInt
len)
CInt
size <- ForeignPtr Word8 -> (Ptr Word8 -> IO CInt) -> IO CInt
forall a b. ForeignPtr a -> (Ptr a -> IO b) -> IO b
withForeignPtr ForeignPtr Word8
fp ((Ptr Word8 -> IO CInt) -> IO CInt)
-> (Ptr Word8 -> IO CInt) -> IO CInt
forall a b. (a -> b) -> a -> b
$ CDouble -> Ptr Word8 -> IO CInt
act (Double -> CDouble
forall a b. (Real a, Fractional b) => a -> b
realToFrac Double
val)
Bool -> IO () -> IO ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (CInt
size CInt -> CInt -> Bool
forall a. Eq a => a -> a -> Bool
== -1) (IO () -> IO ()) -> (String -> IO ()) -> String -> IO ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
String -> IO ()
forall (m :: * -> *) a. MonadFail m => String -> m a
fail (String -> IO ()) -> String -> IO ()
forall a b. (a -> b) -> a -> b
$ "Data.Double.Conversion.ByteString." String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
func String -> String -> String
forall a. [a] -> [a] -> [a]
++
": conversion failed (invalid precision requested)"
ByteString -> IO ByteString
forall (m :: * -> *) a. Monad m => a -> m a
return (ByteString -> IO ByteString) -> ByteString -> IO ByteString
forall a b. (a -> b) -> a -> b
$ ForeignPtr Word8 -> Int -> Int -> ByteString
PS ForeignPtr Word8
fp 0 (CInt -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral CInt
size)