{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE BangPatterns #-}
{-# LANGUAGE MagicHash #-}
{-# LANGUAGE UnboxedTuples #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE UnboxedSums #-}
{-# LANGUAGE PatternSynonyms #-}
{-# LANGUAGE ViewPatterns #-}
{-# LANGUAGE KindSignatures #-}
{-# LANGUAGE PolyKinds #-}
module Data.Text.IDN.Internal.Punycode
( encodePunycode
, decodePunycode
) where
import Data.Text (Text)
import qualified Data.Text as T
import qualified Data.Text.Array as A
import Data.Text.Internal (Text(..))
import Data.Char (ord, chr, isAscii)
import Data.Bits ((.&.), (.|.), shiftR, shiftL)
import Data.Text.IDN.Types (PunycodeError(..), PunycodeState(..), initialState)
import Control.Monad.ST (ST, runST)
import qualified Data.Vector.Unboxed.Mutable as UM
import qualified Data.Vector.Unboxed as U
import qualified Data.Vector.Algorithms.Intro as VA
import GHC.Exts (State#, TYPE, RuntimeRep)
import GHC.ST (ST(ST))
base :: Int
base :: Int
base = Int
36
tmin :: Int
tmin :: Int
tmin = Int
1
tmax :: Int
tmax :: Int
tmax = Int
26
skew :: Int
skew :: Int
skew = Int
38
damp :: Int
damp :: Int
damp = Int
700
initialBias :: Int
initialBias :: Int
initialBias = Int
72
initialN :: Int
initialN :: Int
initialN = Int
0x80
delimiter :: Char
delimiter :: Char
delimiter = Char
'-'
extractSortedNonBasicCPs :: U.Vector Int -> U.Vector Int
Vector Int
arr = (forall s. ST s (Vector Int)) -> Vector Int
forall a. (forall s. ST s a) -> a
runST ((forall s. ST s (Vector Int)) -> Vector Int)
-> (forall s. ST s (Vector Int)) -> Vector Int
forall a b. (a -> b) -> a -> b
$ do
let len :: Int
len = Vector Int -> Int
forall a. Unbox a => Vector a -> Int
U.length Vector Int
arr
MVector s Int
mvec <- Int -> ST s (MVector (PrimState (ST s)) Int)
forall (m :: * -> *) a.
(PrimMonad m, Unbox a) =>
Int -> m (MVector (PrimState m) a)
UM.new Int
len
Int
count <- Int -> Int -> MVector (PrimState (ST s)) Int -> ST s Int
forall {f :: * -> *}.
PrimMonad f =>
Int -> Int -> MVector (PrimState f) Int -> f Int
collectNonBasic Int
0 Int
0 MVector s Int
MVector (PrimState (ST s)) Int
mvec
if Int
count Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
0
then Vector Int -> ST s (Vector Int)
forall a. a -> ST s a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Vector Int
forall a. Unbox a => Vector a
U.empty
else do
MVector (PrimState (ST s)) Int -> ST s ()
forall (m :: * -> *) (v :: * -> * -> *) e.
(PrimMonad m, MVector v e, Ord e) =>
v (PrimState m) e -> m ()
VA.sort (Int -> MVector s Int -> MVector s Int
forall a s. Unbox a => Int -> MVector s a -> MVector s a
UM.take Int
count MVector s Int
mvec)
Int
uniqueCount <- MVector (PrimState (ST s)) Int -> Int -> ST s Int
forall {m :: * -> *} {a}.
(PrimMonad m, Unbox a, Eq a) =>
MVector (PrimState m) a -> Int -> m Int
dedupInPlace MVector s Int
MVector (PrimState (ST s)) Int
mvec Int
count
MVector (PrimState (ST s)) Int -> ST s (Vector Int)
forall a (m :: * -> *).
(Unbox a, PrimMonad m) =>
MVector (PrimState m) a -> m (Vector a)
U.unsafeFreeze (Int -> MVector s Int -> MVector s Int
forall a s. Unbox a => Int -> MVector s a -> MVector s a
UM.take Int
uniqueCount MVector s Int
mvec)
where
collectNonBasic :: Int -> Int -> MVector (PrimState f) Int -> f Int
collectNonBasic !Int
i !Int
outIdx !MVector (PrimState f) Int
mvec
| Int
i Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
>= Vector Int -> Int
forall a. Unbox a => Vector a -> Int
U.length Vector Int
arr = Int -> f Int
forall a. a -> f a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Int
outIdx
| Bool
otherwise = do
let cp :: Int
cp = Vector Int -> Int -> Int
forall a. Unbox a => Vector a -> Int -> a
U.unsafeIndex Vector Int
arr Int
i
if Int -> Bool
isBasicCP Int
cp
then Int -> Int -> MVector (PrimState f) Int -> f Int
collectNonBasic (Int
i Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1) Int
outIdx MVector (PrimState f) Int
mvec
else do
MVector (PrimState f) Int -> Int -> Int -> f ()
forall (m :: * -> *) a.
(PrimMonad m, Unbox a) =>
MVector (PrimState m) a -> Int -> a -> m ()
UM.unsafeWrite MVector (PrimState f) Int
mvec Int
outIdx Int
cp
Int -> Int -> MVector (PrimState f) Int -> f Int
collectNonBasic (Int
i Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1) (Int
outIdx Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1) MVector (PrimState f) Int
mvec
dedupInPlace :: MVector (PrimState m) a -> Int -> m Int
dedupInPlace !MVector (PrimState m) a
mvec !Int
count = do
a
first <- MVector (PrimState m) a -> Int -> m a
forall (m :: * -> *) a.
(PrimMonad m, Unbox a) =>
MVector (PrimState m) a -> Int -> m a
UM.unsafeRead MVector (PrimState m) a
mvec Int
0
Int -> Int -> a -> MVector (PrimState m) a -> Int -> m Int
forall {f :: * -> *} {a}.
(PrimMonad f, Unbox a, Eq a) =>
Int -> Int -> a -> MVector (PrimState f) a -> Int -> f Int
compactFrom Int
1 Int
1 a
first MVector (PrimState m) a
mvec Int
count
compactFrom :: Int -> Int -> a -> MVector (PrimState f) a -> Int -> f Int
compactFrom !Int
readIdx !Int
writeIdx !a
prev !MVector (PrimState f) a
mvec !Int
len
| Int
readIdx Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
>= Int
len = Int -> f Int
forall a. a -> f a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Int
writeIdx
| Bool
otherwise = do
a
curr <- MVector (PrimState f) a -> Int -> f a
forall (m :: * -> *) a.
(PrimMonad m, Unbox a) =>
MVector (PrimState m) a -> Int -> m a
UM.unsafeRead MVector (PrimState f) a
mvec Int
readIdx
if a
curr a -> a -> Bool
forall a. Eq a => a -> a -> Bool
== a
prev
then Int -> Int -> a -> MVector (PrimState f) a -> Int -> f Int
compactFrom (Int
readIdx Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1) Int
writeIdx a
prev MVector (PrimState f) a
mvec Int
len
else do
MVector (PrimState f) a -> Int -> a -> f ()
forall (m :: * -> *) a.
(PrimMonad m, Unbox a) =>
MVector (PrimState m) a -> Int -> a -> m ()
UM.unsafeWrite MVector (PrimState f) a
mvec Int
writeIdx a
curr
Int -> Int -> a -> MVector (PrimState f) a -> Int -> f Int
compactFrom (Int
readIdx Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1) (Int
writeIdx Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1) a
curr MVector (PrimState f) a
mvec Int
len
encodePunycode :: Text -> Either PunycodeError Text
encodePunycode :: Text -> Either PunycodeError Text
encodePunycode Text
input
| Text -> Bool
T.null Text
input = Text -> Either PunycodeError Text
forall a b. b -> Either a b
Right Text
input
| Bool
otherwise = (forall s. ST s (Either PunycodeError Text))
-> Either PunycodeError Text
forall a. (forall s. ST s a) -> a
runST ((forall s. ST s (Either PunycodeError Text))
-> Either PunycodeError Text)
-> (forall s. ST s (Either PunycodeError Text))
-> Either PunycodeError Text
forall a b. (a -> b) -> a -> b
$ do
let inputCPs :: Vector Int
inputCPs = Text -> Vector Int
codePointsVector Text
input
len :: Int
len = Vector Int -> Int
forall a. Unbox a => Vector a -> Int
U.length Vector Int
inputCPs
initialCap :: Int
initialCap = Int -> Int -> Int
forall a. Ord a => a -> a -> a
max (Int
len Int -> Int -> Int
forall a. Num a => a -> a -> a
* Int
4) Int
64
sortedNonBasic :: Vector Int
sortedNonBasic = Vector Int -> Vector Int
extractSortedNonBasicCPs Vector Int
inputCPs
MArray s
marr0 <- Int -> ST s (MArray s)
forall s. Int -> ST s (MArray s)
A.new Int
initialCap
(MArray s
marr1, Int
idxBasics, Int
basicCount) <- Vector Int -> MArray s -> Int -> ST s (MArray s, Int, Int)
forall s.
Vector Int -> MArray s -> Int -> ST s (MArray s, Int, Int)
writeBasicsVec Vector Int
inputCPs MArray s
marr0 Int
0
let state0 :: PunycodeState
state0 = PunycodeState
initialState { h = basicCount }
(MArray s
marr2, Int
idxDelim) <-
if Int
basicCount Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> Int
0
then do
MArray s
marr' <- MArray s -> Int -> Int -> ST s (MArray s)
forall s. MArray s -> Int -> Int -> ST s (MArray s)
ensureCapacity MArray s
marr1 Int
idxBasics Int
1
MArray s -> Int -> Char -> ST s ()
forall s. MArray s -> Int -> Char -> ST s ()
writeChar MArray s
marr' Int
idxBasics Char
delimiter
(MArray s, Int) -> ST s (MArray s, Int)
forall a. a -> ST s a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (MArray s
marr', Int
idxBasics Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1)
else (MArray s, Int) -> ST s (MArray s, Int)
forall a. a -> ST s a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (MArray s
marr1, Int
idxBasics)
Either PunycodeError (MArray s, Int)
result <- MArray s
-> Int
-> Vector Int
-> Int
-> Int
-> PunycodeState
-> Vector Int
-> ST s (Either PunycodeError (MArray s, Int))
forall s.
MArray s
-> Int
-> Vector Int
-> Int
-> Int
-> PunycodeState
-> Vector Int
-> ST s (Either PunycodeError (MArray s, Int))
encodeLoopSorted MArray s
marr2 Int
idxDelim Vector Int
inputCPs Int
len Int
basicCount PunycodeState
state0 Vector Int
sortedNonBasic
case Either PunycodeError (MArray s, Int)
result of
Right (MArray s
marrFinal, Int
finalLen) -> do
Array
arr <- MArray s -> ST s Array
forall s. MArray s -> ST s Array
A.unsafeFreeze MArray s
marrFinal
Either PunycodeError Text -> ST s (Either PunycodeError Text)
forall a. a -> ST s a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Text -> Either PunycodeError Text
forall a b. b -> Either a b
Right (Array -> Int -> Int -> Text
Text Array
arr Int
0 Int
finalLen))
Left PunycodeError
err -> Either PunycodeError Text -> ST s (Either PunycodeError Text)
forall a. a -> ST s a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (PunycodeError -> Either PunycodeError Text
forall a b. a -> Either a b
Left PunycodeError
err)
encodeLoopSorted# :: forall (rep :: RuntimeRep) (r :: TYPE rep) s.
A.MArray s
-> Int
-> U.Vector Int
-> Int
-> Int
-> PunycodeState
-> U.Vector Int
-> Int
-> State# s
-> (State# s -> PunycodeError -> r)
-> (State# s -> A.MArray s -> Int -> r)
-> r
encodeLoopSorted# :: forall r s.
MArray s
-> Int
-> Vector Int
-> Int
-> Int
-> PunycodeState
-> Vector Int
-> Int
-> State# s
-> (State# s -> PunycodeError -> r)
-> (State# s -> MArray s -> Int -> r)
-> r
encodeLoopSorted# MArray s
marr Int
idx Vector Int
inputCPs Int
inputLen Int
basicCount PunycodeState
state Vector Int
sortedCPs Int
sortedIdx State# s
s State# s -> PunycodeError -> r
errK State# s -> MArray s -> Int -> r
succK
| PunycodeState -> Int
h PunycodeState
state Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
>= Int
inputLen = State# s -> MArray s -> Int -> r
succK State# s
s MArray s
marr Int
idx
| Int
sortedIdx Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
>= Vector Int -> Int
forall a. Unbox a => Vector a -> Int
U.length Vector Int
sortedCPs = State# s -> MArray s -> Int -> r
succK State# s
s MArray s
marr Int
idx
| Bool
otherwise =
let m :: Int
m = Vector Int -> Int -> Int
forall a. Unbox a => Vector a -> Int -> a
U.unsafeIndex Vector Int
sortedCPs Int
sortedIdx
!diff :: Int
diff = Int
m Int -> Int -> Int
forall a. Num a => a -> a -> a
- PunycodeState -> Int
n PunycodeState
state
!prod :: Int
prod = Int
diff Int -> Int -> Int
forall a. Num a => a -> a -> a
* (PunycodeState -> Int
h PunycodeState
state Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1)
!delta' :: Int
delta' = PunycodeState -> Int
delta PunycodeState
state Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
prod
in if Int
delta' Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
0
then State# s -> PunycodeError -> r
errK State# s
s PunycodeError
Overflow
else MArray s
-> Int
-> Vector Int
-> Int
-> Int
-> Int
-> Int
-> Int
-> Int
-> State# s
-> (State# s -> PunycodeError -> r)
-> (State# s -> MArray s -> Int -> Int -> Int -> Int -> r)
-> r
forall r s.
MArray s
-> Int
-> Vector Int
-> Int
-> Int
-> Int
-> Int
-> Int
-> Int
-> State# s
-> (State# s -> PunycodeError -> r)
-> (State# s -> MArray s -> Int -> Int -> Int -> Int -> r)
-> r
encodeCurrentPointVec# MArray s
marr Int
idx Vector Int
inputCPs Int
inputLen Int
m Int
delta' (PunycodeState -> Int
bias PunycodeState
state) (PunycodeState -> Int
h PunycodeState
state) Int
basicCount State# s
s
(\State# s
s' PunycodeError
err -> State# s -> PunycodeError -> r
errK State# s
s' PunycodeError
err)
(\State# s
s' MArray s
marr' Int
idx' Int
deltaAfter Int
bias' Int
handled' ->
let !deltaNext :: Int
deltaNext = Int
deltaAfter Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1
in if Int
deltaNext Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
0
then State# s -> PunycodeError -> r
errK State# s
s' PunycodeError
Overflow
else MArray s
-> Int
-> Vector Int
-> Int
-> Int
-> PunycodeState
-> Vector Int
-> Int
-> State# s
-> (State# s -> PunycodeError -> r)
-> (State# s -> MArray s -> Int -> r)
-> r
forall r s.
MArray s
-> Int
-> Vector Int
-> Int
-> Int
-> PunycodeState
-> Vector Int
-> Int
-> State# s
-> (State# s -> PunycodeError -> r)
-> (State# s -> MArray s -> Int -> r)
-> r
encodeLoopSorted# MArray s
marr' Int
idx' Vector Int
inputCPs Int
inputLen Int
basicCount PunycodeState
{ n :: Int
n = Int
m Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1
, delta :: Int
delta = Int
deltaNext
, bias :: Int
bias = Int
bias'
, h :: Int
h = Int
handled'
} Vector Int
sortedCPs (Int
sortedIdx Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1) State# s
s' State# s -> PunycodeError -> r
errK State# s -> MArray s -> Int -> r
succK)
encodeLoopSorted :: A.MArray s
-> Int
-> U.Vector Int
-> Int
-> Int
-> PunycodeState
-> U.Vector Int
-> ST s (Either PunycodeError (A.MArray s, Int))
encodeLoopSorted :: forall s.
MArray s
-> Int
-> Vector Int
-> Int
-> Int
-> PunycodeState
-> Vector Int
-> ST s (Either PunycodeError (MArray s, Int))
encodeLoopSorted MArray s
marr Int
idx Vector Int
inputCPs Int
inputLen Int
basicCount PunycodeState
state Vector Int
sortedCPs = STRep s (Either PunycodeError (MArray s, Int))
-> ST s (Either PunycodeError (MArray s, Int))
forall s a. STRep s a -> ST s a
ST (STRep s (Either PunycodeError (MArray s, Int))
-> ST s (Either PunycodeError (MArray s, Int)))
-> STRep s (Either PunycodeError (MArray s, Int))
-> ST s (Either PunycodeError (MArray s, Int))
forall a b. (a -> b) -> a -> b
$ \State# s
s ->
MArray s
-> Int
-> Vector Int
-> Int
-> Int
-> PunycodeState
-> Vector Int
-> Int
-> State# s
-> (State# s
-> PunycodeError
-> (# State# s, Either PunycodeError (MArray s, Int) #))
-> (State# s
-> MArray s
-> Int
-> (# State# s, Either PunycodeError (MArray s, Int) #))
-> (# State# s, Either PunycodeError (MArray s, Int) #)
forall r s.
MArray s
-> Int
-> Vector Int
-> Int
-> Int
-> PunycodeState
-> Vector Int
-> Int
-> State# s
-> (State# s -> PunycodeError -> r)
-> (State# s -> MArray s -> Int -> r)
-> r
encodeLoopSorted#
MArray s
marr Int
idx Vector Int
inputCPs Int
inputLen Int
basicCount PunycodeState
state Vector Int
sortedCPs Int
0 State# s
s
(\State# s
s' PunycodeError
err -> (# State# s
s', PunycodeError -> Either PunycodeError (MArray s, Int)
forall a b. a -> Either a b
Left PunycodeError
err #))
(\State# s
s' MArray s
marr' Int
idx' -> (# State# s
s', (MArray s, Int) -> Either PunycodeError (MArray s, Int)
forall a b. b -> Either a b
Right (MArray s
marr', Int
idx') #))
encodeCurrentPointVec# :: forall (rep :: RuntimeRep) (r :: TYPE rep) s.
A.MArray s
-> Int
-> U.Vector Int
-> Int
-> Int
-> Int
-> Int
-> Int
-> Int
-> State# s
-> (State# s -> PunycodeError -> r)
-> (State# s -> A.MArray s -> Int -> Int -> Int -> Int -> r)
-> r
encodeCurrentPointVec# :: forall r s.
MArray s
-> Int
-> Vector Int
-> Int
-> Int
-> Int
-> Int
-> Int
-> Int
-> State# s
-> (State# s -> PunycodeError -> r)
-> (State# s -> MArray s -> Int -> Int -> Int -> Int -> r)
-> r
encodeCurrentPointVec# MArray s
marr Int
idx Vector Int
cps Int
len Int
m Int
delta0 Int
bias0 Int
handled0 Int
basicCount State# s
s0 State# s -> PunycodeError -> r
errK State# s -> MArray s -> Int -> Int -> Int -> Int -> r
succK =
Int -> MArray s -> Int -> Int -> Int -> Int -> State# s -> r
go Int
0 MArray s
marr Int
idx Int
delta0 Int
bias0 Int
handled0 State# s
s0
where
go :: Int -> MArray s -> Int -> Int -> Int -> Int -> State# s -> r
go !Int
i !MArray s
arr !Int
outIdx !Int
deltaAcc !Int
biasAcc !Int
handledAcc State# s
s
| Int
i Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
>= Int
len = State# s -> MArray s -> Int -> Int -> Int -> Int -> r
succK State# s
s MArray s
arr Int
outIdx Int
deltaAcc Int
biasAcc Int
handledAcc
| Bool
otherwise =
let cp :: Int
cp = Vector Int -> Int -> Int
forall a. Unbox a => Vector a -> Int -> a
U.unsafeIndex Vector Int
cps Int
i
in if Int
cp Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
m
then
if Int
deltaAcc Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
forall a. Bounded a => a
maxBound
then State# s -> PunycodeError -> r
errK State# s
s PunycodeError
Overflow
else Int -> MArray s -> Int -> Int -> Int -> Int -> State# s -> r
go (Int
i Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1) MArray s
arr Int
outIdx (Int
deltaAcc Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1) Int
biasAcc Int
handledAcc State# s
s
else if Int
cp Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
m
then
let firstTime :: Bool
firstTime = Int
handledAcc Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
basicCount
in MArray s
-> Int
-> Int
-> Int
-> Int
-> Bool
-> State# s
-> (State# s -> PunycodeError -> r)
-> (State# s -> MArray s -> Int -> Int -> r)
-> r
forall r s.
MArray s
-> Int
-> Int
-> Int
-> Int
-> Bool
-> State# s
-> (State# s -> PunycodeError -> r)
-> (State# s -> MArray s -> Int -> Int -> r)
-> r
encodeVarIntST# MArray s
arr Int
outIdx Int
deltaAcc Int
biasAcc (Int
handledAcc Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1) Bool
firstTime State# s
s
(\State# s
s' PunycodeError
err -> State# s -> PunycodeError -> r
errK State# s
s' PunycodeError
err)
(\State# s
s' MArray s
marr' Int
outIdx' Int
newBias ->
Int -> MArray s -> Int -> Int -> Int -> Int -> State# s -> r
go (Int
i Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1) MArray s
marr' Int
outIdx' Int
0 Int
newBias (Int
handledAcc Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1) State# s
s')
else Int -> MArray s -> Int -> Int -> Int -> Int -> State# s -> r
go (Int
i Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1) MArray s
arr Int
outIdx Int
deltaAcc Int
biasAcc Int
handledAcc State# s
s
writeBasicsVec :: U.Vector Int -> A.MArray s -> Int -> ST s (A.MArray s, Int, Int)
writeBasicsVec :: forall s.
Vector Int -> MArray s -> Int -> ST s (MArray s, Int, Int)
writeBasicsVec Vector Int
cps MArray s
marr0 Int
idx0 = Int -> MArray s -> Int -> Int -> ST s (MArray s, Int, Int)
forall {t} {s}.
Num t =>
Int -> MArray s -> Int -> t -> ST s (MArray s, Int, t)
go Int
0 MArray s
marr0 Int
idx0 Int
0
where
len :: Int
len = Vector Int -> Int
forall a. Unbox a => Vector a -> Int
U.length Vector Int
cps
go :: Int -> MArray s -> Int -> t -> ST s (MArray s, Int, t)
go !Int
i !MArray s
marr !Int
idx !t
count
| Int
i Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
>= Int
len = (MArray s, Int, t) -> ST s (MArray s, Int, t)
forall a. a -> ST s a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (MArray s
marr, Int
idx, t
count)
| Bool
otherwise =
let cp :: Int
cp = Vector Int -> Int -> Int
forall a. Unbox a => Vector a -> Int -> a
U.unsafeIndex Vector Int
cps Int
i
in if Int -> Bool
isBasicCP Int
cp
then do
MArray s
marr' <- MArray s -> Int -> Int -> ST s (MArray s)
forall s. MArray s -> Int -> Int -> ST s (MArray s)
ensureCapacity MArray s
marr Int
idx Int
1
MArray s -> Int -> Char -> ST s ()
forall s. MArray s -> Int -> Char -> ST s ()
writeChar MArray s
marr' Int
idx (Int -> Char
chr Int
cp)
Int -> MArray s -> Int -> t -> ST s (MArray s, Int, t)
go (Int
i Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1) MArray s
marr' (Int
idx Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1) (t
count t -> t -> t
forall a. Num a => a -> a -> a
+ t
1)
else Int -> MArray s -> Int -> t -> ST s (MArray s, Int, t)
go (Int
i Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1) MArray s
marr Int
idx t
count
codePointsVector :: Text -> U.Vector Int
codePointsVector :: Text -> Vector Int
codePointsVector (Text Array
arr Int
off Int
len) = (forall s. ST s (Vector Int)) -> Vector Int
forall a. (forall s. ST s a) -> a
runST ((forall s. ST s (Vector Int)) -> Vector Int)
-> (forall s. ST s (Vector Int)) -> Vector Int
forall a b. (a -> b) -> a -> b
$ do
let endIdx :: Int
endIdx = Int
off Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
len
count :: Int
count = Int -> Int -> Int -> Int
countLoop Int
off Int
endIdx Int
0
MVector s Int
marr <- Int -> ST s (MVector (PrimState (ST s)) Int)
forall (m :: * -> *) a.
(PrimMonad m, Unbox a) =>
Int -> m (MVector (PrimState m) a)
UM.new Int
count
MVector s Int -> Int -> Int -> Int -> ST s ()
forall s. MVector s Int -> Int -> Int -> Int -> ST s ()
fillLoop MVector s Int
marr Int
off Int
endIdx Int
0
MVector (PrimState (ST s)) Int -> ST s (Vector Int)
forall a (m :: * -> *).
(Unbox a, PrimMonad m) =>
MVector (PrimState m) a -> m (Vector a)
U.unsafeFreeze MVector s Int
MVector (PrimState (ST s)) Int
marr
where
countLoop :: Int -> Int -> Int -> Int
countLoop :: Int -> Int -> Int -> Int
countLoop Int
idx Int
endIdx Int
acc
| Int
idx Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
>= Int
endIdx = Int
acc
| Bool
otherwise =
case Array -> Int -> (# Int, Int #)
nextCodePoint Array
arr Int
idx of
(# Int
_, Int
stepBytes #) -> Int -> Int -> Int -> Int
countLoop (Int
idx Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
stepBytes) Int
endIdx (Int
acc Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1)
fillLoop :: UM.MVector s Int -> Int -> Int -> Int -> ST s ()
fillLoop :: forall s. MVector s Int -> Int -> Int -> Int -> ST s ()
fillLoop MVector s Int
marr Int
idx Int
endIdx Int
i
| Int
idx Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
>= Int
endIdx = () -> ST s ()
forall a. a -> ST s a
forall (f :: * -> *) a. Applicative f => a -> f a
pure ()
| Bool
otherwise =
case Array -> Int -> (# Int, Int #)
nextCodePoint Array
arr Int
idx of
(# Int
cp, Int
deltaBytes #) -> do
MVector (PrimState (ST s)) Int -> Int -> Int -> ST s ()
forall (m :: * -> *) a.
(PrimMonad m, Unbox a) =>
MVector (PrimState m) a -> Int -> a -> m ()
UM.unsafeWrite MVector s Int
MVector (PrimState (ST s)) Int
marr Int
i Int
cp
MVector s Int -> Int -> Int -> Int -> ST s ()
forall s. MVector s Int -> Int -> Int -> Int -> ST s ()
fillLoop MVector s Int
marr (Int
idx Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
deltaBytes) Int
endIdx (Int
i Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1)
nextCodePoint :: A.Array -> Int -> (# Int, Int #)
nextCodePoint :: Array -> Int -> (# Int, Int #)
nextCodePoint Array
arr Int
idx =
let b1 :: Int
b1 = Word8 -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Array -> Int -> Word8
A.unsafeIndex Array
arr Int
idx) :: Int
in if Int
b1 Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
0x80
then (# Int
b1, Int
1 #)
else if Int
b1 Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
0xE0
then
let b2 :: Int
b2 = Int -> Int
cont (Int
idx Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1)
cp :: Int
cp = ((Int
b1 Int -> Int -> Int
forall a. Bits a => a -> a -> a
.&. Int
0x1F) Int -> Int -> Int
forall a. Bits a => a -> Int -> a
`shiftL` Int
6) Int -> Int -> Int
forall a. Bits a => a -> a -> a
.|. (Int
b2 Int -> Int -> Int
forall a. Bits a => a -> a -> a
.&. Int
0x3F)
in (# Int
cp, Int
2 #)
else if Int
b1 Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
0xF0
then
let b2 :: Int
b2 = Int -> Int
cont (Int
idx Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1)
b3 :: Int
b3 = Int -> Int
cont (Int
idx Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
2)
cp :: Int
cp = ((Int
b1 Int -> Int -> Int
forall a. Bits a => a -> a -> a
.&. Int
0x0F) Int -> Int -> Int
forall a. Bits a => a -> Int -> a
`shiftL` Int
12)
Int -> Int -> Int
forall a. Bits a => a -> a -> a
.|. ((Int
b2 Int -> Int -> Int
forall a. Bits a => a -> a -> a
.&. Int
0x3F) Int -> Int -> Int
forall a. Bits a => a -> Int -> a
`shiftL` Int
6)
Int -> Int -> Int
forall a. Bits a => a -> a -> a
.|. (Int
b3 Int -> Int -> Int
forall a. Bits a => a -> a -> a
.&. Int
0x3F)
in (# Int
cp, Int
3 #)
else
let b2 :: Int
b2 = Int -> Int
cont (Int
idx Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1)
b3 :: Int
b3 = Int -> Int
cont (Int
idx Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
2)
b4 :: Int
b4 = Int -> Int
cont (Int
idx Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
3)
cp :: Int
cp = ((Int
b1 Int -> Int -> Int
forall a. Bits a => a -> a -> a
.&. Int
0x07) Int -> Int -> Int
forall a. Bits a => a -> Int -> a
`shiftL` Int
18)
Int -> Int -> Int
forall a. Bits a => a -> a -> a
.|. ((Int
b2 Int -> Int -> Int
forall a. Bits a => a -> a -> a
.&. Int
0x3F) Int -> Int -> Int
forall a. Bits a => a -> Int -> a
`shiftL` Int
12)
Int -> Int -> Int
forall a. Bits a => a -> a -> a
.|. ((Int
b3 Int -> Int -> Int
forall a. Bits a => a -> a -> a
.&. Int
0x3F) Int -> Int -> Int
forall a. Bits a => a -> Int -> a
`shiftL` Int
6)
Int -> Int -> Int
forall a. Bits a => a -> a -> a
.|. (Int
b4 Int -> Int -> Int
forall a. Bits a => a -> a -> a
.&. Int
0x3F)
in (# Int
cp, Int
4 #)
where
cont :: Int -> Int
cont Int
i = Word8 -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Array -> Int -> Word8
A.unsafeIndex Array
arr Int
i) :: Int
encodeVarIntST# :: forall (rep :: RuntimeRep) (r :: TYPE rep) s.
A.MArray s -> Int -> Int -> Int -> Int -> Bool -> State# s
-> (State# s -> PunycodeError -> r)
-> (State# s -> A.MArray s -> Int -> Int -> r)
-> r
encodeVarIntST# :: forall r s.
MArray s
-> Int
-> Int
-> Int
-> Int
-> Bool
-> State# s
-> (State# s -> PunycodeError -> r)
-> (State# s -> MArray s -> Int -> Int -> r)
-> r
encodeVarIntST# MArray s
marr Int
idx Int
q Int
biasValue Int
numPoints Bool
firstTime State# s
s State# s -> PunycodeError -> r
errK State# s -> MArray s -> Int -> Int -> r
succK =
let newBias :: Int
newBias = Int -> Int -> Bool -> Int
adapt Int
q Int
numPoints Bool
firstTime
in MArray s
-> Int
-> Int
-> Int
-> Int
-> State# s
-> (State# s -> PunycodeError -> r)
-> (State# s -> MArray s -> Int -> r)
-> r
forall r s.
MArray s
-> Int
-> Int
-> Int
-> Int
-> State# s
-> (State# s -> PunycodeError -> r)
-> (State# s -> MArray s -> Int -> r)
-> r
writeVarIntLoop# MArray s
marr Int
idx Int
q Int
base Int
biasValue State# s
s
(\State# s
s' PunycodeError
err -> State# s -> PunycodeError -> r
errK State# s
s' PunycodeError
err)
(\State# s
s' MArray s
marr' Int
idx' -> State# s -> MArray s -> Int -> Int -> r
succK State# s
s' MArray s
marr' Int
idx' Int
newBias)
writeVarIntLoop# :: forall (rep :: RuntimeRep) (r :: TYPE rep) s.
A.MArray s -> Int -> Int -> Int -> Int -> State# s
-> (State# s -> PunycodeError -> r)
-> (State# s -> A.MArray s -> Int -> r)
-> r
writeVarIntLoop# :: forall r s.
MArray s
-> Int
-> Int
-> Int
-> Int
-> State# s
-> (State# s -> PunycodeError -> r)
-> (State# s -> MArray s -> Int -> r)
-> r
writeVarIntLoop# MArray s
marr Int
idx Int
q Int
k Int
biasValue State# s
s State# s -> PunycodeError -> r
_errK State# s -> MArray s -> Int -> r
succK =
let t :: Int
t = if Int
k Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
<= Int
biasValue then Int
tmin
else if Int
k Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
>= Int
biasValue Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
tmax then Int
tmax
else Int
k Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
biasValue
in if Int
q Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
t
then case MArray s -> Int -> Int -> State# s -> (# State# s, MArray s #)
forall s.
MArray s -> Int -> Int -> State# s -> (# State# s, MArray s #)
ensureCapacityST# MArray s
marr Int
idx Int
1 State# s
s of
(# State# s
s', MArray s
marr' #) ->
case MArray s -> Int -> Char -> State# s -> State# s
forall s. MArray s -> Int -> Char -> State# s -> State# s
writeCharST# MArray s
marr' Int
idx (Int -> Char
encodeDigit Int
q) State# s
s' of
State# s
s'' -> State# s -> MArray s -> Int -> r
succK State# s
s'' MArray s
marr' (Int
idx Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1)
else
let digit :: Int
digit = Int
t Int -> Int -> Int
forall a. Num a => a -> a -> a
+ ((Int
q Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
t) Int -> Int -> Int
forall a. Integral a => a -> a -> a
`mod` (Int
base Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
t))
q' :: Int
q' = (Int
q Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
t) Int -> Int -> Int
forall a. Integral a => a -> a -> a
`div` (Int
base Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
t)
in case MArray s -> Int -> Int -> State# s -> (# State# s, MArray s #)
forall s.
MArray s -> Int -> Int -> State# s -> (# State# s, MArray s #)
ensureCapacityST# MArray s
marr Int
idx Int
1 State# s
s of
(# State# s
s', MArray s
marr' #) ->
case MArray s -> Int -> Char -> State# s -> State# s
forall s. MArray s -> Int -> Char -> State# s -> State# s
writeCharST# MArray s
marr' Int
idx (Int -> Char
encodeDigit Int
digit) State# s
s' of
State# s
s'' -> MArray s
-> Int
-> Int
-> Int
-> Int
-> State# s
-> (State# s -> PunycodeError -> r)
-> (State# s -> MArray s -> Int -> r)
-> r
forall r s.
MArray s
-> Int
-> Int
-> Int
-> Int
-> State# s
-> (State# s -> PunycodeError -> r)
-> (State# s -> MArray s -> Int -> r)
-> r
writeVarIntLoop# MArray s
marr' (Int
idx Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1) Int
q' (Int
k Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
base) Int
biasValue State# s
s'' State# s -> PunycodeError -> r
_errK State# s -> MArray s -> Int -> r
succK
encodeDigit :: Int -> Char
encodeDigit :: Int -> Char
encodeDigit Int
d
| Int
d Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
26 = Int -> Char
chr (Char -> Int
ord Char
'a' Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
d)
| Bool
otherwise = Int -> Char
chr (Char -> Int
ord Char
'0' Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
d Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
26)
writeChar :: A.MArray s -> Int -> Char -> ST s ()
writeChar :: forall s. MArray s -> Int -> Char -> ST s ()
writeChar MArray s
marr Int
idx Char
c = MArray s -> Int -> Word8 -> ST s ()
forall s. MArray s -> Int -> Word8 -> ST s ()
A.unsafeWrite MArray s
marr Int
idx (Int -> Word8
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Int -> Word8) -> Int -> Word8
forall a b. (a -> b) -> a -> b
$ Char -> Int
ord Char
c)
writeCharST# :: A.MArray s -> Int -> Char -> State# s -> State# s
writeCharST# :: forall s. MArray s -> Int -> Char -> State# s -> State# s
writeCharST# MArray s
marr Int
idx Char
c State# s
s =
case MArray s -> Int -> Word8 -> ST s ()
forall s. MArray s -> Int -> Word8 -> ST s ()
A.unsafeWrite MArray s
marr Int
idx (Int -> Word8
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Int -> Word8) -> Int -> Word8
forall a b. (a -> b) -> a -> b
$ Char -> Int
ord Char
c) of
ST STRep s ()
f -> case STRep s ()
f State# s
s of (# State# s
s', ()
_ #) -> State# s
s'
ensureCapacity :: A.MArray s -> Int -> Int -> ST s (A.MArray s)
ensureCapacity :: forall s. MArray s -> Int -> Int -> ST s (MArray s)
ensureCapacity MArray s
marr Int
idx Int
needed = do
Int
size <- MArray s -> ST s Int
forall s. MArray s -> ST s Int
A.getSizeofMArray MArray s
marr
if Int
idx Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
needed Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
<= Int
size
then MArray s -> ST s (MArray s)
forall a. a -> ST s a
forall (m :: * -> *) a. Monad m => a -> m a
return MArray s
marr
else do
let newSize :: Int
newSize = Int -> Int -> Int
forall a. Ord a => a -> a -> a
max (Int
size Int -> Int -> Int
forall a. Num a => a -> a -> a
* Int
2) (Int
idx Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
needed)
MArray s -> Int -> ST s (MArray s)
forall s. MArray s -> Int -> ST s (MArray s)
A.resizeM MArray s
marr Int
newSize
ensureCapacityST# :: A.MArray s -> Int -> Int -> State# s -> (# State# s, A.MArray s #)
ensureCapacityST# :: forall s.
MArray s -> Int -> Int -> State# s -> (# State# s, MArray s #)
ensureCapacityST# MArray s
marr Int
idx Int
needed State# s
s =
case MArray s -> ST s Int
forall s. MArray s -> ST s Int
A.getSizeofMArray MArray s
marr of
ST STRep s Int
f -> case STRep s Int
f State# s
s of
(# State# s
s', Int
size #) ->
if Int
idx Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
needed Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
<= Int
size
then (# State# s
s', MArray s
marr #)
else
let newSize :: Int
newSize = Int -> Int -> Int
forall a. Ord a => a -> a -> a
max (Int
size Int -> Int -> Int
forall a. Num a => a -> a -> a
* Int
2) (Int
idx Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
needed)
in case MArray s -> Int -> ST s (MArray s)
forall s. MArray s -> Int -> ST s (MArray s)
A.resizeM MArray s
marr Int
newSize of
ST STRep s (MArray s)
g -> STRep s (MArray s)
g State# s
s'
decodePunycode :: Text -> Either PunycodeError Text
decodePunycode :: Text -> Either PunycodeError Text
decodePunycode Text
input
| Text -> Bool
T.null Text
input = Text -> Either PunycodeError Text
forall a b. b -> Either a b
Right Text
input
| Bool
otherwise =
case (Char -> Bool) -> Text -> Maybe Int
T.findIndex (Char -> Char -> Bool
forall a. Eq a => a -> a -> Bool
== Char
delimiter) (Text -> Text
T.reverse Text
input) of
Maybe Int
Nothing ->
if (Char -> Bool) -> Text -> Bool
T.all Char -> Bool
isBasic Text
input
then case Text -> Text -> Either PunycodeError Text
decodeWithParts Text
T.empty Text
input of
Right Text
txt -> Text -> Either PunycodeError Text
forall a b. b -> Either a b
Right Text
txt
Left PunycodeError
_ -> Text -> Either PunycodeError Text
forall a b. b -> Either a b
Right Text
input
else PunycodeError -> Either PunycodeError Text
forall a b. a -> Either a b
Left (Text -> PunycodeError
InvalidPunycode Text
"Non-basic chars without delimiter")
Just Int
revPos ->
let delimPos :: Int
delimPos = Text -> Int
T.length Text
input Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
revPos Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1
basicPart :: Text
basicPart = Int -> Text -> Text
T.take Int
delimPos Text
input
encodedPart :: Text
encodedPart = Int -> Text -> Text
T.drop (Int
delimPos Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1) Text
input
in if Bool -> Bool
not ((Char -> Bool) -> Text -> Bool
T.all Char -> Bool
isBasic Text
basicPart)
then PunycodeError -> Either PunycodeError Text
forall a b. a -> Either a b
Left (Text -> PunycodeError
InvalidPunycode Text
"Non-basic in basic part")
else if Text -> Bool
T.null Text
encodedPart
then Text -> Either PunycodeError Text
forall a b. b -> Either a b
Right Text
basicPart
else Text -> Text -> Either PunycodeError Text
decodeWithParts Text
basicPart Text
encodedPart
decodeWithParts :: Text -> Text -> Either PunycodeError Text
decodeWithParts :: Text -> Text -> Either PunycodeError Text
decodeWithParts Text
basicPart Text
encodedPart = (forall s. ST s (Either PunycodeError Text))
-> Either PunycodeError Text
forall a. (forall s. ST s a) -> a
runST ((forall s. ST s (Either PunycodeError Text))
-> Either PunycodeError Text)
-> (forall s. ST s (Either PunycodeError Text))
-> Either PunycodeError Text
forall a b. (a -> b) -> a -> b
$ do
let basicCodePoints :: Vector Int
basicCodePoints = Text -> Vector Int
codePointsVector Text
basicPart
basicLen :: Int
basicLen = Vector Int -> Int
forall a. Unbox a => Vector a -> Int
U.length Vector Int
basicCodePoints
encodedCodePoints :: Vector Int
encodedCodePoints = Text -> Vector Int
codePointsVector Text
encodedPart
encodedLen :: Int
encodedLen = Vector Int -> Int
forall a. Unbox a => Vector a -> Int
U.length Vector Int
encodedCodePoints
initialCapacity :: Int
initialCapacity = Int -> Int -> Int
forall a. Ord a => a -> a -> a
max Int
32 (Int
basicLen Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
encodedLen)
MVector s Int
vec0 <- Vector Int -> ST s (MVector (PrimState (ST s)) Int)
forall a (m :: * -> *).
(Unbox a, PrimMonad m) =>
Vector a -> m (MVector (PrimState m) a)
U.thaw (Vector Int -> ST s (MVector (PrimState (ST s)) Int))
-> Vector Int -> ST s (MVector (PrimState (ST s)) Int)
forall a b. (a -> b) -> a -> b
$ Int -> Vector Int -> Vector Int
forall a. Unbox a => Int -> Vector a -> Vector a
U.take Int
initialCapacity Vector Int
basicCodePoints
Either PunycodeError (MVector s Int, Int)
result <- MVector s Int
-> Int
-> Vector Int
-> Int
-> Int
-> Int
-> Int
-> Int
-> ST s (Either PunycodeError (MVector s Int, Int))
forall s.
MVector s Int
-> Int
-> Vector Int
-> Int
-> Int
-> Int
-> Int
-> Int
-> ST s (Either PunycodeError (MVector s Int, Int))
decodeLoopVector MVector s Int
vec0 Int
basicLen Vector Int
encodedCodePoints Int
encodedLen Int
0 Int
initialN Int
0 Int
initialBias
case Either PunycodeError (MVector s Int, Int)
result of
Right (MVector s Int
vecFinal, Int
finalLen) -> do
Text
text <- MVector s Int -> Int -> ST s Text
forall s. MVector s Int -> Int -> ST s Text
buildTextFromVector MVector s Int
vecFinal Int
finalLen
Either PunycodeError Text -> ST s (Either PunycodeError Text)
forall a. a -> ST s a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Text -> Either PunycodeError Text
forall a b. b -> Either a b
Right Text
text)
Left PunycodeError
err -> Either PunycodeError Text -> ST s (Either PunycodeError Text)
forall a. a -> ST s a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (PunycodeError -> Either PunycodeError Text
forall a b. a -> Either a b
Left PunycodeError
err)
decodeLoopVector# :: forall (rep :: RuntimeRep) (r :: TYPE rep) s.
UM.MVector s Int
-> Int
-> U.Vector Int
-> Int
-> Int
-> Int
-> Int
-> Int
-> State# s
-> (State# s -> PunycodeError -> r)
-> (State# s -> UM.MVector s Int -> Int -> r)
-> r
decodeLoopVector# :: forall r s.
MVector s Int
-> Int
-> Vector Int
-> Int
-> Int
-> Int
-> Int
-> Int
-> State# s
-> (State# s -> PunycodeError -> r)
-> (State# s -> MVector s Int -> Int -> r)
-> r
decodeLoopVector# MVector s Int
vec Int
outLen Vector Int
encoded Int
encodedLen Int
encIdx Int
stateN Int
i Int
stateBias State# s
s State# s -> PunycodeError -> r
errK State# s -> MVector s Int -> Int -> r
succK
| Int
encIdx Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
>= Int
encodedLen = State# s -> MVector s Int -> Int -> r
succK State# s
s MVector s Int
vec Int
outLen
| Bool
otherwise =
case Vector Int -> Int -> Int -> Int -> Either PunycodeError (Int, Int)
decodeVarIntVec Vector Int
encoded Int
encodedLen Int
encIdx Int
stateBias of
Left PunycodeError
err -> State# s -> PunycodeError -> r
errK State# s
s PunycodeError
err
Right (Int
deltaVal, Int
nextIdx) ->
let !i' :: Int
i' = Int
i Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
deltaVal
!numPoints :: Int
numPoints = Int
outLen Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1
(!Int
wraps, !Int
insertPos) = Int
i' Int -> Int -> (Int, Int)
forall a. Integral a => a -> a -> (a, a)
`quotRem` Int
numPoints
!n' :: Int
n' = Int
stateN Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
wraps
in if Int
n' Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> Int
0x10FFFF
then State# s -> PunycodeError -> r
errK State# s
s PunycodeError
Overflow
else case MVector s Int
-> Int
-> Int
-> Int
-> State# s
-> (# State# s, MVector s Int, Int #)
forall s.
MVector s Int
-> Int
-> Int
-> Int
-> State# s
-> (# State# s, MVector s Int, Int #)
insertCodePointST# MVector s Int
vec Int
outLen Int
insertPos Int
n' State# s
s of
(# State# s
s', MVector s Int
vec', Int
outLen' #) ->
let !bias' :: Int
bias' = Int -> Int -> Bool -> Int
adapt Int
deltaVal Int
numPoints (Int
i Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
0)
!i'' :: Int
i'' = Int
insertPos Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1
in MVector s Int
-> Int
-> Vector Int
-> Int
-> Int
-> Int
-> Int
-> Int
-> State# s
-> (State# s -> PunycodeError -> r)
-> (State# s -> MVector s Int -> Int -> r)
-> r
forall r s.
MVector s Int
-> Int
-> Vector Int
-> Int
-> Int
-> Int
-> Int
-> Int
-> State# s
-> (State# s -> PunycodeError -> r)
-> (State# s -> MVector s Int -> Int -> r)
-> r
decodeLoopVector# MVector s Int
vec' Int
outLen' Vector Int
encoded Int
encodedLen Int
nextIdx Int
n' Int
i'' Int
bias' State# s
s' State# s -> PunycodeError -> r
errK State# s -> MVector s Int -> Int -> r
succK
decodeLoopVector :: UM.MVector s Int
-> Int
-> U.Vector Int
-> Int
-> Int
-> Int
-> Int
-> Int
-> ST s (Either PunycodeError (UM.MVector s Int, Int))
decodeLoopVector :: forall s.
MVector s Int
-> Int
-> Vector Int
-> Int
-> Int
-> Int
-> Int
-> Int
-> ST s (Either PunycodeError (MVector s Int, Int))
decodeLoopVector MVector s Int
vec Int
outLen Vector Int
encoded Int
encodedLen Int
encIdx Int
stateN Int
i Int
stateBias = STRep s (Either PunycodeError (MVector s Int, Int))
-> ST s (Either PunycodeError (MVector s Int, Int))
forall s a. STRep s a -> ST s a
ST (STRep s (Either PunycodeError (MVector s Int, Int))
-> ST s (Either PunycodeError (MVector s Int, Int)))
-> STRep s (Either PunycodeError (MVector s Int, Int))
-> ST s (Either PunycodeError (MVector s Int, Int))
forall a b. (a -> b) -> a -> b
$ \State# s
s ->
MVector s Int
-> Int
-> Vector Int
-> Int
-> Int
-> Int
-> Int
-> Int
-> State# s
-> (State# s
-> PunycodeError
-> (# State# s, Either PunycodeError (MVector s Int, Int) #))
-> (State# s
-> MVector s Int
-> Int
-> (# State# s, Either PunycodeError (MVector s Int, Int) #))
-> (# State# s, Either PunycodeError (MVector s Int, Int) #)
forall r s.
MVector s Int
-> Int
-> Vector Int
-> Int
-> Int
-> Int
-> Int
-> Int
-> State# s
-> (State# s -> PunycodeError -> r)
-> (State# s -> MVector s Int -> Int -> r)
-> r
decodeLoopVector# MVector s Int
vec Int
outLen Vector Int
encoded Int
encodedLen Int
encIdx Int
stateN Int
i Int
stateBias State# s
s
(\State# s
s' PunycodeError
err -> (# State# s
s', PunycodeError -> Either PunycodeError (MVector s Int, Int)
forall a b. a -> Either a b
Left PunycodeError
err #))
(\State# s
s' MVector s Int
vec' Int
len' -> (# State# s
s', (MVector s Int, Int) -> Either PunycodeError (MVector s Int, Int)
forall a b. b -> Either a b
Right (MVector s Int
vec', Int
len') #))
insertCodePointST# :: UM.MVector s Int -> Int -> Int -> Int -> State# s -> (# State# s, UM.MVector s Int, Int #)
insertCodePointST# :: forall s.
MVector s Int
-> Int
-> Int
-> Int
-> State# s
-> (# State# s, MVector s Int, Int #)
insertCodePointST# MVector s Int
vec Int
len Int
pos Int
cp State# s
s =
case MVector s Int
-> Int -> Int -> State# s -> (# State# s, MVector s Int #)
forall s.
MVector s Int
-> Int -> Int -> State# s -> (# State# s, MVector s Int #)
ensureCapacityVectorST# MVector s Int
vec Int
len Int
1 State# s
s of
(# State# s
s', MVector s Int
vec' #) ->
let s'' :: State# s
s'' = if Int
pos Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
len
then case MVector (PrimState (ST s)) Int
-> MVector (PrimState (ST s)) Int -> ST s ()
forall (m :: * -> *) a.
(PrimMonad m, Unbox a) =>
MVector (PrimState m) a -> MVector (PrimState m) a -> m ()
UM.move (Int -> Int -> MVector s Int -> MVector s Int
forall a s. Unbox a => Int -> Int -> MVector s a -> MVector s a
UM.slice (Int
pos Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1) (Int
len Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
pos) MVector s Int
vec')
(Int -> Int -> MVector s Int -> MVector s Int
forall a s. Unbox a => Int -> Int -> MVector s a -> MVector s a
UM.slice Int
pos (Int
len Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
pos) MVector s Int
vec') of
ST STRep s ()
f -> case STRep s ()
f State# s
s' of (# State# s
s1, ()
_ #) -> State# s
s1
else State# s
s'
in case MVector (PrimState (ST s)) Int -> Int -> Int -> ST s ()
forall (m :: * -> *) a.
(PrimMonad m, Unbox a) =>
MVector (PrimState m) a -> Int -> a -> m ()
UM.write MVector s Int
MVector (PrimState (ST s)) Int
vec' Int
pos Int
cp of
ST STRep s ()
g -> case STRep s ()
g State# s
s'' of
(# State# s
s''', ()
_ #) -> (# State# s
s''', MVector s Int
vec', Int
len Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1 #)
ensureCapacityVectorST# :: UM.MVector s Int -> Int -> Int -> State# s -> (# State# s, UM.MVector s Int #)
ensureCapacityVectorST# :: forall s.
MVector s Int
-> Int -> Int -> State# s -> (# State# s, MVector s Int #)
ensureCapacityVectorST# MVector s Int
vec Int
len Int
needed State# s
s =
let cap :: Int
cap = MVector s Int -> Int
forall a s. Unbox a => MVector s a -> Int
UM.length MVector s Int
vec
in if Int
len Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
needed Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
<= Int
cap
then (# State# s
s, MVector s Int
vec #)
else
let extra :: Int
extra = Int -> Int -> Int
forall a. Ord a => a -> a -> a
max Int
16 (Int
len Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
needed Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
cap)
in case MVector (PrimState (ST s)) Int
-> Int -> ST s (MVector (PrimState (ST s)) Int)
forall (m :: * -> *) a.
(PrimMonad m, Unbox a) =>
MVector (PrimState m) a -> Int -> m (MVector (PrimState m) a)
UM.grow MVector s Int
MVector (PrimState (ST s)) Int
vec Int
extra of
ST STRep s (MVector s Int)
f -> STRep s (MVector s Int)
f State# s
s
buildTextFromVector :: UM.MVector s Int -> Int -> ST s Text
buildTextFromVector :: forall s. MVector s Int -> Int -> ST s Text
buildTextFromVector MVector s Int
vec Int
len = do
Int
totalBytes <- Int -> Int -> ST s Int
forall {f :: * -> *}.
(PrimState f ~ s, PrimMonad f) =>
Int -> Int -> f Int
total Int
0 Int
0
MArray s
marr0 <- Int -> ST s (MArray s)
forall s. Int -> ST s (MArray s)
A.new (Int -> Int -> Int
forall a. Ord a => a -> a -> a
max Int
32 Int
totalBytes)
(MArray s
marrFinal, Int
finalIdx) <- MArray s -> Int -> Int -> ST s (MArray s, Int)
fill MArray s
marr0 Int
0 Int
0
Array
arr <- MArray s -> ST s Array
forall s. MArray s -> ST s Array
A.unsafeFreeze MArray s
marrFinal
Text -> ST s Text
forall a. a -> ST s a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Array -> Int -> Int -> Text
Text Array
arr Int
0 Int
finalIdx)
where
total :: Int -> Int -> f Int
total Int
i Int
acc
| Int
i Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
>= Int
len = Int -> f Int
forall a. a -> f a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Int
acc
| Bool
otherwise = do
Int
cp <- MVector (PrimState f) Int -> Int -> f Int
forall (m :: * -> *) a.
(PrimMonad m, Unbox a) =>
MVector (PrimState m) a -> Int -> m a
UM.read MVector s Int
MVector (PrimState f) Int
vec Int
i
Int -> Int -> f Int
total (Int
i Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1) (Int
acc Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int -> Int
utf8Length Int
cp)
fill :: MArray s -> Int -> Int -> ST s (MArray s, Int)
fill MArray s
marr Int
idx Int
i
| Int
i Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
>= Int
len = (MArray s, Int) -> ST s (MArray s, Int)
forall a. a -> ST s a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (MArray s
marr, Int
idx)
| Bool
otherwise = do
Int
cp <- MVector (PrimState (ST s)) Int -> Int -> ST s Int
forall (m :: * -> *) a.
(PrimMonad m, Unbox a) =>
MVector (PrimState m) a -> Int -> m a
UM.read MVector s Int
MVector (PrimState (ST s)) Int
vec Int
i
let needed :: Int
needed = Int -> Int
utf8Length Int
cp
MArray s
marr' <- MArray s -> Int -> Int -> ST s (MArray s)
forall s. MArray s -> Int -> Int -> ST s (MArray s)
ensureCapacity MArray s
marr Int
idx Int
needed
Int
idx' <- MArray s -> Int -> Int -> ST s Int
forall s. MArray s -> Int -> Int -> ST s Int
utf8Write MArray s
marr' Int
idx Int
cp
MArray s -> Int -> Int -> ST s (MArray s, Int)
fill MArray s
marr' Int
idx' (Int
i Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1)
utf8Length :: Int -> Int
utf8Length :: Int -> Int
utf8Length Int
cp
| Int
cp Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
<= Int
0x7F = Int
1
| Int
cp Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
<= Int
0x7FF = Int
2
| Int
cp Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
<= Int
0xFFFF = Int
3
| Bool
otherwise = Int
4
utf8Write :: A.MArray s -> Int -> Int -> ST s Int
utf8Write :: forall s. MArray s -> Int -> Int -> ST s Int
utf8Write MArray s
marr Int
idx Int
cp
| Int
cp Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
<= Int
0x7F = do
MArray s -> Int -> Word8 -> ST s ()
forall s. MArray s -> Int -> Word8 -> ST s ()
A.unsafeWrite MArray s
marr Int
idx (Int -> Word8
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
cp)
Int -> ST s Int
forall a. a -> ST s a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Int
idx Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1)
| Int
cp Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
<= Int
0x7FF = do
let b1 :: Int
b1 = Int
0xC0 Int -> Int -> Int
forall a. Bits a => a -> a -> a
.|. (Int
cp Int -> Int -> Int
forall a. Bits a => a -> Int -> a
`shiftR` Int
6)
b2 :: Int
b2 = Int
0x80 Int -> Int -> Int
forall a. Bits a => a -> a -> a
.|. (Int
cp Int -> Int -> Int
forall a. Bits a => a -> a -> a
.&. Int
0x3F)
MArray s -> Int -> Word8 -> ST s ()
forall s. MArray s -> Int -> Word8 -> ST s ()
A.unsafeWrite MArray s
marr Int
idx (Int -> Word8
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
b1)
MArray s -> Int -> Word8 -> ST s ()
forall s. MArray s -> Int -> Word8 -> ST s ()
A.unsafeWrite MArray s
marr (Int
idx Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1) (Int -> Word8
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
b2)
Int -> ST s Int
forall a. a -> ST s a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Int
idx Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
2)
| Int
cp Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
<= Int
0xFFFF = do
let b1 :: Int
b1 = Int
0xE0 Int -> Int -> Int
forall a. Bits a => a -> a -> a
.|. (Int
cp Int -> Int -> Int
forall a. Bits a => a -> Int -> a
`shiftR` Int
12)
b2 :: Int
b2 = Int
0x80 Int -> Int -> Int
forall a. Bits a => a -> a -> a
.|. ((Int
cp Int -> Int -> Int
forall a. Bits a => a -> Int -> a
`shiftR` Int
6) Int -> Int -> Int
forall a. Bits a => a -> a -> a
.&. Int
0x3F)
b3 :: Int
b3 = Int
0x80 Int -> Int -> Int
forall a. Bits a => a -> a -> a
.|. (Int
cp Int -> Int -> Int
forall a. Bits a => a -> a -> a
.&. Int
0x3F)
MArray s -> Int -> Word8 -> ST s ()
forall s. MArray s -> Int -> Word8 -> ST s ()
A.unsafeWrite MArray s
marr Int
idx (Int -> Word8
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
b1)
MArray s -> Int -> Word8 -> ST s ()
forall s. MArray s -> Int -> Word8 -> ST s ()
A.unsafeWrite MArray s
marr (Int
idx Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1) (Int -> Word8
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
b2)
MArray s -> Int -> Word8 -> ST s ()
forall s. MArray s -> Int -> Word8 -> ST s ()
A.unsafeWrite MArray s
marr (Int
idx Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
2) (Int -> Word8
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
b3)
Int -> ST s Int
forall a. a -> ST s a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Int
idx Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
3)
| Bool
otherwise = do
let b1 :: Int
b1 = Int
0xF0 Int -> Int -> Int
forall a. Bits a => a -> a -> a
.|. (Int
cp Int -> Int -> Int
forall a. Bits a => a -> Int -> a
`shiftR` Int
18)
b2 :: Int
b2 = Int
0x80 Int -> Int -> Int
forall a. Bits a => a -> a -> a
.|. ((Int
cp Int -> Int -> Int
forall a. Bits a => a -> Int -> a
`shiftR` Int
12) Int -> Int -> Int
forall a. Bits a => a -> a -> a
.&. Int
0x3F)
b3 :: Int
b3 = Int
0x80 Int -> Int -> Int
forall a. Bits a => a -> a -> a
.|. ((Int
cp Int -> Int -> Int
forall a. Bits a => a -> Int -> a
`shiftR` Int
6) Int -> Int -> Int
forall a. Bits a => a -> a -> a
.&. Int
0x3F)
b4 :: Int
b4 = Int
0x80 Int -> Int -> Int
forall a. Bits a => a -> a -> a
.|. (Int
cp Int -> Int -> Int
forall a. Bits a => a -> a -> a
.&. Int
0x3F)
MArray s -> Int -> Word8 -> ST s ()
forall s. MArray s -> Int -> Word8 -> ST s ()
A.unsafeWrite MArray s
marr Int
idx (Int -> Word8
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
b1)
MArray s -> Int -> Word8 -> ST s ()
forall s. MArray s -> Int -> Word8 -> ST s ()
A.unsafeWrite MArray s
marr (Int
idx Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1) (Int -> Word8
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
b2)
MArray s -> Int -> Word8 -> ST s ()
forall s. MArray s -> Int -> Word8 -> ST s ()
A.unsafeWrite MArray s
marr (Int
idx Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
2) (Int -> Word8
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
b3)
MArray s -> Int -> Word8 -> ST s ()
forall s. MArray s -> Int -> Word8 -> ST s ()
A.unsafeWrite MArray s
marr (Int
idx Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
3) (Int -> Word8
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
b4)
Int -> ST s Int
forall a. a -> ST s a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Int
idx Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
4)
decodeVarIntVec :: U.Vector Int -> Int -> Int -> Int -> Either PunycodeError (Int, Int)
decodeVarIntVec :: Vector Int -> Int -> Int -> Int -> Either PunycodeError (Int, Int)
decodeVarIntVec Vector Int
encoded Int
len Int
idx Int
biasValue = Int -> Int -> Int -> Int -> Either PunycodeError (Int, Int)
go Int
idx Int
0 Int
1 Int
base
where
go :: Int -> Int -> Int -> Int -> Either PunycodeError (Int, Int)
go !Int
pos !Int
value !Int
weight !Int
k
| Int
pos Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
>= Int
len = PunycodeError -> Either PunycodeError (Int, Int)
forall a b. a -> Either a b
Left (Text -> PunycodeError
InvalidPunycode Text
"Incomplete variable-length integer")
| Bool
otherwise =
let cp :: Int
cp = Vector Int -> Int -> Int
forall a. Unbox a => Vector a -> Int -> a
U.unsafeIndex Vector Int
encoded Int
pos
d :: Int
d = Int -> Int
decodeDigitInt Int
cp
in if Int
d Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
0
then PunycodeError -> Either PunycodeError (Int, Int)
forall a b. a -> Either a b
Left (Text -> PunycodeError
InvalidPunycode (Text
"Invalid digit: " Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Char -> Text
T.singleton (Int -> Char
chr Int
cp)))
else
let !t :: Int
t = if Int
k Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
<= Int
biasValue then Int
tmin
else if Int
k Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
>= Int
biasValue Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
tmax then Int
tmax
else Int
k Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
biasValue
!value' :: Int
value' = Int
value Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
d Int -> Int -> Int
forall a. Num a => a -> a -> a
* Int
weight
!pos' :: Int
pos' = Int
pos Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1
in if Int
d Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
t
then (Int, Int) -> Either PunycodeError (Int, Int)
forall a b. b -> Either a b
Right (Int
value', Int
pos')
else Int -> Int -> Int -> Int -> Either PunycodeError (Int, Int)
go Int
pos' Int
value' (Int
weight Int -> Int -> Int
forall a. Num a => a -> a -> a
* (Int
base Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
t)) (Int
k Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
base)
decodeDigitInt :: Int -> Int
decodeDigitInt :: Int -> Int
decodeDigitInt Int
cp
| Int
cp Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
>= Int
97 Bool -> Bool -> Bool
&& Int
cp Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
<= Int
122 = Int
cp Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
97
| Int
cp Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
>= Int
65 Bool -> Bool -> Bool
&& Int
cp Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
<= Int
90 = Int
cp Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
65
| Int
cp Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
>= Int
48 Bool -> Bool -> Bool
&& Int
cp Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
<= Int
57 = Int
cp Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
48 Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
26
| Bool
otherwise = -Int
1
adapt :: Int -> Int -> Bool -> Int
adapt :: Int -> Int -> Bool -> Int
adapt Int
deltaValue Int
numPoints Bool
firstTime =
let delta' :: Int
delta' = if Bool
firstTime then Int
deltaValue Int -> Int -> Int
forall a. Integral a => a -> a -> a
`div` Int
damp else Int
deltaValue Int -> Int -> Int
forall a. Integral a => a -> a -> a
`div` Int
2
delta'' :: Int
delta'' = Int
delta' Int -> Int -> Int
forall a. Num a => a -> a -> a
+ (Int
delta' Int -> Int -> Int
forall a. Integral a => a -> a -> a
`div` Int
numPoints)
in Int -> Int -> Int
go Int
delta'' Int
0
where
go :: Int -> Int -> Int
go Int
d Int
k
| Int
d Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> ((Int
base Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
tmin) Int -> Int -> Int
forall a. Num a => a -> a -> a
* Int
tmax) Int -> Int -> Int
forall a. Integral a => a -> a -> a
`div` Int
2 =
Int -> Int -> Int
go (Int
d Int -> Int -> Int
forall a. Integral a => a -> a -> a
`div` (Int
base Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
tmin)) (Int
k Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
base)
| Bool
otherwise = Int
k Int -> Int -> Int
forall a. Num a => a -> a -> a
+ (Int
base Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
tmin Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1) Int -> Int -> Int
forall a. Num a => a -> a -> a
* Int
d Int -> Int -> Int
forall a. Integral a => a -> a -> a
`div` (Int
d Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
skew)
isBasic :: Char -> Bool
isBasic :: Char -> Bool
isBasic = Char -> Bool
isAscii
isBasicCP :: Int -> Bool
isBasicCP :: Int -> Bool
isBasicCP Int
c = Int
c Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
0x80