Skip to content

Commit 99a5af6

Browse files
Merge pull request #22 from Haskell-mouse/master
Added conversion to builder (text and bytestring). Added conversion from Float.
2 parents e4b7956 + 2a30c5f commit 99a5af6

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

65 files changed

+7532
-358
lines changed

.gitignore

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
### Haskell
2+
dist
3+
dist-*
4+
cabal-dev
5+
*.o
6+
*.hi
7+
*.chi
8+
*.chs.h
9+
*.dyn_o
10+
*.dyn_hi
11+
*.prof
12+
*.aux
13+
*.hp
14+
*.eventlog
15+
.virtualenv
16+
.hsenv
17+
.hpc
18+
.cabal-sandbox/
19+
cabal.sandbox.config
20+
cabal.config
21+
cabal.project.local
22+
.HTF/
23+
# Stack
24+
.stack-work/
25+
26+
### IDE/support
27+
# Vim
28+
[._]*.s[a-v][a-z]
29+
[._]*.sw[a-p]
30+
[._]s[a-v][a-z]
31+
[._]sw[a-p]
32+
*~
33+
tags
34+
35+
# IntellijIDEA
36+
.idea/
37+
.ideaHaskellLib/
38+
*.iml
39+
40+
# Atom
41+
.haskell-ghc-mod.json
42+
43+
# VS
44+
.vscode/
45+
46+
# Emacs
47+
*#
48+
.dir-locals.el
49+
TAGS
50+
51+
# other
52+
.DS_Store

Data/Double/Conversion/ByteString.hs

Lines changed: 10 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@
77
-- Stability : experimental
88
-- Portability : GHC
99
--
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+
--
1014
-- Fast, efficient support for converting between double precision
1115
-- floating point values and text.
1216
--
@@ -23,54 +27,27 @@ module Data.Double.Conversion.ByteString
2327
, toShortest
2428
) where
2529

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
3432

3533
-- | Compute a representation in exponential format with the requested
3634
-- number of digits after the decimal point. The last emitted digit is
3735
-- rounded. If -1 digits are requested, then the shortest exponential
3836
-- representation is computed.
3937
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
4439

4540
-- | Compute a decimal representation with a fixed number of digits
4641
-- after the decimal point. The last emitted digit is rounded.
4742
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
5244

5345
-- | Compute the shortest string of digits that correctly represent
5446
-- the input number.
5547
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
5949

6050
-- | Compute @precision@ leading digits of the given value either in
6151
-- exponential or decimal format. The last computed digit is rounded.
6252
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

Comments
 (0)