{-# LINE 1 "Data/Text/ICU/NumberFormatter.hsc" #-}
{-# LANGUAGE EmptyDataDecls, RankNTypes, BangPatterns, ForeignFunctionInterface, RecordWildCards #-}
module Data.Text.ICU.NumberFormatter
(
NumberFormatter,
numberFormatter,
formatIntegral, formatIntegral', formatDouble, formatDouble'
) where
import Data.Int (Int32, Int64)
import Data.Text (Text)
import Data.Text.ICU.Error.Internal (UErrorCode, handleError, handleOverflowError)
import Data.Text.ICU.Internal (LocaleName(..), UChar, withLocaleName, newICUPtr, fromUCharPtr, useAsUCharPtr)
import Foreign.C.String (CString)
import Foreign.C.Types (CDouble(..))
import Foreign.ForeignPtr (withForeignPtr, ForeignPtr)
import Foreign.Ptr (FunPtr, Ptr)
import Prelude hiding (last)
import System.IO.Unsafe (unsafePerformIO)
data UNumberFormatter
data UFormattedNumber
newtype NumberFormatter = NumberFormatter (ForeignPtr UNumberFormatter)
numberFormatter :: Text -> LocaleName -> IO NumberFormatter
numberFormatter skel loc =
withLocaleName loc $ \locale ->
useAsUCharPtr skel $ \skelPtr skelLen ->
newICUPtr NumberFormatter unumf_close $
handleError $ unumf_openForSkeletonAndLocale skelPtr (fromIntegral skelLen) locale
formatIntegral :: Integral a => NumberFormatter -> a -> Text
formatIntegral (NumberFormatter nf) x = unsafePerformIO $ do
withForeignPtr nf $ \nfPtr -> do
resultPtr <- newResult
withForeignPtr resultPtr $ \resPtr -> do
handleError $ unumf_formatInt nfPtr (fromIntegral x) resPtr
t <- handleOverflowError (fromIntegral (64 :: Int))
(\dptr dlen -> unumf_resultToString resPtr dptr dlen)
(\dptr dlen -> fromUCharPtr dptr (fromIntegral dlen))
pure t
formatIntegral' :: (Integral a) => Text -> LocaleName -> a -> Text
formatIntegral' skel loc x = unsafePerformIO $ do
nf <- numberFormatter skel loc
pure $ formatIntegral nf x
formatDouble :: NumberFormatter -> Double -> Text
formatDouble (NumberFormatter nf) x = unsafePerformIO $ do
withForeignPtr nf $ \nfPtr -> do
resultPtr <- newResult
withForeignPtr resultPtr $ \resPtr -> do
handleError $ unumf_formatDouble nfPtr (CDouble x) resPtr
t <- handleOverflowError (fromIntegral (64 :: Int))
(\dptr dlen -> unumf_resultToString resPtr dptr dlen)
(\dptr dlen -> fromUCharPtr dptr (fromIntegral dlen))
pure t
formatDouble' :: Text -> LocaleName -> Double -> Text
formatDouble' skel loc x = unsafePerformIO $ do
nf <- numberFormatter skel loc
pure $ formatDouble nf x
newResult :: IO (ForeignPtr UFormattedNumber)
newResult = newICUPtr id unumf_closeResult $ handleError unumf_openResult
foreign import ccall unsafe "hs_text_icu.h __hs_unumf_openForSkeletonAndLocale" unumf_openForSkeletonAndLocale
:: Ptr UChar -> Int32 -> CString -> Ptr UErrorCode -> IO (Ptr UNumberFormatter)
foreign import ccall unsafe "hs_text_icu.h &__hs_unumf_close" unumf_close
:: FunPtr (Ptr UNumberFormatter -> IO ())
foreign import ccall unsafe "hs_text_icu.h __hs_unumf_openResult" unumf_openResult
:: Ptr UErrorCode -> IO (Ptr UFormattedNumber)
foreign import ccall unsafe "hs_text_icu.h &__hs_unumf_closeResult" unumf_closeResult
:: FunPtr (Ptr UFormattedNumber -> IO ())
foreign import ccall unsafe "hs_text_icu.h __hs_unumf_formatInt" unumf_formatInt
:: Ptr UNumberFormatter -> Int64 -> Ptr UFormattedNumber -> Ptr UErrorCode -> IO ()
foreign import ccall unsafe "hs_text_icu.h __hs_unumf_formatDouble" unumf_formatDouble
:: Ptr UNumberFormatter -> CDouble -> Ptr UFormattedNumber -> Ptr UErrorCode -> IO ()
foreign import ccall unsafe "hs_text_icu.h __hs_unumf_resultToString" unumf_resultToString
:: Ptr UFormattedNumber -> Ptr UChar -> Int32 -> Ptr UErrorCode -> IO Int32