{-# LANGUAGE CPP, MagicHash, Rank2Types, TypeFamilies, TypeOperators #-}
-- |
-- Module      : Data.Double.Conversion.TextBuilder
-- 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.
--

module Data.Double.Conversion.Internal.TextBuilder
    (
      convert
    ) where

import Control.Monad (when)
#if MIN_VERSION_base(4,4,0)
import Control.Monad.ST.Unsafe (unsafeIOToST)
#else
import Control.Monad.ST (unsafeIOToST)
#endif
import Data.Double.Conversion.Internal.FFI (ForeignFloating)
import qualified Data.Text.Array as A
import Data.Text.Internal.Builder (Builder, writeN)
import Foreign.C.Types (CDouble, CFloat, CInt)
import GHC.Prim (MutableByteArray#)
import Control.Monad.ST (runST)

convert :: (RealFloat a, RealFloat b, b ~ ForeignFloating a) => String -> CInt
        -> (forall s. b -> MutableByteArray# s -> IO CInt)
        -> a -> Builder
{-# SPECIALIZE convert :: String -> CInt -> (forall s. CDouble -> MutableByteArray# s -> IO CInt) -> Double -> Builder #-}
{-# SPECIALIZE convert :: String -> CInt -> (forall s. CFloat -> MutableByteArray# s -> IO CInt) -> Float -> Builder #-}
{-# INLINABLE convert #-}
convert :: forall a b.
(RealFloat a, RealFloat b, b ~ ForeignFloating a) =>
String
-> CInt
-> (forall s. b -> MutableByteArray# s -> IO CInt)
-> a
-> Builder
convert String
func CInt
len forall s. b -> MutableByteArray# s -> IO CInt
act a
val = (forall s. ST s Builder) -> Builder
forall a. (forall s. ST s a) -> a
runST ((forall s. ST s Builder) -> Builder)
-> (forall s. ST s Builder) -> Builder
forall a b. (a -> b) -> a -> b
$ do 
#if MIN_VERSION_text(2,0,0)
  mTempArr :: MArray s
mTempArr@(A.MutableByteArray MutableByteArray# s
tempMArr) <- Int -> ST s (MArray s)
forall s. Int -> ST s (MArray s)
A.new (CInt -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral CInt
len)
#else
  mTempArr@(A.MArray tempMArr) <- A.new (fromIntegral len)
#endif
  CInt
size <- IO CInt -> ST s CInt
forall a s. IO a -> ST s a
unsafeIOToST (IO CInt -> ST s CInt) -> IO CInt -> ST s CInt
forall a b. (a -> b) -> a -> b
$ b -> MutableByteArray# s -> IO CInt
forall s. b -> MutableByteArray# s -> IO CInt
act (a -> b
forall a b. (Real a, Fractional b) => a -> b
realToFrac a
val) MutableByteArray# s
tempMArr
  Array
tempArr <- MArray s -> ST s Array
forall s. MArray s -> ST s Array
A.unsafeFreeze MArray s
mTempArr
  Bool -> ST s () -> ST s ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (CInt
size CInt -> CInt -> Bool
forall a. Eq a => a -> a -> Bool
== -CInt
1) (ST s () -> ST s ()) -> (String -> ST s ()) -> String -> ST s ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
      String -> ST s ()
forall a. HasCallStack => String -> a
error (String -> ST s ()) -> String -> ST s ()
forall a b. (a -> b) -> a -> b
$ String
"Data.Double.Conversion.Text." String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
func String -> String -> String
forall a. [a] -> [a] -> [a]
++
              String
": conversion failed."
#if MIN_VERSION_text(2,0,0)
  Builder -> ST s Builder
forall a. a -> ST s a
forall (m :: * -> *) a. Monad m => a -> m a
return (Builder -> ST s Builder) -> Builder -> ST s Builder
forall a b. (a -> b) -> a -> b
$ Int -> (forall s. MArray s -> Int -> ST s ()) -> Builder
writeN (CInt -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral CInt
size) ((forall s. MArray s -> Int -> ST s ()) -> Builder)
-> (forall s. MArray s -> Int -> ST s ()) -> Builder
forall a b. (a -> b) -> a -> b
$ \MArray s
mArr Int
_ -> Int -> MArray s -> Int -> Array -> Int -> ST s ()
forall s. Int -> MArray s -> Int -> Array -> Int -> ST s ()
A.copyI (CInt -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral CInt
size) MArray s
mArr Int
0 Array
tempArr Int
0
#else
  return $ writeN (fromIntegral size) $ \mArr _ -> A.copyI mArr 0 tempArr 0 (fromIntegral size)
#endif