{-# LINE 1 "Data/Text/ICU/Number.hsc" #-}
{-# LANGUAGE CPP, DeriveDataTypeable, ForeignFunctionInterface #-}
module Data.Text.ICU.Number
(
numberFormatter
, FormattableNumber, formatNumber, formatNumber'
, NumberFormatStyle(..)
, NumberFormat
) where
{-# LINE 29 "Data/Text/ICU/Number.hsc" #-}
import GHC.Natural
import Data.Text (Text)
import qualified Data.Text as T
import Data.Text.ICU.Error
import Data.Text.ICU.Error.Internal (UErrorCode, UParseError, handleParseError, handleOverflowError)
import Data.Text.ICU.Internal (UChar, useAsUCharPtr, fromUCharPtr)
import Data.Text.ICU.Internal (LocaleName, withLocaleName)
import Data.Typeable (Typeable)
import Data.Int (Int32)
import Foreign.C.Types (CInt(..), CDouble(..))
import Foreign.Ptr (Ptr)
import System.IO.Unsafe (unsafePerformIO)
import Prelude hiding (compare)
import Foreign.C.String (CString)
import Data.Text.ICU.Number.Internal
data NumberFormatStyle
= NUM_PATTERN_DECIMAL Text
| NUM_DECIMAL
| NUM_CURRENCY
| NUM_PERCENT
| NUM_SCIENTIFIC
| NUM_SPELLOUT
| NUM_ORDINAL
| NUM_DURATION
| NUM_NUMBERING_SYSTEM
| NUM_PATTERN_RULEBASED Text
| NUM_CURRENCY_ISO
| NUM_CURRENCY_PLURAL
| NUM_CURRENCY_ACCOUNTING
| NUM_CASH_CURRENCY
| NUM_DECIMAL_COMPACT_SHORT
| NUM_DECIMAL_COMPACT_LONG
| NUM_CURRENCY_STANDARD
| NUM_FORMAT_STYLE_COUNT
| NUM_DEFAULT
| NUM_IGNORE
deriving (NumberFormatStyle -> NumberFormatStyle -> Bool
(NumberFormatStyle -> NumberFormatStyle -> Bool)
-> (NumberFormatStyle -> NumberFormatStyle -> Bool)
-> Eq NumberFormatStyle
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: NumberFormatStyle -> NumberFormatStyle -> Bool
$c/= :: NumberFormatStyle -> NumberFormatStyle -> Bool
== :: NumberFormatStyle -> NumberFormatStyle -> Bool
$c== :: NumberFormatStyle -> NumberFormatStyle -> Bool
Eq, Int -> NumberFormatStyle -> ShowS
[NumberFormatStyle] -> ShowS
NumberFormatStyle -> String
(Int -> NumberFormatStyle -> ShowS)
-> (NumberFormatStyle -> String)
-> ([NumberFormatStyle] -> ShowS)
-> Show NumberFormatStyle
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [NumberFormatStyle] -> ShowS
$cshowList :: [NumberFormatStyle] -> ShowS
show :: NumberFormatStyle -> String
$cshow :: NumberFormatStyle -> String
showsPrec :: Int -> NumberFormatStyle -> ShowS
$cshowsPrec :: Int -> NumberFormatStyle -> ShowS
Show, Typeable)
type UNumberFormatStyle = CInt
toNFS :: NumberFormatStyle -> UNumberFormatStyle
toNFS :: NumberFormatStyle -> UNumberFormatStyle
toNFS (NUM_PATTERN_DECIMAL Text
_) = UNumberFormatStyle
0
{-# LINE 86 "Data/Text/ICU/Number.hsc" #-}
toNFS NUM_DECIMAL = 1
{-# LINE 87 "Data/Text/ICU/Number.hsc" #-}
toNFS NUM_CURRENCY = 2
{-# LINE 88 "Data/Text/ICU/Number.hsc" #-}
toNFS NUM_PERCENT = 3
{-# LINE 89 "Data/Text/ICU/Number.hsc" #-}
toNFS NUM_SCIENTIFIC = 4
{-# LINE 90 "Data/Text/ICU/Number.hsc" #-}
toNFS NUM_SPELLOUT = 5
{-# LINE 91 "Data/Text/ICU/Number.hsc" #-}
toNFS NUM_ORDINAL = 6
{-# LINE 92 "Data/Text/ICU/Number.hsc" #-}
toNFS NUM_DURATION = 7
{-# LINE 93 "Data/Text/ICU/Number.hsc" #-}
toNFS NUM_NUMBERING_SYSTEM = 8
{-# LINE 94 "Data/Text/ICU/Number.hsc" #-}
toNFS (NUM_PATTERN_RULEBASED _) = 9
{-# LINE 95 "Data/Text/ICU/Number.hsc" #-}
toNFS NUM_CURRENCY_ISO = 10
{-# LINE 96 "Data/Text/ICU/Number.hsc" #-}
toNFS NUM_CURRENCY_PLURAL = 11
{-# LINE 97 "Data/Text/ICU/Number.hsc" #-}
toNFS NUM_CURRENCY_ACCOUNTING = 12
{-# LINE 98 "Data/Text/ICU/Number.hsc" #-}
toNFS NUM_CASH_CURRENCY = 13
{-# LINE 99 "Data/Text/ICU/Number.hsc" #-}
toNFS NUM_DECIMAL_COMPACT_SHORT = 14
{-# LINE 100 "Data/Text/ICU/Number.hsc" #-}
toNFS NUM_DECIMAL_COMPACT_LONG = 15
{-# LINE 101 "Data/Text/ICU/Number.hsc" #-}
toNFS NUM_CURRENCY_STANDARD = 16
{-# LINE 102 "Data/Text/ICU/Number.hsc" #-}
toNFS NUM_FORMAT_STYLE_COUNT = 17
{-# LINE 103 "Data/Text/ICU/Number.hsc" #-}
toNFS NUM_DEFAULT = 1
{-# LINE 104 "Data/Text/ICU/Number.hsc" #-}
toNFS NUM_IGNORE = 0
{-# LINE 105 "Data/Text/ICU/Number.hsc" #-}
numberFormatter :: NumberFormatStyle
-> LocaleName
-> NumberFormat
numberFormatter :: NumberFormatStyle -> LocaleName -> NumberFormat
numberFormatter sty :: NumberFormatStyle
sty@(NUM_PATTERN_DECIMAL Text
pattern) LocaleName
loc = UNumberFormatStyle -> Text -> LocaleName -> NumberFormat
numberFormatter' (NumberFormatStyle -> UNumberFormatStyle
toNFS NumberFormatStyle
sty) Text
pattern LocaleName
loc
numberFormatter sty :: NumberFormatStyle
sty@(NUM_PATTERN_RULEBASED Text
pattern) LocaleName
loc = UNumberFormatStyle -> Text -> LocaleName -> NumberFormat
numberFormatter' (NumberFormatStyle -> UNumberFormatStyle
toNFS NumberFormatStyle
sty) Text
pattern LocaleName
loc
numberFormatter NumberFormatStyle
style LocaleName
loc = UNumberFormatStyle -> Text -> LocaleName -> NumberFormat
numberFormatter' (NumberFormatStyle -> UNumberFormatStyle
toNFS NumberFormatStyle
style) Text
T.empty LocaleName
loc
numberFormatter' :: UNumberFormatStyle
-> Text
-> LocaleName
-> NumberFormat
numberFormatter' :: UNumberFormatStyle -> Text -> LocaleName -> NumberFormat
numberFormatter' UNumberFormatStyle
style Text
pattern LocaleName
loc =
IO NumberFormat -> NumberFormat
forall a. IO a -> a
System.IO.Unsafe.unsafePerformIO (IO NumberFormat -> NumberFormat)
-> IO NumberFormat -> NumberFormat
forall a b. (a -> b) -> a -> b
$ (MNumberFormat -> NumberFormat)
-> IO MNumberFormat -> IO NumberFormat
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap MNumberFormat -> NumberFormat
C (IO MNumberFormat -> IO NumberFormat)
-> IO MNumberFormat -> IO NumberFormat
forall a b. (a -> b) -> a -> b
$ IO (Ptr UNumberFormat) -> IO MNumberFormat
wrap (IO (Ptr UNumberFormat) -> IO MNumberFormat)
-> IO (Ptr UNumberFormat) -> IO MNumberFormat
forall a b. (a -> b) -> a -> b
$
Text
-> (Ptr UChar -> I16 -> IO (Ptr UNumberFormat))
-> IO (Ptr UNumberFormat)
forall a. Text -> (Ptr UChar -> I16 -> IO a) -> IO a
useAsUCharPtr Text
pattern ((Ptr UChar -> I16 -> IO (Ptr UNumberFormat))
-> IO (Ptr UNumberFormat))
-> (Ptr UChar -> I16 -> IO (Ptr UNumberFormat))
-> IO (Ptr UNumberFormat)
forall a b. (a -> b) -> a -> b
$ \Ptr UChar
patternPtr I16
patternLen ->
LocaleName
-> (CString -> IO (Ptr UNumberFormat)) -> IO (Ptr UNumberFormat)
forall a. LocaleName -> (CString -> IO a) -> IO a
withLocaleName LocaleName
loc ((CString -> IO (Ptr UNumberFormat)) -> IO (Ptr UNumberFormat))
-> (CString -> IO (Ptr UNumberFormat)) -> IO (Ptr UNumberFormat)
forall a b. (a -> b) -> a -> b
$
(ICUError -> Bool)
-> (Ptr UParseError
-> Ptr UNumberFormatStyle -> IO (Ptr UNumberFormat))
-> IO (Ptr UNumberFormat)
forall a.
(ICUError -> Bool)
-> (Ptr UParseError -> Ptr UNumberFormatStyle -> IO a) -> IO a
handleParseError (ICUError -> ICUError -> Bool
forall a. Eq a => a -> a -> Bool
== ICUError
u_PARSE_ERROR) ((Ptr UParseError
-> Ptr UNumberFormatStyle -> IO (Ptr UNumberFormat))
-> IO (Ptr UNumberFormat))
-> (CString
-> Ptr UParseError
-> Ptr UNumberFormatStyle
-> IO (Ptr UNumberFormat))
-> CString
-> IO (Ptr UNumberFormat)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (UNumberFormatStyle
-> Ptr UChar
-> Int32
-> CString
-> Ptr UParseError
-> Ptr UNumberFormatStyle
-> IO (Ptr UNumberFormat)
unum_open UNumberFormatStyle
style Ptr UChar
patternPtr (I16 -> Int32
forall a b. (Integral a, Num b) => a -> b
fromIntegral I16
patternLen))
foreign import ccall unsafe "hs_text_icu.h __hs_unum_open" unum_open
:: UNumberFormatStyle -> Ptr UChar -> Int32 -> CString -> Ptr UParseError -> Ptr UErrorCode -> IO (Ptr UNumberFormat)
class FormattableNumber n where
formatNumber :: NumberFormat
-> n
-> Text
formatNumber' :: (FormattableNumber n)
=> NumberFormatStyle
-> LocaleName
-> n
-> Text
formatNumber' :: forall n.
FormattableNumber n =>
NumberFormatStyle -> LocaleName -> n -> Text
formatNumber' NumberFormatStyle
style LocaleName
loc n
x = NumberFormat -> n -> Text
forall n. FormattableNumber n => NumberFormat -> n -> Text
formatNumber (NumberFormatStyle -> LocaleName -> NumberFormat
numberFormatter NumberFormatStyle
style LocaleName
loc) n
x
instance FormattableNumber Integer where
formatNumber :: NumberFormat -> Integer -> Text
formatNumber (C MNumberFormat
nf) Integer
x = MNumberFormat -> Int -> Text
numberFormatInt MNumberFormat
nf (Integer -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral Integer
x)
instance FormattableNumber Natural where
formatNumber :: NumberFormat -> Natural -> Text
formatNumber (C MNumberFormat
nf) Natural
x = MNumberFormat -> Int -> Text
numberFormatInt MNumberFormat
nf (Natural -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral Natural
x)
instance FormattableNumber Int where
formatNumber :: NumberFormat -> Int -> Text
formatNumber (C MNumberFormat
nf) Int
x = MNumberFormat -> Int -> Text
numberFormatInt MNumberFormat
nf Int
x
instance FormattableNumber Double where
formatNumber :: NumberFormat -> Double -> Text
formatNumber (C MNumberFormat
nf) Double
x = MNumberFormat -> Double -> Text
numberFormatDouble MNumberFormat
nf Double
x
instance FormattableNumber Float where
formatNumber :: NumberFormat -> Float -> Text
formatNumber (C MNumberFormat
nf) Float
x = MNumberFormat -> Double -> Text
numberFormatDouble MNumberFormat
nf (Rational -> Double
forall a. Fractional a => Rational -> a
fromRational (Rational -> Double) -> Rational -> Double
forall a b. (a -> b) -> a -> b
$ Float -> Rational
forall a. Real a => a -> Rational
toRational Float
x)
numberFormatInt :: MNumberFormat -> Int -> Text
numberFormatInt :: MNumberFormat -> Int -> Text
numberFormatInt MNumberFormat
nf Int
x = IO Text -> Text
forall a. IO a -> a
System.IO.Unsafe.unsafePerformIO (IO Text -> Text) -> IO Text -> Text
forall a b. (a -> b) -> a -> b
$
MNumberFormat -> (Ptr UNumberFormat -> IO Text) -> IO Text
forall a. MNumberFormat -> (Ptr UNumberFormat -> IO a) -> IO a
withNumberFormat MNumberFormat
nf ((Ptr UNumberFormat -> IO Text) -> IO Text)
-> (Ptr UNumberFormat -> IO Text) -> IO Text
forall a b. (a -> b) -> a -> b
$ \Ptr UNumberFormat
nptr ->
Int
-> (Ptr UChar -> Int32 -> Ptr UNumberFormatStyle -> IO Int32)
-> (Ptr UChar -> Int -> IO Text)
-> IO Text
forall a b.
Storable a =>
Int
-> (Ptr a -> Int32 -> Ptr UNumberFormatStyle -> IO Int32)
-> (Ptr a -> Int -> IO b)
-> IO b
handleOverflowError Int
100
(\Ptr UChar
dptr Int32
dlen Ptr UNumberFormatStyle
ec -> Ptr UNumberFormat
-> Int -> Ptr UChar -> Int32 -> Ptr UNumberFormatStyle -> IO Int32
unum_formatInt64 Ptr UNumberFormat
nptr (Int -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
x) Ptr UChar
dptr (Int32 -> Int32
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int32
dlen) Ptr UNumberFormatStyle
ec)
(\Ptr UChar
dptr Int
dlen -> Ptr UChar -> I16 -> IO Text
fromUCharPtr Ptr UChar
dptr (Int -> I16
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
dlen))
numberFormatDouble :: MNumberFormat -> Double -> Text
numberFormatDouble :: MNumberFormat -> Double -> Text
numberFormatDouble MNumberFormat
nf Double
x = IO Text -> Text
forall a. IO a -> a
System.IO.Unsafe.unsafePerformIO (IO Text -> Text) -> IO Text -> Text
forall a b. (a -> b) -> a -> b
$
MNumberFormat -> (Ptr UNumberFormat -> IO Text) -> IO Text
forall a. MNumberFormat -> (Ptr UNumberFormat -> IO a) -> IO a
withNumberFormat MNumberFormat
nf ((Ptr UNumberFormat -> IO Text) -> IO Text)
-> (Ptr UNumberFormat -> IO Text) -> IO Text
forall a b. (a -> b) -> a -> b
$ \Ptr UNumberFormat
nptr ->
Int
-> (Ptr UChar -> Int32 -> Ptr UNumberFormatStyle -> IO Int32)
-> (Ptr UChar -> Int -> IO Text)
-> IO Text
forall a b.
Storable a =>
Int
-> (Ptr a -> Int32 -> Ptr UNumberFormatStyle -> IO Int32)
-> (Ptr a -> Int -> IO b)
-> IO b
handleOverflowError Int
100
(\Ptr UChar
dptr Int32
dlen Ptr UNumberFormatStyle
ec -> Ptr UNumberFormat
-> CDouble
-> Ptr UChar
-> Int32
-> Ptr UNumberFormatStyle
-> IO Int32
unum_formatDouble Ptr UNumberFormat
nptr (Double -> CDouble
CDouble Double
x) Ptr UChar
dptr (Int32 -> Int32
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int32
dlen) Ptr UNumberFormatStyle
ec)
(\Ptr UChar
dptr Int
dlen -> Ptr UChar -> I16 -> IO Text
fromUCharPtr Ptr UChar
dptr (Int -> I16
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
dlen))
foreign import ccall unsafe "hs_text_icu.h __hs_unum_formatInt64" unum_formatInt64
:: Ptr UNumberFormat -> Int -> Ptr UChar -> Int32 -> Ptr UErrorCode -> IO Int32
foreign import ccall unsafe "hs_text_icu.h __hs_unum_formatDouble" unum_formatDouble
:: Ptr UNumberFormat -> CDouble -> Ptr UChar -> Int32 -> Ptr UErrorCode -> IO Int32