-- |
-- Module      : Data.Double.Conversion.ByteString
-- Copyright   : (c) 2011 MailRank, Inc.
--
-- License     : BSD-style
-- Maintainer  : bos@serpentine.com
-- Stability   : experimental
-- Portability : GHC
--
-- Fast, efficient support for converting between double precision
-- floating point values and text.
--
-- Although about 15 times faster than plain 'show', these functions
-- are /slower/ than their 'Text' counterparts, at roughly half the
-- speed.  (This seems to be due to the cost of allocating
-- 'ByteString' values via @malloc@.)

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)

-- | Compute a representation in exponential format with the requested
-- number of digits after the decimal point. The last emitted digit is
-- rounded.  If -1 digits are requested, then the shortest exponential
-- representation is computed.
toExponential :: Int -> Double -> ByteString
toExponential :: Int -> Double -> ByteString
toExponential Int
ndigits = String
-> CInt
-> (CDouble -> Ptr Word8 -> IO CInt)
-> Double
-> ByteString
convert String
"toExponential" CInt
len ((CDouble -> Ptr Word8 -> IO CInt) -> Double -> ByteString)
-> (CDouble -> Ptr Word8 -> IO CInt) -> Double -> ByteString
forall a b. (a -> b) -> a -> b
$ \CDouble
val 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 #-}

-- | Compute a decimal representation with a fixed number of digits
-- after the decimal point. The last emitted digit is rounded.
toFixed :: Int -> Double -> ByteString
toFixed :: Int -> Double -> ByteString
toFixed Int
ndigits = String
-> CInt
-> (CDouble -> Ptr Word8 -> IO CInt)
-> Double
-> ByteString
convert String
"toFixed" CInt
len ((CDouble -> Ptr Word8 -> IO CInt) -> Double -> ByteString)
-> (CDouble -> Ptr Word8 -> IO CInt) -> Double -> ByteString
forall a b. (a -> b) -> a -> b
$ \CDouble
val 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 #-}

-- | Compute the shortest string of digits that correctly represent
-- the input number.
toShortest :: Double -> ByteString
toShortest :: Double -> ByteString
toShortest = String
-> CInt
-> (CDouble -> Ptr Word8 -> IO CInt)
-> Double
-> ByteString
convert String
"toShortest" CInt
len CDouble -> Ptr Word8 -> IO CInt
c_ToShortest
  where len :: CInt
len = CInt
c_ToShortestLength
        {-# NOINLINE len #-}

-- | Compute @precision@ leading digits of the given value either in
-- exponential or decimal format. The last computed digit is rounded.
toPrecision :: Int -> Double -> ByteString
toPrecision :: Int -> Double -> ByteString
toPrecision Int
ndigits = String
-> CInt
-> (CDouble -> Ptr Word8 -> IO CInt)
-> Double
-> ByteString
convert String
"toPrecision" CInt
len ((CDouble -> Ptr Word8 -> IO CInt) -> Double -> ByteString)
-> (CDouble -> Ptr Word8 -> IO CInt) -> Double -> ByteString
forall a b. (a -> b) -> a -> b
$ \CDouble
val 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 String
func CInt
len CDouble -> Ptr Word8 -> IO CInt
act 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
== -CInt
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
$ String
"Data.Double.Conversion.ByteString." String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
func String -> String -> String
forall a. [a] -> [a] -> [a]
++
           String
": 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 Int
0 (CInt -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral CInt
size)