7
7
-- Stability : experimental
8
8
-- Portability : GHC
9
9
--
10
+ -- This module left now only for compatibility and should not be used
11
+ -- in new projects.
12
+ -- Please, use Convertable type class from Data.Double.Conversion.Convertable
13
+ --
10
14
-- Fast, efficient support for converting between double precision
11
15
-- floating point values and text.
12
16
--
@@ -23,54 +27,27 @@ module Data.Double.Conversion.ByteString
23
27
, toShortest
24
28
) where
25
29
26
- import Control.Monad (when )
27
- import Foreign.ForeignPtr (withForeignPtr )
28
- import Data.Double.Conversion.FFI
29
- import Data.Word (Word8 )
30
- import Data.ByteString.Internal (ByteString (.. ), mallocByteString )
31
- import Foreign.C.Types (CDouble , CInt )
32
- import Foreign.Ptr (Ptr )
33
- import System.IO.Unsafe (unsafePerformIO )
30
+ import qualified Data.Double.Conversion.Convertable
31
+ import Data.ByteString.Internal
34
32
35
33
-- | Compute a representation in exponential format with the requested
36
34
-- number of digits after the decimal point. The last emitted digit is
37
35
-- rounded. If -1 digits are requested, then the shortest exponential
38
36
-- representation is computed.
39
37
toExponential :: Int -> Double -> ByteString
40
- toExponential ndigits = convert " toExponential" len $ \ val mba ->
41
- c_ToExponential val mba (fromIntegral ndigits)
42
- where len = c_ToExponentialLength
43
- {-# NOINLINE len #-}
38
+ toExponential = Data.Double.Conversion.Convertable. toExponential
44
39
45
40
-- | Compute a decimal representation with a fixed number of digits
46
41
-- after the decimal point. The last emitted digit is rounded.
47
42
toFixed :: Int -> Double -> ByteString
48
- toFixed ndigits = convert " toFixed" len $ \ val mba ->
49
- c_ToFixed val mba (fromIntegral ndigits)
50
- where len = c_ToFixedLength
51
- {-# NOINLINE len #-}
43
+ toFixed = Data.Double.Conversion.Convertable. toFixed
52
44
53
45
-- | Compute the shortest string of digits that correctly represent
54
46
-- the input number.
55
47
toShortest :: Double -> ByteString
56
- toShortest = convert " toShortest" len c_ToShortest
57
- where len = c_ToShortestLength
58
- {-# NOINLINE len #-}
48
+ toShortest = Data.Double.Conversion.Convertable. toShortest
59
49
60
50
-- | Compute @precision@ leading digits of the given value either in
61
51
-- exponential or decimal format. The last computed digit is rounded.
62
52
toPrecision :: Int -> Double -> ByteString
63
- toPrecision ndigits = convert " toPrecision" len $ \ val mba ->
64
- c_ToPrecision val mba (fromIntegral ndigits)
65
- where len = c_ToPrecisionLength
66
- {-# NOINLINE len #-}
67
-
68
- convert :: String -> CInt -> (CDouble -> Ptr Word8 -> IO CInt )
69
- -> Double -> ByteString
70
- convert func len act val = unsafePerformIO $ do
71
- fp <- mallocByteString (fromIntegral len)
72
- size <- withForeignPtr fp $ act (realToFrac val)
73
- when (size == - 1 ) .
74
- fail $ " Data.Double.Conversion.ByteString." ++ func ++
75
- " : conversion failed (invalid precision requested)"
76
- return $ PS fp 0 (fromIntegral size)
53
+ toPrecision = Data.Double.Conversion.Convertable. toPrecision
0 commit comments