module Data.RFC5051 (compareUnicode)
where
import Data.RFC5051.UnicodeData (decompositionMap)
import qualified Data.Map as M
import Data.Char (ord, toTitle)
import Data.Text (Text)
import qualified Data.Text as T
compareUnicode :: Text -> Text -> Ordering
compareUnicode :: Text -> Text -> Ordering
compareUnicode x :: Text
x y :: Text
y =
case (Text -> Maybe (Char, Text)
T.uncons Text
x, Text -> Maybe (Char, Text)
T.uncons Text
y) of
(Nothing, Nothing) -> Ordering
EQ
(Nothing, Just _) -> Ordering
LT
(Just _, Nothing) -> Ordering
GT
(Just (xc :: Char
xc,x' :: Text
x'), Just (yc :: Char
yc,y' :: Text
y')) ->
case [Int] -> [Int] -> Ordering
forall a. Ord a => a -> a -> Ordering
compare (Char -> [Int]
canonicalize Char
xc) (Char -> [Int]
canonicalize Char
yc) of
GT -> Ordering
GT
LT -> Ordering
LT
EQ -> Text -> Text -> Ordering
compareUnicode Text
x' Text
y'
canonicalize :: Char -> [Int]
canonicalize :: Char -> [Int]
canonicalize = Int -> [Int]
decompose (Int -> [Int]) -> (Char -> Int) -> Char -> [Int]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Char -> Int
ord (Char -> Int) -> (Char -> Char) -> Char -> Int
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Char -> Char
toTitle
decompose :: Int -> [Int]
decompose :: Int -> [Int]
decompose c :: Int
c =
case Int -> Maybe [Int]
decompose' Int
c of
Nothing -> [Int
c]
Just xs :: [Int]
xs -> (Int -> [Int]) -> [Int] -> [Int]
forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap Int -> [Int]
decompose [Int]
xs
decompose' :: Int -> Maybe [Int]
decompose' :: Int -> Maybe [Int]
decompose' c :: Int
c = Int -> Map Int [Int] -> Maybe [Int]
forall k a. Ord k => k -> Map k a -> Maybe a
M.lookup Int
c Map Int [Int]
decompositionMap