{-# LANGUAGE BangPatterns #-}
module Data.Unicode.Properties.DecomposeHangul
(decomposeCharHangul
, hangulFirst
, isHangul
, isHangulLV
, isJamo
, jamoLFirst
, jamoLIndex
, jamoNCount
, jamoVIndex
, jamoTCount
, jamoTIndex
)
where
import Data.Char (ord)
import GHC.Base (unsafeChr)
jamoLFirst, jamoLCount :: Int
jamoLFirst = 0x1100
jamoLCount = 19
jamoVFirst, jamoVCount :: Int
jamoVFirst = 0x1161
jamoVCount = 21
jamoTFirst, jamoTCount :: Int
jamoTFirst = 0x11a7
jamoTCount = 28
jamoLast :: Int
jamoLast = jamoTFirst + jamoTCount - 1
jamoNCount :: Int
jamoNCount = 588
hangulFirst, hangulLast :: Int
hangulFirst = 0xac00
hangulLast = hangulFirst + jamoLCount * jamoVCount * jamoTCount - 1
isHangul :: Char -> Bool
isHangul c = n >= hangulFirst && n <= hangulLast
where n = ord c
isHangulLV :: Char -> Bool
isHangulLV c = ti == 0
where
i = (ord c) - hangulFirst
!(_, ti) = i `quotRem` jamoTCount
isJamo :: Char -> Bool
isJamo c = n >= jamoLFirst && n <= jamoLast
where n = ord c
jamoLIndex :: Char -> Maybe Int
jamoLIndex c
| index >= 0 && index < jamoLCount = Just index
| otherwise = Nothing
where index = ord c - jamoLFirst
jamoVIndex :: Char -> Maybe Int
jamoVIndex c
| index >= 0 && index < jamoVCount = Just index
| otherwise = Nothing
where index = ord c - jamoVFirst
jamoTIndex :: Char -> Maybe Int
jamoTIndex c
| index > 0 && index < jamoTCount = Just index
| otherwise = Nothing
where index = ord c - jamoTFirst
{-# INLINE decomposeCharHangul #-}
decomposeCharHangul :: Char -> Either (Char, Char) (Char, Char, Char)
decomposeCharHangul c
| ti == 0 = Left (l, v)
| otherwise = Right (l, v, t)
where
i = (ord c) - hangulFirst
!(tn, ti) = i `quotRem` jamoTCount
!(li, vi) = tn `quotRem` jamoVCount
l = unsafeChr (jamoLFirst + li)
v = unsafeChr (jamoVFirst + vi)
t = unsafeChr (jamoTFirst + ti)