Bryan O'Sullivan avatar Bryan O'Sullivan committed 893786e

Refactor in preparation for adding ByteString support.

Comments (0)

Files changed (5)

Data/Double/Conversion.hs

-{-# LANGUAGE ForeignFunctionInterface, MagicHash, Rank2Types,
-    UnliftedFFITypes #-}
+{-# LANGUAGE MagicHash, Rank2Types #-}
 
 -- |
 -- Module      : Data.Double.Conversion
 
 import Control.Monad (when)
 import Control.Monad.ST (unsafeIOToST, runST)
+import Data.Double.Conversion.FFI
 import Data.Text.Internal (Text(Text))
 import Foreign.C.Types (CDouble, CInt)
 import GHC.Prim (MutableByteArray#)
 -- representation is computed.
 toExponential :: Int -> Double -> Text
 toExponential ndigits = convert "toExponential" len $ \val mba ->
-                        c_ToExponential val mba (fromIntegral ndigits)
+                        c_Text_ToExponential val mba (fromIntegral ndigits)
   where len = c_ToExponentialLength
         {-# NOINLINE len #-}
 
 -- after the decimal point. The last emitted digit is rounded.
 toFixed :: Int -> Double -> Text
 toFixed ndigits = convert "toFixed" len $ \val mba ->
-                  c_ToFixed val mba (fromIntegral ndigits)
+                  c_Text_ToFixed val mba (fromIntegral ndigits)
   where len = c_ToFixedLength
         {-# NOINLINE len #-}
 
 -- | Compute the shortest string of digits that correctly represent
 -- the input number.
 toShortest :: Double -> Text
-toShortest = convert "toShortest" len c_ToShortest
+toShortest = convert "toShortest" len c_Text_ToShortest
   where len = c_ToShortestLength
         {-# NOINLINE len #-}
 
 -- exponential or decimal format. The last computed digit is rounded.
 toPrecision :: Int -> Double -> Text
 toPrecision ndigits = convert "toPrecision" len $ \val mba ->
-                      c_ToPrecision val mba (fromIntegral ndigits)
+                      c_Text_ToPrecision val mba (fromIntegral ndigits)
   where len = c_ToPrecisionLength
         {-# NOINLINE len #-}
 
                ": conversion failed (invalid precision requested)"
       frozen <- A.unsafeFreeze buf
       return $ Text frozen 0 (fromIntegral size)
-
-foreign import ccall unsafe "hs-double-conversion.h _hs_ToShortestLength"
-    c_ToShortestLength :: CInt
-
-foreign import ccall unsafe "hs-double-conversion.h _hs_ToShortest"
-    c_ToShortest :: CDouble -> MutableByteArray# s -> IO CInt
-
-foreign import ccall unsafe "hs-double-conversion.h _hs_ToFixedLength"
-    c_ToFixedLength :: CInt
-
-foreign import ccall unsafe "hs-double-conversion.h _hs_ToFixed"
-    c_ToFixed :: CDouble -> MutableByteArray# s -> CInt -> IO CInt
-
-foreign import ccall unsafe "hs-double-conversion.h _hs_ToExponentialLength"
-    c_ToExponentialLength :: CInt
-
-foreign import ccall unsafe "hs-double-conversion.h _hs_ToExponential"
-    c_ToExponential :: CDouble -> MutableByteArray# s -> CInt -> IO CInt
-
-foreign import ccall unsafe "hs-double-conversion.h _hs_ToPrecisionLength"
-    c_ToPrecisionLength :: CInt
-
-foreign import ccall unsafe "hs-double-conversion.h _hs_ToPrecision"
-    c_ToPrecision :: CDouble -> MutableByteArray# s -> CInt -> IO CInt

Data/Double/Conversion/FFI.hs

+{-# LANGUAGE ForeignFunctionInterface, MagicHash, UnliftedFFITypes #-}
+
+-- |
+-- Module      : Data.Double.Conversion.FFI
+-- Copyright   : (c) 2011 MailRank, Inc.
+--
+-- License     : BSD-style
+-- Maintainer  : bos@mailrank.com
+-- Stability   : experimental
+-- Portability : GHC
+--
+-- FFI interface support for converting between double precision
+-- floating point values and text.
+
+module Data.Double.Conversion.FFI
+    (
+      c_Text_ToExponential
+    , c_Text_ToFixed
+    , c_Text_ToPrecision
+    , c_Text_ToShortest
+    , c_ToExponentialLength
+    , c_ToFixedLength
+    , c_ToPrecisionLength
+    , c_ToShortestLength
+    ) where
+
+import Foreign.C.Types (CDouble, CInt)
+import GHC.Prim (MutableByteArray#)
+
+foreign import ccall unsafe "hs-double-conversion.h _hs_ToShortestLength"
+    c_ToShortestLength :: CInt
+
+foreign import ccall unsafe "hs-double-conversion.h _hs_Text_ToShortest"
+    c_Text_ToShortest :: CDouble -> MutableByteArray# s -> IO CInt
+
+foreign import ccall unsafe "hs-double-conversion.h _hs_ToFixedLength"
+    c_ToFixedLength :: CInt
+
+foreign import ccall unsafe "hs-double-conversion.h _hs_Text_ToFixed"
+    c_Text_ToFixed :: CDouble -> MutableByteArray# s -> CInt -> IO CInt
+
+foreign import ccall unsafe "hs-double-conversion.h _hs_ToExponentialLength"
+    c_ToExponentialLength :: CInt
+
+foreign import ccall unsafe "hs-double-conversion.h _hs_Text_ToExponential"
+    c_Text_ToExponential :: CDouble -> MutableByteArray# s -> CInt -> IO CInt
+
+foreign import ccall unsafe "hs-double-conversion.h _hs_ToPrecisionLength"
+    c_ToPrecisionLength :: CInt
+
+foreign import ccall unsafe "hs-double-conversion.h _hs_Text_ToPrecision"
+    c_Text_ToPrecision :: CDouble -> MutableByteArray# s -> CInt -> IO CInt

cbits/hs-double-conversion.cc

 }
 
 extern "C"
-int _hs_ToShortest(double value, uint16_t *buf)
+int _hs_Text_ToShortest(double value, uint16_t *buf)
 {
   char cbuf[kToShortestLength];
   StringBuilder builder(cbuf, kToShortestLength);
 }
 
 extern "C"
-int _hs_ToFixed(double value, uint16_t *buf, const int ndigits)
+int _hs_Text_ToFixed(double value, uint16_t *buf, const int ndigits)
 {
   char cbuf[kToFixedLength];
   StringBuilder builder(cbuf, kToFixedLength);
 }
 
 extern "C"
-int _hs_ToExponential(double value, uint16_t *buf, const int ndigits)
+int _hs_Text_ToExponential(double value, uint16_t *buf, const int ndigits)
 {
   char cbuf[kToExponentialLength];
   StringBuilder builder(cbuf, kToExponentialLength);
 }
 
 extern "C"
-int _hs_ToPrecision(double value, uint16_t *buf, const int precision)
+int _hs_Text_ToPrecision(double value, uint16_t *buf, const int precision)
 {
   char cbuf[kToPrecisionLength];
   StringBuilder builder(cbuf, kToPrecisionLength);

double-conversion.cabal

 name:           double-conversion
-version:        0.1.0.0
+version:        0.1.1.0
 license:        BSD3
 license-file:   LICENSE
 homepage:       https://github.com/mailrank/double-conversion
   exposed-modules:
     Data.Double.Conversion
 
+  other-modules:
+    Data.Double.Conversion.FFI
+
   build-depends:
     base == 4.*,
     ghc-prim,

include/hs-double-conversion.h

 #include <stddef.h>
 
 int _hs_ToShortestLength(void);
-int _hs_ToShortest(double value, uint16_t *buf);
+int _hs_Text_ToShortest(double value, uint16_t *buf);
 int _hs_ToFixedLength(void);
-int _hs_ToFixed(double value, uint16_t *buf, int ndigits);
+int _hs_Text_ToFixed(double value, uint16_t *buf, int ndigits);
 int _hs_ToExponentialLength(void);
-int _hs_ToExponential(double value, uint16_t *buf, int ndigits);
+int _hs_Text_ToExponential(double value, uint16_t *buf, int ndigits);
 int _hs_ToPrecisionLength(void);
-int _hs_ToPrecision(double value, uint16_t *buf, int ndigits);
+int _hs_Text_ToPrecision(double value, uint16_t *buf, int ndigits);
 
 #ifdef __cplusplus
 }
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.