{-# LANGUAGE OverloadedStrings #-}
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)
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)
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
| 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
| Bool
otherwise = () -> Either IDNError ()
forall a. a -> Either IDNError a
forall (f :: * -> *) a. Applicative f => a -> f a
pure ()
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 ()
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))
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
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
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)
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)
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)
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)
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)
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)
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)
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 ()
validateRTLLabel :: Text -> Either IDNError ()
validateRTLLabel :: Text -> Either IDNError ()
validateRTLLabel Text
label = do
let len :: Int
len = Text -> Int
T.length Text
label
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 ()
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 ()
Int -> Int -> Either IDNError ()
checkNSMPositions Int
0 Int
len
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
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 ()
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
}
data BidiFlags = BidiFlags
{ BidiFlags -> Bool
bfHasAN :: {-# UNPACK #-} !Bool
, BidiFlags -> Bool
bfHasEN :: {-# UNPACK #-} !Bool
, BidiFlags -> Bool
bfHasESorCS :: {-# UNPACK #-} !Bool
}