{-# LANGUAGE OverloadedStrings #-}

-- | Internal IDN contextual and bidirectional validation rules.
--
-- This module provides validation functions used internally for IDN processing.
-- For public API, see 'Data.Text.IDN'.
module Data.Text.IDN.Internal.Validation
  ( validateContextualRules
  , validateBidiRules
  ) where

import Data.Text (Text)
import qualified Data.Text as T
import Data.Text.IDN.Types
import Data.Text.IDN.Internal.Unicode
import Data.Char (ord)

-- | Validate contextual rules for CONTEXTJ and CONTEXTO code points (RFC 5892 Appendix A)
validateContextualRules :: Text -> Either IDNError ()
validateContextualRules :: Text -> Either IDNError ()
validateContextualRules Text
label = Int -> Either IDNError ()
go Int
0
  where
    len :: Int
len = Text -> Int
T.length Text
label
    go :: Int -> Either IDNError ()
go Int
pos
      | Int
pos Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
>= Int
len = () -> Either IDNError ()
forall a. a -> Either IDNError a
forall (f :: * -> *) a. Applicative f => a -> f a
pure ()
      | Bool
otherwise = do
          let c :: Char
c = HasCallStack => Text -> Int -> Char
Text -> Int -> Char
T.index Text
label Int
pos
          case Char -> CodePointStatus
codePointStatus Char
c of
            CodePointStatus
CONTEXTJ -> Text -> Int -> Char -> Either IDNError ()
validateContextJ Text
label Int
pos Char
c
            CodePointStatus
CONTEXTO -> Text -> Int -> Char -> Either IDNError ()
validateContextO Text
label Int
pos Char
c
            CodePointStatus
_ -> () -> Either IDNError ()
forall a. a -> Either IDNError a
forall (f :: * -> *) a. Applicative f => a -> f a
pure ()
          Int -> Either IDNError ()
go (Int
pos Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1)

-- | Validate CONTEXTJ characters (Join Controls)
validateContextJ :: Text -> Int -> Char -> Either IDNError ()
validateContextJ :: Text -> Int -> Char -> Either IDNError ()
validateContextJ Text
label Int
pos Char
c
  | Char -> Int
ord Char
c Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
0x200C = Text -> Int -> Char -> Either IDNError ()
validateZWNJ Text
label Int
pos Char
c  -- ZERO WIDTH NON-JOINER
  | Char -> Int
ord Char
c Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
0x200D = Text -> Int -> Char -> Either IDNError ()
validateZWJ Text
label Int
pos Char
c   -- ZERO WIDTH JOINER
  | Bool
otherwise = () -> Either IDNError ()
forall a. a -> Either IDNError a
forall (f :: * -> *) a. Applicative f => a -> f a
pure ()

-- | Validate CONTEXTO characters (Other)
validateContextO :: Text -> Int -> Char -> Either IDNError ()
validateContextO :: Text -> Int -> Char -> Either IDNError ()
validateContextO Text
label Int
pos Char
c =
  case Char -> Maybe ContextRule
lookupContextRule Char
c of
    Just ContextRule
MiddleDotRule -> Text -> Int -> Char -> Either IDNError ()
validateMiddleDot Text
label Int
pos Char
c
    Just ContextRule
GreekKeraiaRule -> Text -> Int -> Char -> Either IDNError ()
validateGreekKeraia Text
label Int
pos Char
c
    Just ContextRule
HebrewGereshRule -> Text -> Int -> Char -> Either IDNError ()
validateHebrewPunctuation Text
label Int
pos Char
c
    Just ContextRule
HebrewGershayimRule -> Text -> Int -> Char -> Either IDNError ()
validateHebrewPunctuation Text
label Int
pos Char
c
    Just ContextRule
KatakanaMiddleDotRule -> Text -> Int -> Char -> Either IDNError ()
validateKatakanaMiddleDot Text
label Int
pos Char
c
    Just ContextRule
ArabicIndicDigitsRule -> Text -> Int -> Char -> Either IDNError ()
validateArabicIndicDigits Text
label Int
pos Char
c
    Just ContextRule
ExtendedArabicIndicDigitsRule -> Text -> Int -> Char -> Either IDNError ()
validateExtendedArabicIndicDigits Text
label Int
pos Char
c
    Maybe ContextRule
_ -> () -> Either IDNError ()
forall a. a -> Either IDNError a
forall (f :: * -> *) a. Applicative f => a -> f a
pure ()

-- | ZERO WIDTH NON-JOINER (U+200C) - RFC 5892 Appendix A.1
-- Allowed after Virama OR in Arabic script contexts matching the regexp
validateZWNJ :: Text -> Int -> Char -> Either IDNError ()
validateZWNJ :: Text -> Int -> Char -> Either IDNError ()
validateZWNJ Text
label Int
pos Char
c =
  let precededByVirama :: Bool
precededByVirama = Int
pos Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> Int
0 Bool -> Bool -> Bool
&& Char -> Bool
isVirama (HasCallStack => Text -> Int -> Char
Text -> Int -> Char
T.index Text
label (Int
pos Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1))
      -- Arabic context: check if surrounded by Arabic joining characters
      -- Per W3C ALREQ, ZWNJ is used in Arabic between joining letters
      inArabicContext :: Bool
inArabicContext = Int
pos Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> Int
0 Bool -> Bool -> Bool
&& Int
pos Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Text -> Int
T.length Text
label Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1 Bool -> Bool -> Bool
&&
                       Char -> Bool
isArabicJoining (HasCallStack => Text -> Int -> Char
Text -> Int -> Char
T.index Text
label (Int
pos Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1)) Bool -> Bool -> Bool
&&
                       Char -> Bool
isArabicJoining (HasCallStack => Text -> Int -> Char
Text -> Int -> Char
T.index Text
label (Int
pos Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1))
  in if Bool
precededByVirama Bool -> Bool -> Bool
|| Bool
inArabicContext
       then () -> Either IDNError ()
forall a. a -> Either IDNError a
forall (f :: * -> *) a. Applicative f => a -> f a
pure ()
       else IDNError -> Either IDNError ()
forall a b. a -> Either a b
Left (Char -> ContextRule -> Int -> Text -> IDNError
InvalidContext Char
c ContextRule
ZWNJRule Int
pos Text
label)
  where
    -- Characters in Arabic script that can join (simplified check)
    isArabicJoining :: Char -> Bool
isArabicJoining Char
ch = Char -> Maybe Script
scriptOf Char
ch Maybe Script -> Maybe Script -> Bool
forall a. Eq a => a -> a -> Bool
== Script -> Maybe Script
forall a. a -> Maybe a
Just Script
Arabic Bool -> Bool -> Bool
&& Char -> Int
ord Char
ch Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
>= Int
0x0620 Bool -> Bool -> Bool
&& Char -> Int
ord Char
ch Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
<= Int
0x06FF

-- | ZERO WIDTH JOINER (U+200D) - RFC 5892 Appendix A.2
validateZWJ :: Text -> Int -> Char -> Either IDNError ()
validateZWJ :: Text -> Int -> Char -> Either IDNError ()
validateZWJ Text
label Int
pos Char
c =
  if Int
pos Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> Int
0 Bool -> Bool -> Bool
&& Char -> Bool
isVirama (HasCallStack => Text -> Int -> Char
Text -> Int -> Char
T.index Text
label (Int
pos Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1))
    then () -> Either IDNError ()
forall a. a -> Either IDNError a
forall (f :: * -> *) a. Applicative f => a -> f a
pure ()
    else IDNError -> Either IDNError ()
forall a b. a -> Either a b
Left (Char -> ContextRule -> Int -> Text -> IDNError
InvalidContext Char
c ContextRule
ZWJRule Int
pos Text
label)

-- | MIDDLE DOT (U+00B7) - RFC 5892 Appendix A.3
validateMiddleDot :: Text -> Int -> Char -> Either IDNError ()
validateMiddleDot :: Text -> Int -> Char -> Either IDNError ()
validateMiddleDot Text
label Int
pos Char
c =
  let before :: Bool
before = Int
pos Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> Int
0 Bool -> Bool -> Bool
&& HasCallStack => Text -> Int -> Char
Text -> Int -> Char
T.index Text
label (Int
pos Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1) Char -> Char -> Bool
forall a. Eq a => a -> a -> Bool
== Char
'l'
      after :: Bool
after = Int
pos Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Text -> Int
T.length Text
label Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1 Bool -> Bool -> Bool
&& HasCallStack => Text -> Int -> Char
Text -> Int -> Char
T.index Text
label (Int
pos Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1) Char -> Char -> Bool
forall a. Eq a => a -> a -> Bool
== Char
'l'
  in if Bool
before Bool -> Bool -> Bool
&& Bool
after
       then () -> Either IDNError ()
forall a. a -> Either IDNError a
forall (f :: * -> *) a. Applicative f => a -> f a
pure ()
       else IDNError -> Either IDNError ()
forall a b. a -> Either a b
Left (Char -> ContextRule -> Int -> Text -> IDNError
InvalidContext Char
c ContextRule
MiddleDotRule Int
pos Text
label)

-- | GREEK LOWER NUMERAL SIGN (U+0375) - RFC 5892 Appendix A.4
-- The script of the following character MUST be Greek
validateGreekKeraia :: Text -> Int -> Char -> Either IDNError ()
validateGreekKeraia :: Text -> Int -> Char -> Either IDNError ()
validateGreekKeraia Text
label Int
pos Char
c =
  let followedByGreek :: Bool
followedByGreek = Int
pos Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Text -> Int
T.length Text
label Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1 Bool -> Bool -> Bool
&&
                       Char -> Maybe Script
scriptOf (HasCallStack => Text -> Int -> Char
Text -> Int -> Char
T.index Text
label (Int
pos Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1)) Maybe Script -> Maybe Script -> Bool
forall a. Eq a => a -> a -> Bool
== Script -> Maybe Script
forall a. a -> Maybe a
Just Script
Greek
  in if Bool
followedByGreek
       then () -> Either IDNError ()
forall a. a -> Either IDNError a
forall (f :: * -> *) a. Applicative f => a -> f a
pure ()
       else IDNError -> Either IDNError ()
forall a b. a -> Either a b
Left (Char -> ContextRule -> Int -> Text -> IDNError
InvalidContext Char
c ContextRule
GreekKeraiaRule Int
pos Text
label)

-- | HEBREW PUNCTUATION (U+05F3, U+05F4) - RFC 5892 Appendix A.5/A.6
-- The script of the preceding character MUST be Hebrew
validateHebrewPunctuation :: Text -> Int -> Char -> Either IDNError ()
validateHebrewPunctuation :: Text -> Int -> Char -> Either IDNError ()
validateHebrewPunctuation Text
label Int
pos Char
c =
  let precededByHebrew :: Bool
precededByHebrew = Int
pos Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> Int
0 Bool -> Bool -> Bool
&&
                        Char -> Maybe Script
scriptOf (HasCallStack => Text -> Int -> Char
Text -> Int -> Char
T.index Text
label (Int
pos Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1)) Maybe Script -> Maybe Script -> Bool
forall a. Eq a => a -> a -> Bool
== Script -> Maybe Script
forall a. a -> Maybe a
Just Script
Hebrew
  in if Bool
precededByHebrew
       then () -> Either IDNError ()
forall a. a -> Either IDNError a
forall (f :: * -> *) a. Applicative f => a -> f a
pure ()
       else IDNError -> Either IDNError ()
forall a b. a -> Either a b
Left (Char -> ContextRule -> Int -> Text -> IDNError
InvalidContext Char
c ContextRule
HebrewGereshRule Int
pos Text
label)

-- | KATAKANA MIDDLE DOT (U+30FB) - RFC 5892 Appendix A.7
validateKatakanaMiddleDot :: Text -> Int -> Char -> Either IDNError ()
validateKatakanaMiddleDot :: Text -> Int -> Char -> Either IDNError ()
validateKatakanaMiddleDot Text
label Int
pos Char
c =
  let hasKatakanaOrHiragana :: Bool
hasKatakanaOrHiragana = (Char -> Bool) -> Text -> Bool
T.any (\Char
ch -> 
        let s :: Maybe Script
s = Char -> Maybe Script
scriptOf Char
ch
        in Maybe Script
s Maybe Script -> Maybe Script -> Bool
forall a. Eq a => a -> a -> Bool
== Script -> Maybe Script
forall a. a -> Maybe a
Just Script
Katakana Bool -> Bool -> Bool
|| Maybe Script
s Maybe Script -> Maybe Script -> Bool
forall a. Eq a => a -> a -> Bool
== Script -> Maybe Script
forall a. a -> Maybe a
Just Script
Hiragana Bool -> Bool -> Bool
|| Maybe Script
s Maybe Script -> Maybe Script -> Bool
forall a. Eq a => a -> a -> Bool
== Script -> Maybe Script
forall a. a -> Maybe a
Just Script
Han
        ) Text
label
  in if Bool
hasKatakanaOrHiragana
       then () -> Either IDNError ()
forall a. a -> Either IDNError a
forall (f :: * -> *) a. Applicative f => a -> f a
pure ()
       else IDNError -> Either IDNError ()
forall a b. a -> Either a b
Left (Char -> ContextRule -> Int -> Text -> IDNError
InvalidContext Char
c ContextRule
KatakanaMiddleDotRule Int
pos Text
label)

-- | ARABIC-INDIC DIGITS (U+0660..U+0669) - RFC 5892 Appendix A.8
validateArabicIndicDigits :: Text -> Int -> Char -> Either IDNError ()
validateArabicIndicDigits :: Text -> Int -> Char -> Either IDNError ()
validateArabicIndicDigits Text
label Int
pos Char
c =
  let hasExtended :: Bool
hasExtended = (Char -> Bool) -> Text -> Bool
T.any (\Char
ch -> Char -> Int
ord Char
ch Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
>= Int
0x06F0 Bool -> Bool -> Bool
&& Char -> Int
ord Char
ch Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
<= Int
0x06F9) Text
label
  in if Bool -> Bool
not Bool
hasExtended
       then () -> Either IDNError ()
forall a. a -> Either IDNError a
forall (f :: * -> *) a. Applicative f => a -> f a
pure ()
       else IDNError -> Either IDNError ()
forall a b. a -> Either a b
Left (Char -> ContextRule -> Int -> Text -> IDNError
InvalidContext Char
c ContextRule
ArabicIndicDigitsRule Int
pos Text
label)

-- | EXTENDED ARABIC-INDIC DIGITS (U+06F0..U+06F9) - RFC 5892 Appendix A.9
validateExtendedArabicIndicDigits :: Text -> Int -> Char -> Either IDNError ()
validateExtendedArabicIndicDigits :: Text -> Int -> Char -> Either IDNError ()
validateExtendedArabicIndicDigits Text
label Int
pos Char
c =
  let hasNormal :: Bool
hasNormal = (Char -> Bool) -> Text -> Bool
T.any (\Char
ch -> Char -> Int
ord Char
ch Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
>= Int
0x0660 Bool -> Bool -> Bool
&& Char -> Int
ord Char
ch Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
<= Int
0x0669) Text
label
  in if Bool -> Bool
not Bool
hasNormal
       then () -> Either IDNError ()
forall a. a -> Either IDNError a
forall (f :: * -> *) a. Applicative f => a -> f a
pure ()
       else IDNError -> Either IDNError ()
forall a b. a -> Either a b
Left (Char -> ContextRule -> Int -> Text -> IDNError
InvalidContext Char
c ContextRule
ExtendedArabicIndicDigitsRule Int
pos Text
label)

-- | Validate bidirectional text rules (RFC 5893)
validateBidiRules :: Text -> Either IDNError ()
validateBidiRules :: Text -> Either IDNError ()
validateBidiRules Text
label
  | Text -> Bool
T.null Text
label = () -> Either IDNError ()
forall a. a -> Either IDNError a
forall (f :: * -> *) a. Applicative f => a -> f a
pure ()
  | Bool
otherwise =
      let hasRTL :: Bool
hasRTL = (Char -> Bool) -> Text -> Bool
T.any (\Char
c -> let bc :: BidiClass
bc = Char -> BidiClass
bidiClass Char
c in BidiClass
bc BidiClass -> BidiClass -> Bool
forall a. Eq a => a -> a -> Bool
== BidiClass
R Bool -> Bool -> Bool
|| BidiClass
bc BidiClass -> BidiClass -> Bool
forall a. Eq a => a -> a -> Bool
== BidiClass
AL Bool -> Bool -> Bool
|| BidiClass
bc BidiClass -> BidiClass -> Bool
forall a. Eq a => a -> a -> Bool
== BidiClass
AN) Text
label
      in if Bool
hasRTL
           then Text -> Either IDNError ()
validateRTLLabel Text
label
           else () -> Either IDNError ()
forall a. a -> Either IDNError a
forall (f :: * -> *) a. Applicative f => a -> f a
pure ()  -- LTR labels have minimal bidi restrictions

-- | Validate RTL labels (RFC 5893 Rules 2-6)
validateRTLLabel :: Text -> Either IDNError ()
validateRTLLabel :: Text -> Either IDNError ()
validateRTLLabel Text
label = do
  let len :: Int
len = Text -> Int
T.length Text
label
  
  -- Rule 2: First character must be R or AL
  let firstBC :: BidiClass
firstBC = Char -> BidiClass
bidiClass (HasCallStack => Text -> Int -> Char
Text -> Int -> Char
T.index Text
label Int
0)
  if BidiClass
firstBC BidiClass -> [BidiClass] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`notElem` [BidiClass
R, BidiClass
AL]
    then IDNError -> Either IDNError ()
forall a b. a -> Either a b
Left (BidiRule -> Text -> IDNError
BidiViolation BidiRule
Rule2 Text
label)
    else () -> Either IDNError ()
forall a. a -> Either IDNError a
forall (f :: * -> *) a. Applicative f => a -> f a
pure ()
  
  -- Rule 3: Last character must be R, AL, EN, or AN
  let lastBC :: BidiClass
lastBC = Char -> BidiClass
bidiClass (HasCallStack => Text -> Int -> Char
Text -> Int -> Char
T.index Text
label (Int
len Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1))
  if BidiClass
lastBC BidiClass -> [BidiClass] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`notElem` [BidiClass
R, BidiClass
AL, BidiClass
EN, BidiClass
AN]
    then IDNError -> Either IDNError ()
forall a b. a -> Either a b
Left (BidiRule -> Text -> IDNError
BidiViolation BidiRule
Rule3 Text
label)
    else () -> Either IDNError ()
forall a. a -> Either IDNError a
forall (f :: * -> *) a. Applicative f => a -> f a
pure ()
  
  -- Rule 4: NSM must follow allowed characters
  Int -> Int -> Either IDNError ()
checkNSMPositions Int
0 Int
len
  
  -- Rules 5 & 6: Collect bidi class flags in a single pass
  let flags :: BidiFlags
flags = (BidiFlags -> Char -> BidiFlags) -> BidiFlags -> Text -> BidiFlags
forall a. (a -> Char -> a) -> a -> Text -> a
T.foldl' BidiFlags -> Char -> BidiFlags
checkBidiClass (Bool -> Bool -> Bool -> BidiFlags
BidiFlags Bool
False Bool
False Bool
False) Text
label
  
  -- Rule 5: No ES or CS with AN
  if BidiFlags -> Bool
bfHasAN BidiFlags
flags Bool -> Bool -> Bool
&& BidiFlags -> Bool
bfHasESorCS BidiFlags
flags
    then IDNError -> Either IDNError ()
forall a b. a -> Either a b
Left (BidiRule -> Text -> IDNError
BidiViolation BidiRule
Rule5 Text
label)
    else () -> Either IDNError ()
forall a. a -> Either IDNError a
forall (f :: * -> *) a. Applicative f => a -> f a
pure ()
  
  -- Rule 6: EN and AN cannot be mixed
  if BidiFlags -> Bool
bfHasEN BidiFlags
flags Bool -> Bool -> Bool
&& BidiFlags -> Bool
bfHasAN BidiFlags
flags
    then IDNError -> Either IDNError ()
forall a b. a -> Either a b
Left (BidiRule -> Text -> IDNError
BidiViolation BidiRule
Rule6 Text
label)
    else () -> Either IDNError ()
forall a. a -> Either IDNError a
forall (f :: * -> *) a. Applicative f => a -> f a
pure ()
  
  where
    checkNSMPositions :: Int -> Int -> Either IDNError ()
checkNSMPositions Int
pos Int
len
      | Int
pos Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
>= Int
len = () -> Either IDNError ()
forall a. a -> Either IDNError a
forall (f :: * -> *) a. Applicative f => a -> f a
pure ()
      | Bool
otherwise = do
          let currentBC :: BidiClass
currentBC = Char -> BidiClass
bidiClass (HasCallStack => Text -> Int -> Char
Text -> Int -> Char
T.index Text
label Int
pos)
          if BidiClass
currentBC BidiClass -> BidiClass -> Bool
forall a. Eq a => a -> a -> Bool
== BidiClass
NSM Bool -> Bool -> Bool
&& Int
pos Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> Int
0
            then do
              let prevBC :: BidiClass
prevBC = Char -> BidiClass
bidiClass (HasCallStack => Text -> Int -> Char
Text -> Int -> Char
T.index Text
label (Int
pos Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1))
              if BidiClass
prevBC BidiClass -> [BidiClass] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`notElem` [BidiClass
R, BidiClass
AL, BidiClass
EN, BidiClass
AN]
                then IDNError -> Either IDNError ()
forall a b. a -> Either a b
Left (BidiRule -> Text -> IDNError
BidiViolation BidiRule
Rule4 Text
label)
                else Int -> Int -> Either IDNError ()
checkNSMPositions (Int
pos Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1) Int
len
            else Int -> Int -> Either IDNError ()
checkNSMPositions (Int
pos Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1) Int
len
    
    checkBidiClass :: BidiFlags -> Char -> BidiFlags
checkBidiClass BidiFlags
acc Char
c =
      let bc :: BidiClass
bc = Char -> BidiClass
bidiClass Char
c
      in BidiFlags
           { bfHasAN :: Bool
bfHasAN = BidiFlags -> Bool
bfHasAN BidiFlags
acc Bool -> Bool -> Bool
|| BidiClass
bc BidiClass -> BidiClass -> Bool
forall a. Eq a => a -> a -> Bool
== BidiClass
AN
           , bfHasEN :: Bool
bfHasEN = BidiFlags -> Bool
bfHasEN BidiFlags
acc Bool -> Bool -> Bool
|| BidiClass
bc BidiClass -> BidiClass -> Bool
forall a. Eq a => a -> a -> Bool
== BidiClass
EN
           , bfHasESorCS :: Bool
bfHasESorCS = BidiFlags -> Bool
bfHasESorCS BidiFlags
acc Bool -> Bool -> Bool
|| BidiClass
bc BidiClass -> BidiClass -> Bool
forall a. Eq a => a -> a -> Bool
== BidiClass
ES Bool -> Bool -> Bool
|| BidiClass
bc BidiClass -> BidiClass -> Bool
forall a. Eq a => a -> a -> Bool
== BidiClass
CS
           }

-- | Strict accumulator for collecting bidi class information in a single pass
data BidiFlags = BidiFlags
  { BidiFlags -> Bool
bfHasAN     :: {-# UNPACK #-} !Bool  -- ^ Has Arabic-Indic digits (AN)
  , BidiFlags -> Bool
bfHasEN     :: {-# UNPACK #-} !Bool  -- ^ Has European numbers (EN)
  , BidiFlags -> Bool
bfHasESorCS :: {-# UNPACK #-} !Bool  -- ^ Has European or Common separators (ES/CS)
  }