{-# LANGUAGE TemplateHaskell #-}
{-# LANGUAGE OverloadedStrings #-}

-- | Internal Unicode property lookups for IDN validation.
--
-- This module provides Unicode property lookups used internally for IDN
-- validation. For public API, see 'Data.Text.IDN'.
--
-- = Implementation Note: IntMap/IntSet vs CharSet
--
-- This module uses 'IntMap' and 'IntSet' from containers rather than 'CharSet'
-- from the charset package. Benchmarking showed that:
--
-- * Map lookups (codePointStatus, bidiClass, scriptOf, lookupContextRule)
--   are 2-23% slower with CharSet, as it provides no benefit for map operations
-- * Set membership tests (isCombiningMark, isVirama) show mixed results:
--     - CharSet is 36% faster for negative lookups (non-combining chars)
--     - CharSet is 2-6% slower for positive lookups and mixed workloads
-- * Overall: IntMap/IntSet is faster for our workload which is dominated by
--   map lookups rather than pure set membership tests
module Data.Text.IDN.Internal.Unicode
  ( -- * Property Lookup Functions
    codePointStatus
  , bidiClass
  , isCombiningMark
  , isVirama
  , scriptOf
  , lookupContextRule
  
    -- * Unicode Normalization
  , isNFCQuickCheck
  ) where

import Data.Text.IDN.Types
import qualified Data.IntMap.Strict as IM
import qualified Data.IntSet as IS
import Data.IntMap.Strict (IntMap)
import Data.IntSet (IntSet)
import Data.Char (ord)
import qualified Data.Text as T
import qualified Data.Text.Read as TR
import qualified Data.Text.Encoding as TE
import qualified Data.ByteString as BS
import Data.FileEmbed (embedFile)
import System.IO.Unsafe (unsafePerformIO)

-- Embed the Unicode data files at compile time
bidiClassData :: BS.ByteString
bidiClassData :: ByteString
bidiClassData = $(embedFile "data/unicode/DerivedBidiClass.txt")

unicodeData :: BS.ByteString
unicodeData :: ByteString
unicodeData = $(embedFile "data/unicode/UnicodeData.txt")

joiningTypeData :: BS.ByteString
joiningTypeData :: ByteString
joiningTypeData = $(embedFile "data/unicode/DerivedJoiningType.txt")

scriptsData :: BS.ByteString
scriptsData :: ByteString
scriptsData = $(embedFile "data/unicode/Scripts.txt")

-- Parse data files once at module load time
{-# NOINLINE codePointStatusTable #-}
codePointStatusTable :: IntMap CodePointStatus
codePointStatusTable :: IntMap CodePointStatus
codePointStatusTable = Text -> IntMap CodePointStatus
deriveCodePointStatus (ByteString -> Text
TE.decodeUtf8 ByteString
unicodeData)

{-# NOINLINE bidiClassTable #-}
bidiClassTable :: IntMap BidiClass
bidiClassTable :: IntMap BidiClass
bidiClassTable = IO (IntMap BidiClass) -> IntMap BidiClass
forall a. IO a -> a
unsafePerformIO (IO (IntMap BidiClass) -> IntMap BidiClass)
-> IO (IntMap BidiClass) -> IntMap BidiClass
forall a b. (a -> b) -> a -> b
$ Text -> IO (IntMap BidiClass)
parseBidiClasses (ByteString -> Text
TE.decodeUtf8 ByteString
bidiClassData)

{-# NOINLINE combiningMarksSet #-}
combiningMarksSet :: IntSet
combiningMarksSet :: IntSet
combiningMarksSet = Text -> IntSet
parseCombiningMarks (ByteString -> Text
TE.decodeUtf8 ByteString
unicodeData)

{-# NOINLINE viramasSet #-}
viramasSet :: IntSet
viramasSet :: IntSet
viramasSet = Text -> IntSet
parseViramas (ByteString -> Text
TE.decodeUtf8 ByteString
joiningTypeData)

{-# NOINLINE scriptTable #-}
scriptTable :: IntMap Script
scriptTable :: IntMap Script
scriptTable = case Text -> Either [Char] (IntMap Script)
parseScripts (ByteString -> Text
TE.decodeUtf8 ByteString
scriptsData) of
  Left [Char]
err -> [Char] -> IntMap Script
forall a. HasCallStack => [Char] -> a
error ([Char] -> IntMap Script) -> [Char] -> IntMap Script
forall a b. (a -> b) -> a -> b
$ [Char]
"Failed to parse scripts: " [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ [Char]
err
  Right IntMap Script
m -> IntMap Script
m

{-# NOINLINE contextRuleTable #-}
contextRuleTable :: IntMap ContextRule
contextRuleTable :: IntMap ContextRule
contextRuleTable = [(Int, ContextRule)] -> IntMap ContextRule
forall a. [(Int, a)] -> IntMap a
IM.fromList ([(Int, ContextRule)] -> IntMap ContextRule)
-> [(Int, ContextRule)] -> IntMap ContextRule
forall a b. (a -> b) -> a -> b
$
  [ (Int
0x200C, ContextRule
ZWNJRule)
  , (Int
0x200D, ContextRule
ZWJRule)
  , (Int
0x00B7, ContextRule
MiddleDotRule)
  , (Int
0x0375, ContextRule
GreekKeraiaRule)
  , (Int
0x05F3, ContextRule
HebrewGereshRule)
  , (Int
0x05F4, ContextRule
HebrewGershayimRule)
  , (Int
0x30FB, ContextRule
KatakanaMiddleDotRule)
  ] [(Int, ContextRule)]
-> [(Int, ContextRule)] -> [(Int, ContextRule)]
forall a. [a] -> [a] -> [a]
++ [(Int
cp, ContextRule
ArabicIndicDigitsRule) | Int
cp <- [Int
0x0660..Int
0x0669]]
    [(Int, ContextRule)]
-> [(Int, ContextRule)] -> [(Int, ContextRule)]
forall a. [a] -> [a] -> [a]
++ [(Int
cp, ContextRule
ExtendedArabicIndicDigitsRule) | Int
cp <- [Int
0x06F0..Int
0x06F9]]

-- Lookup functions
codePointStatus :: Char -> CodePointStatus
codePointStatus :: Char -> CodePointStatus
codePointStatus Char
c = CodePointStatus -> Int -> IntMap CodePointStatus -> CodePointStatus
forall a. a -> Int -> IntMap a -> a
IM.findWithDefault CodePointStatus
PVALID (Char -> Int
ord Char
c) IntMap CodePointStatus
codePointStatusTable
{-# INLINE codePointStatus #-}

bidiClass :: Char -> BidiClass
bidiClass :: Char -> BidiClass
bidiClass Char
c = BidiClass -> Int -> IntMap BidiClass -> BidiClass
forall a. a -> Int -> IntMap a -> a
IM.findWithDefault BidiClass
L (Char -> Int
ord Char
c) IntMap BidiClass
bidiClassTable
{-# INLINE bidiClass #-}

isCombiningMark :: Char -> Bool
isCombiningMark :: Char -> Bool
isCombiningMark Char
c = Int -> IntSet -> Bool
IS.member (Char -> Int
ord Char
c) IntSet
combiningMarksSet
{-# INLINE isCombiningMark #-}

isVirama :: Char -> Bool
isVirama :: Char -> Bool
isVirama Char
c = Int -> IntSet -> Bool
IS.member (Char -> Int
ord Char
c) IntSet
viramasSet
{-# INLINE isVirama #-}

scriptOf :: Char -> Maybe Script
scriptOf :: Char -> Maybe Script
scriptOf Char
c = Int -> IntMap Script -> Maybe Script
forall a. Int -> IntMap a -> Maybe a
IM.lookup (Char -> Int
ord Char
c) IntMap Script
scriptTable
{-# INLINE scriptOf #-}

lookupContextRule :: Char -> Maybe ContextRule
lookupContextRule :: Char -> Maybe ContextRule
lookupContextRule Char
c = Int -> IntMap ContextRule -> Maybe ContextRule
forall a. Int -> IntMap a -> Maybe a
IM.lookup (Char -> Int
ord Char
c) IntMap ContextRule
contextRuleTable
{-# INLINE lookupContextRule #-}

isNFCQuickCheck :: String -> Bool
isNFCQuickCheck :: [Char] -> Bool
isNFCQuickCheck = (Char -> Bool) -> [Char] -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
all Char -> Bool
checkChar
  where
    checkChar :: Char -> Bool
checkChar Char
c = 
      let cp :: Int
cp = Char -> Int
ord Char
c
      in Int
cp Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
0x0300 Bool -> Bool -> Bool
|| (Int
cp Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> Int
0x0370 Bool -> Bool -> Bool
&& Int
cp Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
0x1AB0)
{-# INLINE isNFCQuickCheck #-}

-- Parsing functions
parseBidiClasses :: T.Text -> IO (IntMap BidiClass)
parseBidiClasses :: Text -> IO (IntMap BidiClass)
parseBidiClasses Text
text = do
  let dataLines :: [Text]
dataLines = (Text -> Bool) -> [Text] -> [Text]
forall a. (a -> Bool) -> [a] -> [a]
filter (Bool -> Bool
not (Bool -> Bool) -> (Text -> Bool) -> Text -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> Bool
isComment) (Text -> [Text]
T.lines Text
text)
      entries :: Either [Char] [[(Int, BidiClass)]]
entries = (Text -> Either [Char] [(Int, BidiClass)])
-> [Text] -> Either [Char] [[(Int, BidiClass)]]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
forall (m :: * -> *) a b. Monad m => (a -> m b) -> [a] -> m [b]
mapM Text -> Either [Char] [(Int, BidiClass)]
parseBidiLine [Text]
dataLines
  case Either [Char] [[(Int, BidiClass)]]
entries of
    Left [Char]
err -> [Char] -> IO (IntMap BidiClass)
forall a. HasCallStack => [Char] -> a
error ([Char] -> IO (IntMap BidiClass))
-> [Char] -> IO (IntMap BidiClass)
forall a b. (a -> b) -> a -> b
$ [Char]
"Failed to parse bidi classes: " [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ [Char]
err
    Right [[(Int, BidiClass)]]
xs -> IntMap BidiClass -> IO (IntMap BidiClass)
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return (IntMap BidiClass -> IO (IntMap BidiClass))
-> IntMap BidiClass -> IO (IntMap BidiClass)
forall a b. (a -> b) -> a -> b
$ [(Int, BidiClass)] -> IntMap BidiClass
forall a. [(Int, a)] -> IntMap a
IM.fromList ([[(Int, BidiClass)]] -> [(Int, BidiClass)]
forall (t :: * -> *) a. Foldable t => t [a] -> [a]
concat [[(Int, BidiClass)]]
xs)

parseBidiLine :: T.Text -> Either String [(Int, BidiClass)]
parseBidiLine :: Text -> Either [Char] [(Int, BidiClass)]
parseBidiLine Text
line = do
  let content :: Text
content = Text -> Text
T.strip (Text -> Text) -> Text -> Text
forall a b. (a -> b) -> a -> b
$ (Char -> Bool) -> Text -> Text
T.takeWhile (Char -> Char -> Bool
forall a. Eq a => a -> a -> Bool
/= Char
'#') Text
line
  case Text -> [Text]
T.words Text
content of
    [] -> [(Int, BidiClass)] -> Either [Char] [(Int, BidiClass)]
forall a b. b -> Either a b
Right []
    [Text
cpRange, Text
";", Text
bidiClassStr] -> do
      (Int
startCP, Int
endCP) <- Text -> Either [Char] (Int, Int)
parseCodePointRange (Text -> Text
T.strip Text
cpRange)
      BidiClass
bidiClassVal <- Text -> Either [Char] BidiClass
parseBidiClass (Text -> Text
T.strip Text
bidiClassStr)
      [(Int, BidiClass)] -> Either [Char] [(Int, BidiClass)]
forall a. a -> Either [Char] a
forall (m :: * -> *) a. Monad m => a -> m a
return [(Int
cp, BidiClass
bidiClassVal) | Int
cp <- [Int
startCP..Int
endCP]]
    [Text
cpRange, Text
bidiClassStr] -> do  -- Handle without semicolon
      (Int
startCP, Int
endCP) <- Text -> Either [Char] (Int, Int)
parseCodePointRange (Text -> Text
T.strip Text
cpRange)
      BidiClass
bidiClassVal <- Text -> Either [Char] BidiClass
parseBidiClass (Text -> Text
T.strip Text
bidiClassStr)
      [(Int, BidiClass)] -> Either [Char] [(Int, BidiClass)]
forall a. a -> Either [Char] a
forall (m :: * -> *) a. Monad m => a -> m a
return [(Int
cp, BidiClass
bidiClassVal) | Int
cp <- [Int
startCP..Int
endCP]]
    [Text]
_ -> [Char] -> Either [Char] [(Int, BidiClass)]
forall a b. a -> Either a b
Left ([Char] -> Either [Char] [(Int, BidiClass)])
-> [Char] -> Either [Char] [(Int, BidiClass)]
forall a b. (a -> b) -> a -> b
$ [Char]
"Invalid bidi line: " [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ Text -> [Char]
T.unpack Text
line

parseCodePointRange :: T.Text -> Either String (Int, Int)
parseCodePointRange :: Text -> Either [Char] (Int, Int)
parseCodePointRange Text
t = case HasCallStack => Text -> Text -> [Text]
Text -> Text -> [Text]
T.splitOn Text
".." Text
t of
  [Text
single] -> case Reader Int
forall a. Integral a => Reader a
TR.hexadecimal Text
t of
    Right (Int
cp, Text
_) -> (Int, Int) -> Either [Char] (Int, Int)
forall a b. b -> Either a b
Right (Int
cp, Int
cp)
    Left [Char]
err -> [Char] -> Either [Char] (Int, Int)
forall a b. a -> Either a b
Left [Char]
err
  [Text
start, Text
end] -> do
    (Int
startCP, Text
_) <- Reader Int
forall a. Integral a => Reader a
TR.hexadecimal Text
start
    (Int
endCP, Text
_) <- Reader Int
forall a. Integral a => Reader a
TR.hexadecimal Text
end
    (Int, Int) -> Either [Char] (Int, Int)
forall a. a -> Either [Char] a
forall (m :: * -> *) a. Monad m => a -> m a
return (Int
startCP, Int
endCP)
  [Text]
_ -> [Char] -> Either [Char] (Int, Int)
forall a b. a -> Either a b
Left ([Char] -> Either [Char] (Int, Int))
-> [Char] -> Either [Char] (Int, Int)
forall a b. (a -> b) -> a -> b
$ [Char]
"Invalid range: " [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ Text -> [Char]
T.unpack Text
t

parseBidiClass :: T.Text -> Either String BidiClass
parseBidiClass :: Text -> Either [Char] BidiClass
parseBidiClass Text
"L" = BidiClass -> Either [Char] BidiClass
forall a b. b -> Either a b
Right BidiClass
L
parseBidiClass Text
"R" = BidiClass -> Either [Char] BidiClass
forall a b. b -> Either a b
Right BidiClass
R
parseBidiClass Text
"AL" = BidiClass -> Either [Char] BidiClass
forall a b. b -> Either a b
Right BidiClass
AL
parseBidiClass Text
"AN" = BidiClass -> Either [Char] BidiClass
forall a b. b -> Either a b
Right BidiClass
AN
parseBidiClass Text
"EN" = BidiClass -> Either [Char] BidiClass
forall a b. b -> Either a b
Right BidiClass
EN
parseBidiClass Text
"ES" = BidiClass -> Either [Char] BidiClass
forall a b. b -> Either a b
Right BidiClass
ES
parseBidiClass Text
"ET" = BidiClass -> Either [Char] BidiClass
forall a b. b -> Either a b
Right BidiClass
ET
parseBidiClass Text
"CS" = BidiClass -> Either [Char] BidiClass
forall a b. b -> Either a b
Right BidiClass
CS
parseBidiClass Text
"NSM" = BidiClass -> Either [Char] BidiClass
forall a b. b -> Either a b
Right BidiClass
NSM
parseBidiClass Text
"BN" = BidiClass -> Either [Char] BidiClass
forall a b. b -> Either a b
Right BidiClass
BN
parseBidiClass Text
"B" = BidiClass -> Either [Char] BidiClass
forall a b. b -> Either a b
Right BidiClass
B
parseBidiClass Text
"S" = BidiClass -> Either [Char] BidiClass
forall a b. b -> Either a b
Right BidiClass
S
parseBidiClass Text
"WS" = BidiClass -> Either [Char] BidiClass
forall a b. b -> Either a b
Right BidiClass
WS
parseBidiClass Text
"ON" = BidiClass -> Either [Char] BidiClass
forall a b. b -> Either a b
Right BidiClass
ON
parseBidiClass Text
"LRE" = BidiClass -> Either [Char] BidiClass
forall a b. b -> Either a b
Right BidiClass
LRE
parseBidiClass Text
"LRO" = BidiClass -> Either [Char] BidiClass
forall a b. b -> Either a b
Right BidiClass
LRO
parseBidiClass Text
"RLE" = BidiClass -> Either [Char] BidiClass
forall a b. b -> Either a b
Right BidiClass
RLE
parseBidiClass Text
"RLO" = BidiClass -> Either [Char] BidiClass
forall a b. b -> Either a b
Right BidiClass
RLO
parseBidiClass Text
"PDF" = BidiClass -> Either [Char] BidiClass
forall a b. b -> Either a b
Right BidiClass
PDF
parseBidiClass Text
"LRI" = BidiClass -> Either [Char] BidiClass
forall a b. b -> Either a b
Right BidiClass
LRI
parseBidiClass Text
"RLI" = BidiClass -> Either [Char] BidiClass
forall a b. b -> Either a b
Right BidiClass
RLI
parseBidiClass Text
"FSI" = BidiClass -> Either [Char] BidiClass
forall a b. b -> Either a b
Right BidiClass
FSI
parseBidiClass Text
"PDI" = BidiClass -> Either [Char] BidiClass
forall a b. b -> Either a b
Right BidiClass
PDI
parseBidiClass Text
t = [Char] -> Either [Char] BidiClass
forall a b. a -> Either a b
Left ([Char] -> Either [Char] BidiClass)
-> [Char] -> Either [Char] BidiClass
forall a b. (a -> b) -> a -> b
$ [Char]
"Unknown bidi class: " [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ Text -> [Char]
T.unpack Text
t

-- | Derive code point status according to RFC 5892
-- This implements the IDNA2008 derivation algorithm based on Unicode properties
deriveCodePointStatus :: T.Text -> IntMap CodePointStatus
deriveCodePointStatus :: Text -> IntMap CodePointStatus
deriveCodePointStatus Text
text =
  let dataLines :: [Text]
dataLines = (Text -> Bool) -> [Text] -> [Text]
forall a. (a -> Bool) -> [a] -> [a]
filter (Bool -> Bool
not (Bool -> Bool) -> (Text -> Bool) -> Text -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> Bool
T.null) ([Text] -> [Text]) -> [Text] -> [Text]
forall a b. (a -> b) -> a -> b
$ Text -> [Text]
T.lines Text
text
      entries :: [(Int, CodePointStatus)]
entries = (Text -> Maybe (Int, CodePointStatus))
-> [Text] -> [(Int, CodePointStatus)]
forall {t :: * -> *} {t} {a}.
Foldable t =>
(t -> Maybe a) -> t t -> [a]
mapMaybe Text -> Maybe (Int, CodePointStatus)
parseUnicodeDataLine [Text]
dataLines
  in [(Int, CodePointStatus)] -> IntMap CodePointStatus
forall a. [(Int, a)] -> IntMap a
IM.fromList [(Int, CodePointStatus)]
entries
  where
    mapMaybe :: (t -> Maybe a) -> t t -> [a]
mapMaybe t -> Maybe a
f = (t -> [a] -> [a]) -> [a] -> t t -> [a]
forall a b. (a -> b -> b) -> b -> t a -> b
forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr (\t
x [a]
acc -> case t -> Maybe a
f t
x of Just a
y -> a
y a -> [a] -> [a]
forall a. a -> [a] -> [a]
: [a]
acc; Maybe a
Nothing -> [a]
acc) []

data UnicodeEntry = UnicodeEntry
  { UnicodeEntry -> Int
ueCodePoint :: Int
  , UnicodeEntry -> Text
ueName :: T.Text
  , UnicodeEntry -> Text
ueCategory :: T.Text
  , UnicodeEntry -> Int
ueCombiningClass :: Int
  }

parseUnicodeDataLine :: T.Text -> Maybe (Int, CodePointStatus)
parseUnicodeDataLine :: Text -> Maybe (Int, CodePointStatus)
parseUnicodeDataLine Text
line =
  case HasCallStack => Text -> Text -> [Text]
Text -> Text -> [Text]
T.splitOn Text
";" Text
line of
    (Text
cpHex:Text
name:Text
cat:Text
ccc:[Text]
_rest) -> do
      (Int
cp, Text
_) <- ([Char] -> Maybe (Int, Text))
-> ((Int, Text) -> Maybe (Int, Text))
-> Either [Char] (Int, Text)
-> Maybe (Int, Text)
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either (Maybe (Int, Text) -> [Char] -> Maybe (Int, Text)
forall a b. a -> b -> a
const Maybe (Int, Text)
forall a. Maybe a
Nothing) (Int, Text) -> Maybe (Int, Text)
forall a. a -> Maybe a
Just (Either [Char] (Int, Text) -> Maybe (Int, Text))
-> Either [Char] (Int, Text) -> Maybe (Int, Text)
forall a b. (a -> b) -> a -> b
$ Reader Int
forall a. Integral a => Reader a
TR.hexadecimal (Text -> Text
T.strip Text
cpHex)
      let category :: Text
category = Text -> Text
T.strip Text
cat
          status :: CodePointStatus
status = Int -> Text -> CodePointStatus
deriveStatus Int
cp Text
category
      (Int, CodePointStatus) -> Maybe (Int, CodePointStatus)
forall a. a -> Maybe a
Just (Int
cp, CodePointStatus
status)
    [Text]
_ -> Maybe (Int, CodePointStatus)
forall a. Maybe a
Nothing

-- | RFC 5892 derivation rules
deriveStatus :: Int -> T.Text -> CodePointStatus
deriveStatus :: Int -> Text -> CodePointStatus
deriveStatus Int
cp Text
cat
  -- Explicit CONTEXTJ code points
  | Int
cp Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
0x200C = CodePointStatus
CONTEXTJ  -- Zero Width Non-Joiner
  | Int
cp Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
0x200D = CodePointStatus
CONTEXTJ  -- Zero Width Joiner
  -- Explicit CONTEXTO code points
  | Int
cp Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
0x00B7 = CodePointStatus
CONTEXTO  -- Middle Dot
  | Int
cp Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
0x0375 = CodePointStatus
CONTEXTO  -- Greek Lower Numeral Sign (Keraia)
  | Int
cp Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
0x05F3 = CodePointStatus
CONTEXTO  -- Hebrew Punctuation Geresh
  | Int
cp Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
0x05F4 = CodePointStatus
CONTEXTO  -- Hebrew Punctuation Gershayim
  | Int
cp Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
0x30FB = CodePointStatus
CONTEXTO  -- Katakana Middle Dot
  | Int
cp Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
>= Int
0x0660 Bool -> Bool -> Bool
&& Int
cp Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
<= Int
0x0669 = CodePointStatus
CONTEXTO  -- Arabic-Indic Digits
  | Int
cp Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
>= Int
0x06F0 Bool -> Bool -> Bool
&& Int
cp Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
<= Int
0x06F9 = CodePointStatus
CONTEXTO  -- Extended Arabic-Indic Digits
  -- RFC 5892 Section 2.6: Exceptions (PVALID)
  | Int
cp Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
0x00DF = CodePointStatus
PVALID  -- LATIN SMALL LETTER SHARP S
  | Int
cp Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
0x03C2 = CodePointStatus
PVALID  -- GREEK SMALL LETTER FINAL SIGMA
  | Int
cp Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
0x06FD = CodePointStatus
PVALID  -- ARABIC SIGN SINDHI AMPERSAND
  | Int
cp Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
0x06FE = CodePointStatus
PVALID  -- ARABIC SIGN SINDHI POSTPOSITION MEN
  | Int
cp Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
0x0F0B = CodePointStatus
PVALID  -- TIBETAN MARK INTERSYLLABIC TSHEG
  | Int
cp Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
0x3007 = CodePointStatus
PVALID  -- IDEOGRAPHIC NUMBER ZERO
  -- RFC 5892 Section 2.6: Exceptions (DISALLOWED)
  | Int
cp Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
0x0640 = CodePointStatus
DISALLOWED  -- ARABIC TATWEEL
  | Int
cp Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
0x07FA = CodePointStatus
DISALLOWED  -- NKO LAJANYALAN
  | Int
cp Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
0x302E = CodePointStatus
DISALLOWED  -- HANGUL SINGLE DOT TONE MARK
  | Int
cp Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
0x302F = CodePointStatus
DISALLOWED  -- HANGUL DOUBLE DOT TONE MARK
  | Int
cp Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
0x3031 = CodePointStatus
DISALLOWED  -- VERTICAL KANA REPEAT MARK
  | Int
cp Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
0x3032 = CodePointStatus
DISALLOWED  -- VERTICAL KANA REPEAT WITH VOICED SOUND MARK
  | Int
cp Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
0x3033 = CodePointStatus
DISALLOWED  -- VERTICAL KANA REPEAT MARK UPPER HALF
  | Int
cp Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
0x3034 = CodePointStatus
DISALLOWED  -- VERTICAL KANA REPEAT WITH VOICED SOUND MARK UPPER HALF
  | Int
cp Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
0x3035 = CodePointStatus
DISALLOWED  -- VERTICAL KANA REPEAT MARK LOWER HALF
  | Int
cp Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
0x303B = CodePointStatus
DISALLOWED  -- VERTICAL IDEOGRAPHIC ITERATION MARK
  -- ASCII exceptions that are DISALLOWED (per RFC 5892)
  | Int
cp Int -> [Int] -> Bool
forall a. Eq a => a -> [a] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` [Int
0x0020, Int
0x007F] = CodePointStatus
DISALLOWED  -- Space and DEL
  -- ASCII printable range (0x21-0x7E) is mostly PVALID
  | Int
cp Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
>= Int
0x0021 Bool -> Bool -> Bool
&& Int
cp Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
<= Int
0x007E = CodePointStatus
PVALID
  -- Letters and marks are generally PVALID
  | Text
cat Text -> [Text] -> Bool
forall a. Eq a => a -> [a] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` [Text
"Lu", Text
"Ll", Text
"Lt", Text
"Lm", Text
"Lo", Text
"Mn", Text
"Mc"] = CodePointStatus
PVALID
  -- Decimal numbers are PVALID
  | Text
cat Text -> Text -> Bool
forall a. Eq a => a -> a -> Bool
== Text
"Nd" = CodePointStatus
PVALID
  -- Control characters and format characters are DISALLOWED
  | Text
cat Text -> [Text] -> Bool
forall a. Eq a => a -> [a] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` [Text
"Cc", Text
"Cf"] = CodePointStatus
DISALLOWED
  -- Unassigned
  | Text
cat Text -> Text -> Bool
forall a. Eq a => a -> a -> Bool
== Text
"Cn" = CodePointStatus
UNASSIGNED
  -- Symbols, punctuation, separators, and other categories are DISALLOWED
  | Text
cat Text -> [Text] -> Bool
forall a. Eq a => a -> [a] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` [Text
"Zs", Text
"Zl", Text
"Zp", Text
"Pc", Text
"Pd", Text
"Ps", Text
"Pe", Text
"Pi", Text
"Pf", Text
"Po", 
                Text
"Sm", Text
"Sc", Text
"Sk", Text
"So", Text
"Me", Text
"No", Text
"Nl"] = CodePointStatus
DISALLOWED
  -- Default to DISALLOWED for safety
  | Bool
otherwise = CodePointStatus
DISALLOWED

parseCombiningMarks :: T.Text -> IntSet
parseCombiningMarks :: Text -> IntSet
parseCombiningMarks Text
text =
  let dataLines :: [Text]
dataLines = (Text -> Bool) -> [Text] -> [Text]
forall a. (a -> Bool) -> [a] -> [a]
filter (Bool -> Bool
not (Bool -> Bool) -> (Text -> Bool) -> Text -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> Bool
T.null) ([Text] -> [Text]) -> [Text] -> [Text]
forall a b. (a -> b) -> a -> b
$ Text -> [Text]
T.lines Text
text
      marks :: [Int]
marks = (Text -> Maybe Int) -> [Text] -> [Int]
forall {t :: * -> *} {t} {a}.
Foldable t =>
(t -> Maybe a) -> t t -> [a]
mapMaybe Text -> Maybe Int
parseCombiningMarkLine [Text]
dataLines
  in [Int] -> IntSet
IS.fromList [Int]
marks
  where
    mapMaybe :: (t -> Maybe a) -> t t -> [a]
mapMaybe t -> Maybe a
f = (t -> [a] -> [a]) -> [a] -> t t -> [a]
forall a b. (a -> b -> b) -> b -> t a -> b
forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr (\t
x [a]
acc -> case t -> Maybe a
f t
x of Just a
y -> a
y a -> [a] -> [a]
forall a. a -> [a] -> [a]
: [a]
acc; Maybe a
Nothing -> [a]
acc) []

parseCombiningMarkLine :: T.Text -> Maybe Int
parseCombiningMarkLine :: Text -> Maybe Int
parseCombiningMarkLine Text
line =
  case HasCallStack => Text -> Text -> [Text]
Text -> Text -> [Text]
T.splitOn Text
";" Text
line of
    (Text
cpHex:Text
_name:Text
cat:Text
ccc:[Text]
_rest) ->
      if Text -> Text
T.strip Text
cat Text -> Text -> Bool
forall a. Eq a => a -> a -> Bool
== Text
"Mn" Bool -> Bool -> Bool
|| Text -> Text
T.strip Text
cat Text -> Text -> Bool
forall a. Eq a => a -> a -> Bool
== Text
"Mc" Bool -> Bool -> Bool
|| Text -> Text
T.strip Text
cat Text -> Text -> Bool
forall a. Eq a => a -> a -> Bool
== Text
"Me" Bool -> Bool -> Bool
||
         Text -> Text
T.strip Text
ccc Text -> Text -> Bool
forall a. Eq a => a -> a -> Bool
/= Text
"0"
      then case Reader Int
forall a. Integral a => Reader a
TR.hexadecimal (Text -> Text
T.strip Text
cpHex) of
        Right (Int
cp, Text
_) -> Int -> Maybe Int
forall a. a -> Maybe a
Just Int
cp
        Left [Char]
_ -> Maybe Int
forall a. Maybe a
Nothing
      else Maybe Int
forall a. Maybe a
Nothing
    [Text]
_ -> Maybe Int
forall a. Maybe a
Nothing

parseViramas :: T.Text -> IntSet
parseViramas :: Text -> IntSet
parseViramas Text
text =
  let dataLines :: [Text]
dataLines = (Text -> Bool) -> [Text] -> [Text]
forall a. (a -> Bool) -> [a] -> [a]
filter (Bool -> Bool
not (Bool -> Bool) -> (Text -> Bool) -> Text -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> Bool
isComment) (Text -> [Text]
T.lines Text
text)
      viramas :: [Int]
viramas = (Text -> Maybe Int) -> [Text] -> [Int]
forall {t :: * -> *} {t} {a}.
Foldable t =>
(t -> Maybe a) -> t t -> [a]
mapMaybe Text -> Maybe Int
parseViramaLine [Text]
dataLines
  in [Int] -> IntSet
IS.fromList [Int]
viramas
  where
    mapMaybe :: (t -> Maybe a) -> t t -> [a]
mapMaybe t -> Maybe a
f = (t -> [a] -> [a]) -> [a] -> t t -> [a]
forall a b. (a -> b -> b) -> b -> t a -> b
forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr (\t
x [a]
acc -> case t -> Maybe a
f t
x of Just a
y -> a
y a -> [a] -> [a]
forall a. a -> [a] -> [a]
: [a]
acc; Maybe a
Nothing -> [a]
acc) []

parseViramaLine :: T.Text -> Maybe Int
parseViramaLine :: Text -> Maybe Int
parseViramaLine Text
line =
  let content :: Text
content = Text -> Text
T.strip (Text -> Text) -> Text -> Text
forall a b. (a -> b) -> a -> b
$ (Char -> Bool) -> Text -> Text
T.takeWhile (Char -> Char -> Bool
forall a. Eq a => a -> a -> Bool
/= Char
'#') Text
line
  in case HasCallStack => Text -> Text -> [Text]
Text -> Text -> [Text]
T.splitOn Text
";" Text
content of
    [Text
cpRange, Text
jType] | Text -> Text
T.strip Text
jType Text -> Text -> Bool
forall a. Eq a => a -> a -> Bool
== Text
"T" ->
      case Text -> Either [Char] (Int, Int)
parseCodePointRange (Text -> Text
T.strip Text
cpRange) of
        Right (Int
cp, Int
_) -> Int -> Maybe Int
forall a. a -> Maybe a
Just Int
cp  -- Just take first of range
        Left [Char]
_ -> Maybe Int
forall a. Maybe a
Nothing
    [Text]
_ -> Maybe Int
forall a. Maybe a
Nothing

parseScripts :: T.Text -> Either String (IntMap Script)
parseScripts :: Text -> Either [Char] (IntMap Script)
parseScripts Text
text = do
  let dataLines :: [Text]
dataLines = (Text -> Bool) -> [Text] -> [Text]
forall a. (a -> Bool) -> [a] -> [a]
filter (Bool -> Bool
not (Bool -> Bool) -> (Text -> Bool) -> Text -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> Bool
isComment) (Text -> [Text]
T.lines Text
text)
  [[(Int, Script)]]
entries <- (Text -> Either [Char] [(Int, Script)])
-> [Text] -> Either [Char] [[(Int, Script)]]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
forall (m :: * -> *) a b. Monad m => (a -> m b) -> [a] -> m [b]
mapM Text -> Either [Char] [(Int, Script)]
parseScriptLine [Text]
dataLines
  IntMap Script -> Either [Char] (IntMap Script)
forall a. a -> Either [Char] a
forall (m :: * -> *) a. Monad m => a -> m a
return (IntMap Script -> Either [Char] (IntMap Script))
-> IntMap Script -> Either [Char] (IntMap Script)
forall a b. (a -> b) -> a -> b
$ [(Int, Script)] -> IntMap Script
forall a. [(Int, a)] -> IntMap a
IM.fromList ([[(Int, Script)]] -> [(Int, Script)]
forall (t :: * -> *) a. Foldable t => t [a] -> [a]
concat [[(Int, Script)]]
entries)

parseScriptLine :: T.Text -> Either String [(Int, Script)]
parseScriptLine :: Text -> Either [Char] [(Int, Script)]
parseScriptLine Text
line =
  let content :: Text
content = Text -> Text
T.strip (Text -> Text) -> Text -> Text
forall a b. (a -> b) -> a -> b
$ (Char -> Bool) -> Text -> Text
T.takeWhile (Char -> Char -> Bool
forall a. Eq a => a -> a -> Bool
/= Char
'#') Text
line
  in case HasCallStack => Text -> Text -> [Text]
Text -> Text -> [Text]
T.splitOn Text
";" Text
content of
    [] -> [(Int, Script)] -> Either [Char] [(Int, Script)]
forall a b. b -> Either a b
Right []
    [Text
cpRange, Text
scriptName] -> do
      (Int
startCP, Int
endCP) <- Text -> Either [Char] (Int, Int)
parseCodePointRange (Text -> Text
T.strip Text
cpRange)
      Script
script <- Text -> Either [Char] Script
parseScriptName (Text -> Text
T.strip Text
scriptName)
      [(Int, Script)] -> Either [Char] [(Int, Script)]
forall a. a -> Either [Char] a
forall (m :: * -> *) a. Monad m => a -> m a
return [(Int
cp, Script
script) | Int
cp <- [Int
startCP..Int
endCP]]
    [Text]
_ -> case Text -> [Text]
T.words Text
content of
      [] -> [(Int, Script)] -> Either [Char] [(Int, Script)]
forall a b. b -> Either a b
Right []
      [Text
cpRange, Text
scriptName] -> do
        (Int
startCP, Int
endCP) <- Text -> Either [Char] (Int, Int)
parseCodePointRange (Text -> Text
T.strip Text
cpRange)
        Script
script <- Text -> Either [Char] Script
parseScriptName (Text -> Text
T.strip Text
scriptName)
        [(Int, Script)] -> Either [Char] [(Int, Script)]
forall a. a -> Either [Char] a
forall (m :: * -> *) a. Monad m => a -> m a
return [(Int
cp, Script
script) | Int
cp <- [Int
startCP..Int
endCP]]
      [Text]
_ -> [Char] -> Either [Char] [(Int, Script)]
forall a b. a -> Either a b
Left ([Char] -> Either [Char] [(Int, Script)])
-> [Char] -> Either [Char] [(Int, Script)]
forall a b. (a -> b) -> a -> b
$ [Char]
"Invalid script line: " [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ Text -> [Char]
T.unpack Text
line

parseScriptName :: T.Text -> Either String Script
parseScriptName :: Text -> Either [Char] Script
parseScriptName Text
"Arabic" = Script -> Either [Char] Script
forall a b. b -> Either a b
Right Script
Arabic
parseScriptName Text
"Hebrew" = Script -> Either [Char] Script
forall a b. b -> Either a b
Right Script
Hebrew
parseScriptName Text
"Greek" = Script -> Either [Char] Script
forall a b. b -> Either a b
Right Script
Greek
parseScriptName Text
"Hiragana" = Script -> Either [Char] Script
forall a b. b -> Either a b
Right Script
Hiragana
parseScriptName Text
"Katakana" = Script -> Either [Char] Script
forall a b. b -> Either a b
Right Script
Katakana
parseScriptName Text
"Han" = Script -> Either [Char] Script
forall a b. b -> Either a b
Right Script
Han
parseScriptName Text
"Hangul" = Script -> Either [Char] Script
forall a b. b -> Either a b
Right Script
Hangul
parseScriptName Text
"Latin" = Script -> Either [Char] Script
forall a b. b -> Either a b
Right Script
Latin
parseScriptName Text
"Cyrillic" = Script -> Either [Char] Script
forall a b. b -> Either a b
Right Script
Cyrillic
parseScriptName Text
"Devanagari" = Script -> Either [Char] Script
forall a b. b -> Either a b
Right Script
Devanagari
parseScriptName Text
"Bengali" = Script -> Either [Char] Script
forall a b. b -> Either a b
Right Script
Bengali
parseScriptName Text
"Gurmukhi" = Script -> Either [Char] Script
forall a b. b -> Either a b
Right Script
Gurmukhi
parseScriptName Text
"Gujarati" = Script -> Either [Char] Script
forall a b. b -> Either a b
Right Script
Gujarati
parseScriptName Text
"Oriya" = Script -> Either [Char] Script
forall a b. b -> Either a b
Right Script
Oriya
parseScriptName Text
"Tamil" = Script -> Either [Char] Script
forall a b. b -> Either a b
Right Script
Tamil
parseScriptName Text
"Telugu" = Script -> Either [Char] Script
forall a b. b -> Either a b
Right Script
Telugu
parseScriptName Text
"Kannada" = Script -> Either [Char] Script
forall a b. b -> Either a b
Right Script
Kannada
parseScriptName Text
"Malayalam" = Script -> Either [Char] Script
forall a b. b -> Either a b
Right Script
Malayalam
parseScriptName Text
"Thai" = Script -> Either [Char] Script
forall a b. b -> Either a b
Right Script
Thai
parseScriptName Text
"Lao" = Script -> Either [Char] Script
forall a b. b -> Either a b
Right Script
Lao
parseScriptName Text
"Tibetan" = Script -> Either [Char] Script
forall a b. b -> Either a b
Right Script
Tibetan
parseScriptName Text
"Myanmar" = Script -> Either [Char] Script
forall a b. b -> Either a b
Right Script
Myanmar
parseScriptName Text
"Georgian" = Script -> Either [Char] Script
forall a b. b -> Either a b
Right Script
Georgian
parseScriptName Text
_ = Script -> Either [Char] Script
forall a b. b -> Either a b
Right Script
OtherScript

isComment :: T.Text -> Bool
isComment :: Text -> Bool
isComment Text
line = Text -> Bool
T.null (Text -> Text
T.strip Text
line) Bool -> Bool -> Bool
|| Text
"#" Text -> Text -> Bool
`T.isPrefixOf` Text -> Text
T.strip Text
line