{-# LINE 1 "Data/Text/ICU/Normalize.hsc" #-}
{-# LANGUAGE CPP, DeriveDataTypeable, ForeignFunctionInterface #-}
module Data.Text.ICU.Normalize {-# DEPRECATED "Use Data.Text.ICU.Normalize2 instead" #-}
(
NormalizationMode(..)
, normalize
, quickCheck
, isNormalized
, CompareOption(..)
, compare
) where
{-# LINE 36 "Data/Text/ICU/Normalize.hsc" #-}
import Data.Text (Text)
import Data.Text.ICU.Error.Internal (UErrorCode, handleError, handleOverflowError)
import Data.Text.ICU.Internal (UBool, UChar, asBool, asOrdering, useAsUCharPtr, fromUCharPtr)
import Data.Text.ICU.Normalize.Internal (UNormalizationCheckResult, toNCR)
import Data.Typeable (Typeable)
import Data.Int (Int32)
import Data.Word (Word32)
import Foreign.C.Types (CInt(..))
import Foreign.Ptr (Ptr)
import System.IO.Unsafe (unsafePerformIO)
import Prelude hiding (compare)
import Data.List (foldl')
import Data.Bits ((.|.))
type UCompareOption = Word32
data CompareOption = InputIsFCD
| CompareIgnoreCase
| FoldCaseExcludeSpecialI
deriving (CompareOption -> CompareOption -> Bool
(CompareOption -> CompareOption -> Bool)
-> (CompareOption -> CompareOption -> Bool) -> Eq CompareOption
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: CompareOption -> CompareOption -> Bool
$c/= :: CompareOption -> CompareOption -> Bool
== :: CompareOption -> CompareOption -> Bool
$c== :: CompareOption -> CompareOption -> Bool
Eq, Int -> CompareOption -> ShowS
[CompareOption] -> ShowS
CompareOption -> String
(Int -> CompareOption -> ShowS)
-> (CompareOption -> String)
-> ([CompareOption] -> ShowS)
-> Show CompareOption
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [CompareOption] -> ShowS
$cshowList :: [CompareOption] -> ShowS
show :: CompareOption -> String
$cshow :: CompareOption -> String
showsPrec :: Int -> CompareOption -> ShowS
$cshowsPrec :: Int -> CompareOption -> ShowS
Show, Int -> CompareOption
CompareOption -> Int
CompareOption -> [CompareOption]
CompareOption -> CompareOption
CompareOption -> CompareOption -> [CompareOption]
CompareOption -> CompareOption -> CompareOption -> [CompareOption]
(CompareOption -> CompareOption)
-> (CompareOption -> CompareOption)
-> (Int -> CompareOption)
-> (CompareOption -> Int)
-> (CompareOption -> [CompareOption])
-> (CompareOption -> CompareOption -> [CompareOption])
-> (CompareOption -> CompareOption -> [CompareOption])
-> (CompareOption
-> CompareOption -> CompareOption -> [CompareOption])
-> Enum CompareOption
forall a.
(a -> a)
-> (a -> a)
-> (Int -> a)
-> (a -> Int)
-> (a -> [a])
-> (a -> a -> [a])
-> (a -> a -> [a])
-> (a -> a -> a -> [a])
-> Enum a
enumFromThenTo :: CompareOption -> CompareOption -> CompareOption -> [CompareOption]
$cenumFromThenTo :: CompareOption -> CompareOption -> CompareOption -> [CompareOption]
enumFromTo :: CompareOption -> CompareOption -> [CompareOption]
$cenumFromTo :: CompareOption -> CompareOption -> [CompareOption]
enumFromThen :: CompareOption -> CompareOption -> [CompareOption]
$cenumFromThen :: CompareOption -> CompareOption -> [CompareOption]
enumFrom :: CompareOption -> [CompareOption]
$cenumFrom :: CompareOption -> [CompareOption]
fromEnum :: CompareOption -> Int
$cfromEnum :: CompareOption -> Int
toEnum :: Int -> CompareOption
$ctoEnum :: Int -> CompareOption
pred :: CompareOption -> CompareOption
$cpred :: CompareOption -> CompareOption
succ :: CompareOption -> CompareOption
$csucc :: CompareOption -> CompareOption
Enum, Typeable)
fromCompareOption :: CompareOption -> UCompareOption
fromCompareOption :: CompareOption -> UCompareOption
fromCompareOption CompareOption
InputIsFCD = UCompareOption
131072
{-# LINE 179 "Data/Text/ICU/Normalize.hsc" #-}
fromCompareOption CompareIgnoreCase = 65536
{-# LINE 180 "Data/Text/ICU/Normalize.hsc" #-}
fromCompareOption FoldCaseExcludeSpecialI = 1
{-# LINE 181 "Data/Text/ICU/Normalize.hsc" #-}
reduceCompareOptions :: [CompareOption] -> UCompareOption
reduceCompareOptions :: [CompareOption] -> UCompareOption
reduceCompareOptions = (UCompareOption -> CompareOption -> UCompareOption)
-> UCompareOption -> [CompareOption] -> UCompareOption
forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
foldl' UCompareOption -> CompareOption -> UCompareOption
orO (UCompareOption
32768)
{-# LINE 184 "Data/Text/ICU/Normalize.hsc" #-}
where UCompareOption
a orO :: UCompareOption -> CompareOption -> UCompareOption
`orO` CompareOption
b = UCompareOption
a UCompareOption -> UCompareOption -> UCompareOption
forall a. Bits a => a -> a -> a
.|. CompareOption -> UCompareOption
fromCompareOption CompareOption
b
type UNormalizationMode = CInt
data NormalizationMode
= None
| NFD
| NFKD
| NFC
| NFKC
| FCD
deriving (NormalizationMode -> NormalizationMode -> Bool
(NormalizationMode -> NormalizationMode -> Bool)
-> (NormalizationMode -> NormalizationMode -> Bool)
-> Eq NormalizationMode
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: NormalizationMode -> NormalizationMode -> Bool
$c/= :: NormalizationMode -> NormalizationMode -> Bool
== :: NormalizationMode -> NormalizationMode -> Bool
$c== :: NormalizationMode -> NormalizationMode -> Bool
Eq, Int -> NormalizationMode -> ShowS
[NormalizationMode] -> ShowS
NormalizationMode -> String
(Int -> NormalizationMode -> ShowS)
-> (NormalizationMode -> String)
-> ([NormalizationMode] -> ShowS)
-> Show NormalizationMode
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [NormalizationMode] -> ShowS
$cshowList :: [NormalizationMode] -> ShowS
show :: NormalizationMode -> String
$cshow :: NormalizationMode -> String
showsPrec :: Int -> NormalizationMode -> ShowS
$cshowsPrec :: Int -> NormalizationMode -> ShowS
Show, Int -> NormalizationMode
NormalizationMode -> Int
NormalizationMode -> [NormalizationMode]
NormalizationMode -> NormalizationMode
NormalizationMode -> NormalizationMode -> [NormalizationMode]
NormalizationMode
-> NormalizationMode -> NormalizationMode -> [NormalizationMode]
(NormalizationMode -> NormalizationMode)
-> (NormalizationMode -> NormalizationMode)
-> (Int -> NormalizationMode)
-> (NormalizationMode -> Int)
-> (NormalizationMode -> [NormalizationMode])
-> (NormalizationMode -> NormalizationMode -> [NormalizationMode])
-> (NormalizationMode -> NormalizationMode -> [NormalizationMode])
-> (NormalizationMode
-> NormalizationMode -> NormalizationMode -> [NormalizationMode])
-> Enum NormalizationMode
forall a.
(a -> a)
-> (a -> a)
-> (Int -> a)
-> (a -> Int)
-> (a -> [a])
-> (a -> a -> [a])
-> (a -> a -> [a])
-> (a -> a -> a -> [a])
-> Enum a
enumFromThenTo :: NormalizationMode
-> NormalizationMode -> NormalizationMode -> [NormalizationMode]
$cenumFromThenTo :: NormalizationMode
-> NormalizationMode -> NormalizationMode -> [NormalizationMode]
enumFromTo :: NormalizationMode -> NormalizationMode -> [NormalizationMode]
$cenumFromTo :: NormalizationMode -> NormalizationMode -> [NormalizationMode]
enumFromThen :: NormalizationMode -> NormalizationMode -> [NormalizationMode]
$cenumFromThen :: NormalizationMode -> NormalizationMode -> [NormalizationMode]
enumFrom :: NormalizationMode -> [NormalizationMode]
$cenumFrom :: NormalizationMode -> [NormalizationMode]
fromEnum :: NormalizationMode -> Int
$cfromEnum :: NormalizationMode -> Int
toEnum :: Int -> NormalizationMode
$ctoEnum :: Int -> NormalizationMode
pred :: NormalizationMode -> NormalizationMode
$cpred :: NormalizationMode -> NormalizationMode
succ :: NormalizationMode -> NormalizationMode
$csucc :: NormalizationMode -> NormalizationMode
Enum, Typeable)
toNM :: NormalizationMode -> UNormalizationMode
toNM :: NormalizationMode -> UNormalizationMode
toNM NormalizationMode
None = UNormalizationMode
1
{-# LINE 200 "Data/Text/ICU/Normalize.hsc" #-}
toNM NFD = 2
{-# LINE 201 "Data/Text/ICU/Normalize.hsc" #-}
toNM NFKD = 3
{-# LINE 202 "Data/Text/ICU/Normalize.hsc" #-}
toNM NFC = 4
{-# LINE 203 "Data/Text/ICU/Normalize.hsc" #-}
toNM NFKC = 5
{-# LINE 204 "Data/Text/ICU/Normalize.hsc" #-}
toNM FCD = 6
{-# LINE 205 "Data/Text/ICU/Normalize.hsc" #-}
normalize :: NormalizationMode -> Text -> Text
normalize :: NormalizationMode -> Text -> Text
normalize NormalizationMode
mode Text
t = IO Text -> Text
forall a. IO a -> a
unsafePerformIO (IO Text -> Text)
-> ((Ptr UChar -> I16 -> IO Text) -> IO Text)
-> (Ptr UChar -> I16 -> IO Text)
-> Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> (Ptr UChar -> I16 -> IO Text) -> IO Text
forall a. Text -> (Ptr UChar -> I16 -> IO a) -> IO a
useAsUCharPtr Text
t ((Ptr UChar -> I16 -> IO Text) -> Text)
-> (Ptr UChar -> I16 -> IO Text) -> Text
forall a b. (a -> b) -> a -> b
$ \Ptr UChar
sptr I16
slen ->
let slen' :: Int32
slen' = I16 -> Int32
forall a b. (Integral a, Num b) => a -> b
fromIntegral I16
slen
mode' :: UNormalizationMode
mode' = NormalizationMode -> UNormalizationMode
toNM NormalizationMode
mode
in Int
-> (Ptr UChar -> Int32 -> Ptr UNormalizationMode -> IO Int32)
-> (Ptr UChar -> Int -> IO Text)
-> IO Text
forall a b.
Storable a =>
Int
-> (Ptr a -> Int32 -> Ptr UNormalizationMode -> IO Int32)
-> (Ptr a -> Int -> IO b)
-> IO b
handleOverflowError (I16 -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral I16
slen)
(\Ptr UChar
dptr Int32
dlen -> Ptr UChar
-> Int32
-> UNormalizationMode
-> Int32
-> Ptr UChar
-> Int32
-> Ptr UNormalizationMode
-> IO Int32
unorm_normalize Ptr UChar
sptr Int32
slen' UNormalizationMode
mode' Int32
0 Ptr UChar
dptr (Int32 -> Int32
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int32
dlen))
(\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))
quickCheck :: NormalizationMode -> Text -> Maybe Bool
quickCheck :: NormalizationMode -> Text -> Maybe Bool
quickCheck NormalizationMode
mode Text
t =
IO (Maybe Bool) -> Maybe Bool
forall a. IO a -> a
unsafePerformIO (IO (Maybe Bool) -> Maybe Bool)
-> ((Ptr UChar -> I16 -> IO (Maybe Bool)) -> IO (Maybe Bool))
-> (Ptr UChar -> I16 -> IO (Maybe Bool))
-> Maybe Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> (Ptr UChar -> I16 -> IO (Maybe Bool)) -> IO (Maybe Bool)
forall a. Text -> (Ptr UChar -> I16 -> IO a) -> IO a
useAsUCharPtr Text
t ((Ptr UChar -> I16 -> IO (Maybe Bool)) -> Maybe Bool)
-> (Ptr UChar -> I16 -> IO (Maybe Bool)) -> Maybe Bool
forall a b. (a -> b) -> a -> b
$ \Ptr UChar
ptr I16
len ->
(UNormalizationMode -> Maybe Bool)
-> IO UNormalizationMode -> IO (Maybe Bool)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap UNormalizationMode -> Maybe Bool
toNCR (IO UNormalizationMode -> IO (Maybe Bool))
-> ((Ptr UNormalizationMode -> IO UNormalizationMode)
-> IO UNormalizationMode)
-> (Ptr UNormalizationMode -> IO UNormalizationMode)
-> IO (Maybe Bool)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Ptr UNormalizationMode -> IO UNormalizationMode)
-> IO UNormalizationMode
forall a. (Ptr UNormalizationMode -> IO a) -> IO a
handleError ((Ptr UNormalizationMode -> IO UNormalizationMode)
-> IO (Maybe Bool))
-> (Ptr UNormalizationMode -> IO UNormalizationMode)
-> IO (Maybe Bool)
forall a b. (a -> b) -> a -> b
$ Ptr UChar
-> Int32
-> UNormalizationMode
-> Ptr UNormalizationMode
-> IO UNormalizationMode
unorm_quickCheck Ptr UChar
ptr (I16 -> Int32
forall a b. (Integral a, Num b) => a -> b
fromIntegral I16
len)
(NormalizationMode -> UNormalizationMode
toNM NormalizationMode
mode)
isNormalized :: NormalizationMode -> Text -> Bool
isNormalized :: NormalizationMode -> Text -> Bool
isNormalized NormalizationMode
mode Text
t =
IO Bool -> Bool
forall a. IO a -> a
unsafePerformIO (IO Bool -> Bool)
-> ((Ptr UChar -> I16 -> IO Bool) -> IO Bool)
-> (Ptr UChar -> I16 -> IO Bool)
-> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> (Ptr UChar -> I16 -> IO Bool) -> IO Bool
forall a. Text -> (Ptr UChar -> I16 -> IO a) -> IO a
useAsUCharPtr Text
t ((Ptr UChar -> I16 -> IO Bool) -> Bool)
-> (Ptr UChar -> I16 -> IO Bool) -> Bool
forall a b. (a -> b) -> a -> b
$ \Ptr UChar
ptr I16
len ->
(UBool -> Bool) -> IO UBool -> IO Bool
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap UBool -> Bool
forall a. Integral a => a -> Bool
asBool (IO UBool -> IO Bool)
-> ((Ptr UNormalizationMode -> IO UBool) -> IO UBool)
-> (Ptr UNormalizationMode -> IO UBool)
-> IO Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Ptr UNormalizationMode -> IO UBool) -> IO UBool
forall a. (Ptr UNormalizationMode -> IO a) -> IO a
handleError ((Ptr UNormalizationMode -> IO UBool) -> IO Bool)
-> (Ptr UNormalizationMode -> IO UBool) -> IO Bool
forall a b. (a -> b) -> a -> b
$ Ptr UChar
-> Int32
-> UNormalizationMode
-> Ptr UNormalizationMode
-> IO UBool
unorm_isNormalized Ptr UChar
ptr (I16 -> Int32
forall a b. (Integral a, Num b) => a -> b
fromIntegral I16
len)
(NormalizationMode -> UNormalizationMode
toNM NormalizationMode
mode)
compare :: [CompareOption] -> Text -> Text -> Ordering
compare :: [CompareOption] -> Text -> Text -> Ordering
compare [CompareOption]
opts Text
a Text
b = IO Ordering -> Ordering
forall a. IO a -> a
unsafePerformIO (IO Ordering -> Ordering)
-> ((Ptr UChar -> I16 -> IO Ordering) -> IO Ordering)
-> (Ptr UChar -> I16 -> IO Ordering)
-> Ordering
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
Text -> (Ptr UChar -> I16 -> IO Ordering) -> IO Ordering
forall a. Text -> (Ptr UChar -> I16 -> IO a) -> IO a
useAsUCharPtr Text
a ((Ptr UChar -> I16 -> IO Ordering) -> Ordering)
-> (Ptr UChar -> I16 -> IO Ordering) -> Ordering
forall a b. (a -> b) -> a -> b
$ \Ptr UChar
aptr I16
alen ->
Text -> (Ptr UChar -> I16 -> IO Ordering) -> IO Ordering
forall a. Text -> (Ptr UChar -> I16 -> IO a) -> IO a
useAsUCharPtr Text
b ((Ptr UChar -> I16 -> IO Ordering) -> IO Ordering)
-> (Ptr UChar -> I16 -> IO Ordering) -> IO Ordering
forall a b. (a -> b) -> a -> b
$ \Ptr UChar
bptr I16
blen ->
(Int32 -> Ordering) -> IO Int32 -> IO Ordering
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Int32 -> Ordering
forall a. Integral a => a -> Ordering
asOrdering (IO Int32 -> IO Ordering)
-> ((Ptr UNormalizationMode -> IO Int32) -> IO Int32)
-> (Ptr UNormalizationMode -> IO Int32)
-> IO Ordering
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Ptr UNormalizationMode -> IO Int32) -> IO Int32
forall a. (Ptr UNormalizationMode -> IO a) -> IO a
handleError ((Ptr UNormalizationMode -> IO Int32) -> IO Ordering)
-> (Ptr UNormalizationMode -> IO Int32) -> IO Ordering
forall a b. (a -> b) -> a -> b
$
Ptr UChar
-> Int32
-> Ptr UChar
-> Int32
-> UCompareOption
-> Ptr UNormalizationMode
-> IO Int32
unorm_compare Ptr UChar
aptr (I16 -> Int32
forall a b. (Integral a, Num b) => a -> b
fromIntegral I16
alen) Ptr UChar
bptr (I16 -> Int32
forall a b. (Integral a, Num b) => a -> b
fromIntegral I16
blen)
([CompareOption] -> UCompareOption
reduceCompareOptions [CompareOption]
opts)
foreign import ccall unsafe "hs_text_icu.h __hs_unorm_compare" unorm_compare
:: Ptr UChar -> Int32
-> Ptr UChar -> Int32
-> Word32
-> Ptr UErrorCode
-> IO Int32
foreign import ccall unsafe "hs_text_icu.h __hs_unorm_quickCheck" unorm_quickCheck
:: Ptr UChar -> Int32
-> UNormalizationMode
-> Ptr UErrorCode
-> IO UNormalizationCheckResult
foreign import ccall unsafe "hs_text_icu.h __hs_unorm_isNormalized" unorm_isNormalized
:: Ptr UChar -> Int32
-> UNormalizationMode
-> Ptr UErrorCode
-> IO UBool
foreign import ccall unsafe "hs_text_icu.h __hs_unorm_normalize" unorm_normalize
:: Ptr UChar -> Int32
-> UNormalizationMode
-> Int32
-> Ptr UChar -> Int32
-> Ptr UErrorCode
-> IO Int32