{-# LANGUAGE BangPatterns #-}
{-# LANGUAGE CPP #-}
{-# LANGUAGE MultiWayIf #-}
module Data.Aeson.Internal.Unescape
( unescapeText
) where
import Control.Exception (throwIO, try)
import Data.Bits (shiftL, shiftR, (.&.), (.|.))
import Data.ByteString (ByteString)
import Data.Text (Text)
import Data.Text.Encoding.Error (UnicodeException (..))
import Data.Text.Unsafe (unsafeDupablePerformIO)
import Data.Word (Word32, Word8)
import Foreign.ForeignPtr (withForeignPtr)
import Foreign.Ptr (Ptr, plusPtr)
import Foreign.Storable (peek)
import qualified Data.Primitive as P
import qualified Data.Text.Array as T
import qualified Data.Text.Internal as T
import Data.Aeson.Internal.ByteString
#if !MIN_VERSION_text(2,0,0)
import Data.Word (Word16)
#endif
unescapeText :: ByteString -> Either UnicodeException Text
unescapeText :: ByteString -> Either UnicodeException Text
unescapeText = forall a. IO a -> a
unsafeDupablePerformIO forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall e a. Exception e => IO a -> IO (Either e a)
try forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> IO Text
unescapeTextIO
throwDecodeError :: IO a
throwDecodeError :: forall a. IO a
throwDecodeError =
let desc :: String
desc = String
"Data.Text.Internal.Encoding.decodeUtf8: Invalid UTF-8 stream"
in forall e a. Exception e => e -> IO a
throwIO (String -> Maybe Word8 -> UnicodeException
DecodeError String
desc forall a. Maybe a
Nothing)
unescapeTextIO :: ByteString -> IO Text
#if MIN_VERSION_text(2,0,0)
unescapeTextIO :: ByteString -> IO Text
unescapeTextIO ByteString
bs = forall r. ByteString -> (ForeignPtr Word8 -> Int -> r) -> r
withBS ByteString
bs forall a b. (a -> b) -> a -> b
$ \ForeignPtr Word8
fptr Int
len ->
forall a b. ForeignPtr a -> (Ptr a -> IO b) -> IO b
withForeignPtr ForeignPtr Word8
fptr forall a b. (a -> b) -> a -> b
$ \Ptr Word8
begin -> do
let end :: Ptr Word8
end :: Ptr Word8
end = forall a b. Ptr a -> Int -> Ptr b
plusPtr Ptr Word8
begin Int
len
MutablePrimArray RealWorld Word8
arr <- forall (m :: * -> *) a.
(PrimMonad m, Prim a) =>
Int -> m (MutablePrimArray (PrimState m) a)
P.newPrimArray Int
len
let write3bytes :: Int -> Word8 -> Word8 -> Word8 -> Ptr Word8 -> IO Text
write3bytes :: Int -> Word8 -> Word8 -> Word8 -> Ptr Word8 -> IO Text
write3bytes !Int
out !Word8
b1 !Word8
b2 !Word8
b3 !Ptr Word8
inp = do
forall a (m :: * -> *).
(Prim a, PrimMonad m) =>
MutablePrimArray (PrimState m) a -> Int -> a -> m ()
P.writePrimArray MutablePrimArray RealWorld Word8
arr Int
out Word8
b1
Int -> Word8 -> Word8 -> Ptr Word8 -> IO Text
write2bytes (Int
out forall a. Num a => a -> a -> a
+ Int
1) Word8
b2 Word8
b3 Ptr Word8
inp
write2bytes :: Int -> Word8 -> Word8 -> Ptr Word8 -> IO Text
write2bytes :: Int -> Word8 -> Word8 -> Ptr Word8 -> IO Text
write2bytes !Int
out !Word8
b1 !Word8
b2 !Ptr Word8
inp = do
forall a (m :: * -> *).
(Prim a, PrimMonad m) =>
MutablePrimArray (PrimState m) a -> Int -> a -> m ()
P.writePrimArray MutablePrimArray RealWorld Word8
arr Int
out Word8
b1
Int -> Word8 -> Ptr Word8 -> IO Text
write1byte (Int
out forall a. Num a => a -> a -> a
+ Int
1) Word8
b2 Ptr Word8
inp
write1byte :: Int -> Word8 -> Ptr Word8 -> IO Text
write1byte :: Int -> Word8 -> Ptr Word8 -> IO Text
write1byte !Int
out !Word8
b1 !Ptr Word8
inp = do
forall a (m :: * -> *).
(Prim a, PrimMonad m) =>
MutablePrimArray (PrimState m) a -> Int -> a -> m ()
P.writePrimArray MutablePrimArray RealWorld Word8
arr Int
out Word8
b1
Int -> Ptr Word8 -> IO Text
state_start (Int
out forall a. Num a => a -> a -> a
+ Int
1) Ptr Word8
inp
writeCodePoint :: Int -> Ptr Word8 -> Word32 -> IO Text
writeCodePoint :: Int -> Ptr Word8 -> Word32 -> IO Text
writeCodePoint !Int
out !Ptr Word8
inp !Word32
acc
| Word32
acc forall a. Ord a => a -> a -> Bool
<= Word32
127 = do
forall a (m :: * -> *).
(Prim a, PrimMonad m) =>
MutablePrimArray (PrimState m) a -> Int -> a -> m ()
P.writePrimArray MutablePrimArray RealWorld Word8
arr Int
out (forall a b. (Integral a, Num b) => a -> b
fromIntegral Word32
acc :: Word8)
Int -> Ptr Word8 -> IO Text
state_start (Int
out forall a. Num a => a -> a -> a
+ Int
1) (forall a b. Ptr a -> Int -> Ptr b
plusPtr Ptr Word8
inp Int
1)
| Word32
acc forall a. Ord a => a -> a -> Bool
<= Word32
2047 = do
let b1 :: Word8
b1 = forall a b. (Integral a, Num b) => a -> b
fromIntegral (forall a. Bits a => a -> Int -> a
shiftR Word32
acc Int
6 forall a. Bits a => a -> a -> a
.|. Word32
192) :: Word8
let b2 :: Word8
b2 = forall a b. (Integral a, Num b) => a -> b
fromIntegral ((Word32
acc forall a. Bits a => a -> a -> a
.&. Word32
63) forall a. Bits a => a -> a -> a
.|. Word32
128) :: Word8
forall a (m :: * -> *).
(Prim a, PrimMonad m) =>
MutablePrimArray (PrimState m) a -> Int -> a -> m ()
P.writePrimArray MutablePrimArray RealWorld Word8
arr Int
out Word8
b1
Int -> Word8 -> Ptr Word8 -> IO Text
write1byte (Int
out forall a. Num a => a -> a -> a
+ Int
1) Word8
b2 (forall a b. Ptr a -> Int -> Ptr b
plusPtr Ptr Word8
inp Int
1)
| Word32
acc forall a. Ord a => a -> a -> Bool
<= Word32
65535 = do
let b1 :: Word8
b1 = forall a b. (Integral a, Num b) => a -> b
fromIntegral (forall a. Bits a => a -> Int -> a
shiftR Word32
acc Int
12 forall a. Bits a => a -> a -> a
.|. Word32
224) :: Word8
let b2 :: Word8
b2 = forall a b. (Integral a, Num b) => a -> b
fromIntegral ((forall a. Bits a => a -> Int -> a
shiftR Word32
acc Int
6 forall a. Bits a => a -> a -> a
.&. Word32
63) forall a. Bits a => a -> a -> a
.|. Word32
128) :: Word8
let b3 :: Word8
b3 = forall a b. (Integral a, Num b) => a -> b
fromIntegral ((Word32
acc forall a. Bits a => a -> a -> a
.&. Word32
63) forall a. Bits a => a -> a -> a
.|. Word32
128) :: Word8
forall a (m :: * -> *).
(Prim a, PrimMonad m) =>
MutablePrimArray (PrimState m) a -> Int -> a -> m ()
P.writePrimArray MutablePrimArray RealWorld Word8
arr Int
out Word8
b1
Int -> Word8 -> Word8 -> Ptr Word8 -> IO Text
write2bytes (Int
out forall a. Num a => a -> a -> a
+ Int
1) Word8
b2 Word8
b3 (forall a b. Ptr a -> Int -> Ptr b
plusPtr Ptr Word8
inp Int
1)
| Bool
otherwise = do
let b1 :: Word8
b1 = forall a b. (Integral a, Num b) => a -> b
fromIntegral (forall a. Bits a => a -> Int -> a
shiftR Word32
acc Int
18 forall a. Bits a => a -> a -> a
.|. Word32
240) :: Word8
let b2 :: Word8
b2 = forall a b. (Integral a, Num b) => a -> b
fromIntegral ((forall a. Bits a => a -> Int -> a
shiftR Word32
acc Int
12 forall a. Bits a => a -> a -> a
.&. Word32
63) forall a. Bits a => a -> a -> a
.|. Word32
128) :: Word8
let b3 :: Word8
b3 = forall a b. (Integral a, Num b) => a -> b
fromIntegral ((forall a. Bits a => a -> Int -> a
shiftR Word32
acc Int
6 forall a. Bits a => a -> a -> a
.&. Word32
63) forall a. Bits a => a -> a -> a
.|. Word32
128) :: Word8
let b4 :: Word8
b4 = forall a b. (Integral a, Num b) => a -> b
fromIntegral ((Word32
acc forall a. Bits a => a -> a -> a
.&. Word32
63) forall a. Bits a => a -> a -> a
.|. Word32
128) :: Word8
forall a (m :: * -> *).
(Prim a, PrimMonad m) =>
MutablePrimArray (PrimState m) a -> Int -> a -> m ()
P.writePrimArray MutablePrimArray RealWorld Word8
arr Int
out Word8
b1
Int -> Word8 -> Word8 -> Word8 -> Ptr Word8 -> IO Text
write3bytes (Int
out forall a. Num a => a -> a -> a
+ Int
1) Word8
b2 Word8
b3 Word8
b4 (forall a b. Ptr a -> Int -> Ptr b
plusPtr Ptr Word8
inp Int
1)
state_sudone :: Int -> Ptr Word8 -> Word32 -> Word32 -> IO Text
state_sudone :: Int -> Ptr Word8 -> Word32 -> Word32 -> IO Text
state_sudone !Int
out !Ptr Word8
inp !Word32
hi !Word32
lo
| Word32
56320 forall a. Ord a => a -> a -> Bool
<= Word32
lo, Word32
lo forall a. Ord a => a -> a -> Bool
<= Word32
57343
= Int -> Ptr Word8 -> Word32 -> IO Text
writeCodePoint Int
out Ptr Word8
inp (Word32
65536 forall a. Num a => a -> a -> a
+ (forall a. Bits a => a -> Int -> a
shiftL (Word32
hi forall a. Num a => a -> a -> a
- Word32
55296) Int
10 forall a. Bits a => a -> a -> a
.|. (Word32
lo forall a. Num a => a -> a -> a
- Word32
56320)))
| Bool
otherwise
= forall a. IO a
throwDecodeError
state_su4 :: Int -> Ptr Word8 -> Word32 -> Word32 -> IO Text
state_su4 :: Int -> Ptr Word8 -> Word32 -> Word32 -> IO Text
state_su4 !Int
out !Ptr Word8
inp !Word32
hi !Word32
acc
| Ptr Word8
inp forall a. Eq a => a -> a -> Bool
== Ptr Word8
end = forall a. IO a
throwDecodeError
| Bool
otherwise = do
Word8
w8 <- forall a. Storable a => Ptr a -> IO a
peek Ptr Word8
inp
if | Word8
48 forall a. Ord a => a -> a -> Bool
<= Word8
w8, Word8
w8 forall a. Ord a => a -> a -> Bool
<= Word8
57 ->
Int -> Ptr Word8 -> Word32 -> Word32 -> IO Text
state_sudone Int
out Ptr Word8
inp Word32
hi (forall a. Bits a => a -> Int -> a
shiftL Word32
acc Int
4 forall a. Bits a => a -> a -> a
.|. forall a b. (Integral a, Num b) => a -> b
fromIntegral (Word8
w8 forall a. Num a => a -> a -> a
- Word8
48))
| Word8
65 forall a. Ord a => a -> a -> Bool
<= Word8
w8, Word8
w8 forall a. Ord a => a -> a -> Bool
<= Word8
70 ->
Int -> Ptr Word8 -> Word32 -> Word32 -> IO Text
state_sudone Int
out Ptr Word8
inp Word32
hi (forall a. Bits a => a -> Int -> a
shiftL Word32
acc Int
4 forall a. Bits a => a -> a -> a
.|. forall a b. (Integral a, Num b) => a -> b
fromIntegral (Word8
w8 forall a. Num a => a -> a -> a
- Word8
55))
| Word8
97 forall a. Ord a => a -> a -> Bool
<= Word8
w8, Word8
w8 forall a. Ord a => a -> a -> Bool
<= Word8
102 ->
Int -> Ptr Word8 -> Word32 -> Word32 -> IO Text
state_sudone Int
out Ptr Word8
inp Word32
hi (forall a. Bits a => a -> Int -> a
shiftL Word32
acc Int
4 forall a. Bits a => a -> a -> a
.|. forall a b. (Integral a, Num b) => a -> b
fromIntegral (Word8
w8 forall a. Num a => a -> a -> a
- Word8
87))
| Bool
otherwise ->
forall a. IO a
throwDecodeError
state_su3 :: Int -> Ptr Word8 -> Word32 -> Word32 -> IO Text
state_su3 :: Int -> Ptr Word8 -> Word32 -> Word32 -> IO Text
state_su3 !Int
out !Ptr Word8
inp !Word32
hi !Word32
acc
| Ptr Word8
inp forall a. Eq a => a -> a -> Bool
== Ptr Word8
end = forall a. IO a
throwDecodeError
| Bool
otherwise = do
Word8
w8 <- forall a. Storable a => Ptr a -> IO a
peek Ptr Word8
inp
if | Word8
48 forall a. Ord a => a -> a -> Bool
<= Word8
w8, Word8
w8 forall a. Ord a => a -> a -> Bool
<= Word8
57 ->
Int -> Ptr Word8 -> Word32 -> Word32 -> IO Text
state_su4 Int
out (forall a b. Ptr a -> Int -> Ptr b
plusPtr Ptr Word8
inp Int
1) Word32
hi (forall a. Bits a => a -> Int -> a
shiftL Word32
acc Int
4 forall a. Bits a => a -> a -> a
.|. forall a b. (Integral a, Num b) => a -> b
fromIntegral (Word8
w8 forall a. Num a => a -> a -> a
- Word8
48))
| Word8
65 forall a. Ord a => a -> a -> Bool
<= Word8
w8, Word8
w8 forall a. Ord a => a -> a -> Bool
<= Word8
70 ->
Int -> Ptr Word8 -> Word32 -> Word32 -> IO Text
state_su4 Int
out (forall a b. Ptr a -> Int -> Ptr b
plusPtr Ptr Word8
inp Int
1) Word32
hi (forall a. Bits a => a -> Int -> a
shiftL Word32
acc Int
4 forall a. Bits a => a -> a -> a
.|. forall a b. (Integral a, Num b) => a -> b
fromIntegral (Word8
w8 forall a. Num a => a -> a -> a
- Word8
55))
| Word8
97 forall a. Ord a => a -> a -> Bool
<= Word8
w8, Word8
w8 forall a. Ord a => a -> a -> Bool
<= Word8
102 ->
Int -> Ptr Word8 -> Word32 -> Word32 -> IO Text
state_su4 Int
out (forall a b. Ptr a -> Int -> Ptr b
plusPtr Ptr Word8
inp Int
1) Word32
hi (forall a. Bits a => a -> Int -> a
shiftL Word32
acc Int
4 forall a. Bits a => a -> a -> a
.|. forall a b. (Integral a, Num b) => a -> b
fromIntegral (Word8
w8 forall a. Num a => a -> a -> a
- Word8
87))
| Bool
otherwise ->
forall a. IO a
throwDecodeError
state_su2 :: Int -> Ptr Word8 -> Word32 -> Word32 -> IO Text
state_su2 :: Int -> Ptr Word8 -> Word32 -> Word32 -> IO Text
state_su2 !Int
out !Ptr Word8
inp !Word32
hi !Word32
acc
| Ptr Word8
inp forall a. Eq a => a -> a -> Bool
== Ptr Word8
end = forall a. IO a
throwDecodeError
| Bool
otherwise = do
Word8
w8 <- forall a. Storable a => Ptr a -> IO a
peek Ptr Word8
inp
if | Word8
48 forall a. Ord a => a -> a -> Bool
<= Word8
w8, Word8
w8 forall a. Ord a => a -> a -> Bool
<= Word8
57 ->
Int -> Ptr Word8 -> Word32 -> Word32 -> IO Text
state_su3 Int
out (forall a b. Ptr a -> Int -> Ptr b
plusPtr Ptr Word8
inp Int
1) Word32
hi (forall a. Bits a => a -> Int -> a
shiftL Word32
acc Int
4 forall a. Bits a => a -> a -> a
.|. forall a b. (Integral a, Num b) => a -> b
fromIntegral (Word8
w8 forall a. Num a => a -> a -> a
- Word8
48))
| Word8
65 forall a. Ord a => a -> a -> Bool
<= Word8
w8, Word8
w8 forall a. Ord a => a -> a -> Bool
<= Word8
70 ->
Int -> Ptr Word8 -> Word32 -> Word32 -> IO Text
state_su3 Int
out (forall a b. Ptr a -> Int -> Ptr b
plusPtr Ptr Word8
inp Int
1) Word32
hi (forall a. Bits a => a -> Int -> a
shiftL Word32
acc Int
4 forall a. Bits a => a -> a -> a
.|. forall a b. (Integral a, Num b) => a -> b
fromIntegral (Word8
w8 forall a. Num a => a -> a -> a
- Word8
55))
| Word8
97 forall a. Ord a => a -> a -> Bool
<= Word8
w8, Word8
w8 forall a. Ord a => a -> a -> Bool
<= Word8
102 ->
Int -> Ptr Word8 -> Word32 -> Word32 -> IO Text
state_su3 Int
out (forall a b. Ptr a -> Int -> Ptr b
plusPtr Ptr Word8
inp Int
1) Word32
hi (forall a. Bits a => a -> Int -> a
shiftL Word32
acc Int
4 forall a. Bits a => a -> a -> a
.|. forall a b. (Integral a, Num b) => a -> b
fromIntegral (Word8
w8 forall a. Num a => a -> a -> a
- Word8
87))
| Bool
otherwise ->
forall a. IO a
throwDecodeError
state_su1 :: Int -> Ptr Word8 -> Word32 -> IO Text
state_su1 :: Int -> Ptr Word8 -> Word32 -> IO Text
state_su1 !Int
out !Ptr Word8
inp !Word32
hi
| Ptr Word8
inp forall a. Eq a => a -> a -> Bool
== Ptr Word8
end = forall a. IO a
throwDecodeError
| Bool
otherwise = do
Word8
w8 <- forall a. Storable a => Ptr a -> IO a
peek Ptr Word8
inp
if | Word8
48 forall a. Ord a => a -> a -> Bool
<= Word8
w8, Word8
w8 forall a. Ord a => a -> a -> Bool
<= Word8
57 ->
Int -> Ptr Word8 -> Word32 -> Word32 -> IO Text
state_su2 Int
out (forall a b. Ptr a -> Int -> Ptr b
plusPtr Ptr Word8
inp Int
1) Word32
hi (forall a b. (Integral a, Num b) => a -> b
fromIntegral (Word8
w8 forall a. Num a => a -> a -> a
- Word8
48))
| Word8
65 forall a. Ord a => a -> a -> Bool
<= Word8
w8, Word8
w8 forall a. Ord a => a -> a -> Bool
<= Word8
70 ->
Int -> Ptr Word8 -> Word32 -> Word32 -> IO Text
state_su2 Int
out (forall a b. Ptr a -> Int -> Ptr b
plusPtr Ptr Word8
inp Int
1) Word32
hi (forall a b. (Integral a, Num b) => a -> b
fromIntegral (Word8
w8 forall a. Num a => a -> a -> a
- Word8
55))
| Word8
97 forall a. Ord a => a -> a -> Bool
<= Word8
w8, Word8
w8 forall a. Ord a => a -> a -> Bool
<= Word8
102 ->
Int -> Ptr Word8 -> Word32 -> Word32 -> IO Text
state_su2 Int
out (forall a b. Ptr a -> Int -> Ptr b
plusPtr Ptr Word8
inp Int
1) Word32
hi (forall a b. (Integral a, Num b) => a -> b
fromIntegral (Word8
w8 forall a. Num a => a -> a -> a
- Word8
87))
| Bool
otherwise ->
forall a. IO a
throwDecodeError
state_su :: Int -> Ptr Word8 -> Word32 -> IO Text
state_su :: Int -> Ptr Word8 -> Word32 -> IO Text
state_su !Int
out !Ptr Word8
inp !Word32
hi
| Ptr Word8
inp forall a. Eq a => a -> a -> Bool
== Ptr Word8
end = forall a. IO a
throwDecodeError
| Bool
otherwise = do
Word8
w8 <- forall a. Storable a => Ptr a -> IO a
peek Ptr Word8
inp
case Word8
w8 of
Word8
117 -> Int -> Ptr Word8 -> Word32 -> IO Text
state_su1 Int
out (forall a b. Ptr a -> Int -> Ptr b
plusPtr Ptr Word8
inp Int
1) Word32
hi
Word8
_ -> forall a. IO a
throwDecodeError
state_ss :: Int -> Ptr Word8 -> Word32 -> IO Text
state_ss :: Int -> Ptr Word8 -> Word32 -> IO Text
state_ss !Int
out !Ptr Word8
inp !Word32
hi
| Ptr Word8
inp forall a. Eq a => a -> a -> Bool
== Ptr Word8
end = forall a. IO a
throwDecodeError
| Bool
otherwise = do
Word8
w8 <- forall a. Storable a => Ptr a -> IO a
peek Ptr Word8
inp
case Word8
w8 of
Word8
92 -> Int -> Ptr Word8 -> Word32 -> IO Text
state_su Int
out (forall a b. Ptr a -> Int -> Ptr b
plusPtr Ptr Word8
inp Int
1) Word32
hi
Word8
_ -> forall a. IO a
throwDecodeError
state_udone :: Int -> Ptr Word8 -> Word32 -> IO Text
state_udone :: Int -> Ptr Word8 -> Word32 -> IO Text
state_udone !Int
out !Ptr Word8
inp !Word32
acc
| Word32
acc forall a. Ord a => a -> a -> Bool
< Word32
55296 Bool -> Bool -> Bool
|| Word32
acc forall a. Ord a => a -> a -> Bool
> Word32
57343 =
Int -> Ptr Word8 -> Word32 -> IO Text
writeCodePoint Int
out Ptr Word8
inp Word32
acc
| Word32
acc forall a. Ord a => a -> a -> Bool
< Word32
56320 =
Int -> Ptr Word8 -> Word32 -> IO Text
state_ss Int
out (forall a b. Ptr a -> Int -> Ptr b
plusPtr Ptr Word8
inp Int
1) Word32
acc
| Bool
otherwise =
forall a. IO a
throwDecodeError
state_u4 :: Int -> Ptr Word8 -> Word32 -> IO Text
state_u4 :: Int -> Ptr Word8 -> Word32 -> IO Text
state_u4 !Int
out !Ptr Word8
inp !Word32
acc
| Ptr Word8
inp forall a. Eq a => a -> a -> Bool
== Ptr Word8
end = forall a. IO a
throwDecodeError
| Bool
otherwise = do
Word8
w8 <- forall a. Storable a => Ptr a -> IO a
peek Ptr Word8
inp
if | Word8
48 forall a. Ord a => a -> a -> Bool
<= Word8
w8, Word8
w8 forall a. Ord a => a -> a -> Bool
<= Word8
57 ->
Int -> Ptr Word8 -> Word32 -> IO Text
state_udone Int
out Ptr Word8
inp (forall a. Bits a => a -> Int -> a
shiftL Word32
acc Int
4 forall a. Bits a => a -> a -> a
.|. forall a b. (Integral a, Num b) => a -> b
fromIntegral (Word8
w8 forall a. Num a => a -> a -> a
- Word8
48))
| Word8
65 forall a. Ord a => a -> a -> Bool
<= Word8
w8, Word8
w8 forall a. Ord a => a -> a -> Bool
<= Word8
70 ->
Int -> Ptr Word8 -> Word32 -> IO Text
state_udone Int
out Ptr Word8
inp (forall a. Bits a => a -> Int -> a
shiftL Word32
acc Int
4 forall a. Bits a => a -> a -> a
.|. forall a b. (Integral a, Num b) => a -> b
fromIntegral (Word8
w8 forall a. Num a => a -> a -> a
- Word8
55))
| Word8
97 forall a. Ord a => a -> a -> Bool
<= Word8
w8, Word8
w8 forall a. Ord a => a -> a -> Bool
<= Word8
102 ->
Int -> Ptr Word8 -> Word32 -> IO Text
state_udone Int
out Ptr Word8
inp (forall a. Bits a => a -> Int -> a
shiftL Word32
acc Int
4 forall a. Bits a => a -> a -> a
.|. forall a b. (Integral a, Num b) => a -> b
fromIntegral (Word8
w8 forall a. Num a => a -> a -> a
- Word8
87))
| Bool
otherwise ->
forall a. IO a
throwDecodeError
state_u3 :: Int -> Ptr Word8 -> Word32 -> IO Text
state_u3 :: Int -> Ptr Word8 -> Word32 -> IO Text
state_u3 !Int
out !Ptr Word8
inp !Word32
acc
| Ptr Word8
inp forall a. Eq a => a -> a -> Bool
== Ptr Word8
end = forall a. IO a
throwDecodeError
| Bool
otherwise = do
Word8
w8 <- forall a. Storable a => Ptr a -> IO a
peek Ptr Word8
inp
if | Word8
48 forall a. Ord a => a -> a -> Bool
<= Word8
w8, Word8
w8 forall a. Ord a => a -> a -> Bool
<= Word8
57 ->
Int -> Ptr Word8 -> Word32 -> IO Text
state_u4 Int
out (forall a b. Ptr a -> Int -> Ptr b
plusPtr Ptr Word8
inp Int
1) (forall a. Bits a => a -> Int -> a
shiftL Word32
acc Int
4 forall a. Bits a => a -> a -> a
.|. forall a b. (Integral a, Num b) => a -> b
fromIntegral (Word8
w8 forall a. Num a => a -> a -> a
- Word8
48))
| Word8
65 forall a. Ord a => a -> a -> Bool
<= Word8
w8, Word8
w8 forall a. Ord a => a -> a -> Bool
<= Word8
70 ->
Int -> Ptr Word8 -> Word32 -> IO Text
state_u4 Int
out (forall a b. Ptr a -> Int -> Ptr b
plusPtr Ptr Word8
inp Int
1) (forall a. Bits a => a -> Int -> a
shiftL Word32
acc Int
4 forall a. Bits a => a -> a -> a
.|. forall a b. (Integral a, Num b) => a -> b
fromIntegral (Word8
w8 forall a. Num a => a -> a -> a
- Word8
55))
| Word8
97 forall a. Ord a => a -> a -> Bool
<= Word8
w8, Word8
w8 forall a. Ord a => a -> a -> Bool
<= Word8
102 ->
Int -> Ptr Word8 -> Word32 -> IO Text
state_u4 Int
out (forall a b. Ptr a -> Int -> Ptr b
plusPtr Ptr Word8
inp Int
1) (forall a. Bits a => a -> Int -> a
shiftL Word32
acc Int
4 forall a. Bits a => a -> a -> a
.|. forall a b. (Integral a, Num b) => a -> b
fromIntegral (Word8
w8 forall a. Num a => a -> a -> a
- Word8
87))
| Bool
otherwise ->
forall a. IO a
throwDecodeError
state_u2 :: Int -> Ptr Word8 -> Word32 -> IO Text
state_u2 :: Int -> Ptr Word8 -> Word32 -> IO Text
state_u2 !Int
out !Ptr Word8
inp !Word32
acc
| Ptr Word8
inp forall a. Eq a => a -> a -> Bool
== Ptr Word8
end = forall a. IO a
throwDecodeError
| Bool
otherwise = do
Word8
w8 <- forall a. Storable a => Ptr a -> IO a
peek Ptr Word8
inp
if | Word8
48 forall a. Ord a => a -> a -> Bool
<= Word8
w8, Word8
w8 forall a. Ord a => a -> a -> Bool
<= Word8
57 ->
Int -> Ptr Word8 -> Word32 -> IO Text
state_u3 Int
out (forall a b. Ptr a -> Int -> Ptr b
plusPtr Ptr Word8
inp Int
1) (forall a. Bits a => a -> Int -> a
shiftL Word32
acc Int
4 forall a. Bits a => a -> a -> a
.|. forall a b. (Integral a, Num b) => a -> b
fromIntegral (Word8
w8 forall a. Num a => a -> a -> a
- Word8
48))
| Word8
65 forall a. Ord a => a -> a -> Bool
<= Word8
w8, Word8
w8 forall a. Ord a => a -> a -> Bool
<= Word8
70 ->
Int -> Ptr Word8 -> Word32 -> IO Text
state_u3 Int
out (forall a b. Ptr a -> Int -> Ptr b
plusPtr Ptr Word8
inp Int
1) (forall a. Bits a => a -> Int -> a
shiftL Word32
acc Int
4 forall a. Bits a => a -> a -> a
.|. forall a b. (Integral a, Num b) => a -> b
fromIntegral (Word8
w8 forall a. Num a => a -> a -> a
- Word8
55))
| Word8
97 forall a. Ord a => a -> a -> Bool
<= Word8
w8, Word8
w8 forall a. Ord a => a -> a -> Bool
<= Word8
102 ->
Int -> Ptr Word8 -> Word32 -> IO Text
state_u3 Int
out (forall a b. Ptr a -> Int -> Ptr b
plusPtr Ptr Word8
inp Int
1) (forall a. Bits a => a -> Int -> a
shiftL Word32
acc Int
4 forall a. Bits a => a -> a -> a
.|. forall a b. (Integral a, Num b) => a -> b
fromIntegral (Word8
w8 forall a. Num a => a -> a -> a
- Word8
87))
| Bool
otherwise ->
forall a. IO a
throwDecodeError
state_u1 :: Int -> Ptr Word8 -> IO Text
state_u1 :: Int -> Ptr Word8 -> IO Text
state_u1 !Int
out !Ptr Word8
inp
| Ptr Word8
inp forall a. Eq a => a -> a -> Bool
== Ptr Word8
end = forall a. IO a
throwDecodeError
| Bool
otherwise = do
Word8
w8 <- forall a. Storable a => Ptr a -> IO a
peek Ptr Word8
inp
if | Word8
48 forall a. Ord a => a -> a -> Bool
<= Word8
w8, Word8
w8 forall a. Ord a => a -> a -> Bool
<= Word8
57 ->
Int -> Ptr Word8 -> Word32 -> IO Text
state_u2 Int
out (forall a b. Ptr a -> Int -> Ptr b
plusPtr Ptr Word8
inp Int
1) (forall a b. (Integral a, Num b) => a -> b
fromIntegral (Word8
w8 forall a. Num a => a -> a -> a
- Word8
48))
| Word8
65 forall a. Ord a => a -> a -> Bool
<= Word8
w8, Word8
w8 forall a. Ord a => a -> a -> Bool
<= Word8
70 ->
Int -> Ptr Word8 -> Word32 -> IO Text
state_u2 Int
out (forall a b. Ptr a -> Int -> Ptr b
plusPtr Ptr Word8
inp Int
1) (forall a b. (Integral a, Num b) => a -> b
fromIntegral (Word8
w8 forall a. Num a => a -> a -> a
- Word8
55))
| Word8
97 forall a. Ord a => a -> a -> Bool
<= Word8
w8, Word8
w8 forall a. Ord a => a -> a -> Bool
<= Word8
102 ->
Int -> Ptr Word8 -> Word32 -> IO Text
state_u2 Int
out (forall a b. Ptr a -> Int -> Ptr b
plusPtr Ptr Word8
inp Int
1) (forall a b. (Integral a, Num b) => a -> b
fromIntegral (Word8
w8 forall a. Num a => a -> a -> a
- Word8
87))
| Bool
otherwise ->
forall a. IO a
throwDecodeError
state_escape :: Int -> Ptr Word8 -> IO Text
state_escape :: Int -> Ptr Word8 -> IO Text
state_escape !Int
out !Ptr Word8
inp
| Ptr Word8
inp forall a. Eq a => a -> a -> Bool
== Ptr Word8
end = forall a. IO a
throwDecodeError
| Bool
otherwise = do
Word8
w8 <- forall a. Storable a => Ptr a -> IO a
peek Ptr Word8
inp
case Word8
w8 of
Word8
34 -> do
forall a (m :: * -> *).
(Prim a, PrimMonad m) =>
MutablePrimArray (PrimState m) a -> Int -> a -> m ()
P.writePrimArray MutablePrimArray RealWorld Word8
arr Int
out Word8
34
Int -> Ptr Word8 -> IO Text
state_start (Int
out forall a. Num a => a -> a -> a
+ Int
1) (forall a b. Ptr a -> Int -> Ptr b
plusPtr Ptr Word8
inp Int
1)
Word8
92 -> do
forall a (m :: * -> *).
(Prim a, PrimMonad m) =>
MutablePrimArray (PrimState m) a -> Int -> a -> m ()
P.writePrimArray MutablePrimArray RealWorld Word8
arr Int
out Word8
92
Int -> Ptr Word8 -> IO Text
state_start (Int
out forall a. Num a => a -> a -> a
+ Int
1) (forall a b. Ptr a -> Int -> Ptr b
plusPtr Ptr Word8
inp Int
1)
Word8
47 -> do
forall a (m :: * -> *).
(Prim a, PrimMonad m) =>
MutablePrimArray (PrimState m) a -> Int -> a -> m ()
P.writePrimArray MutablePrimArray RealWorld Word8
arr Int
out Word8
47
Int -> Ptr Word8 -> IO Text
state_start (Int
out forall a. Num a => a -> a -> a
+ Int
1) (forall a b. Ptr a -> Int -> Ptr b
plusPtr Ptr Word8
inp Int
1)
Word8
98 -> do
forall a (m :: * -> *).
(Prim a, PrimMonad m) =>
MutablePrimArray (PrimState m) a -> Int -> a -> m ()
P.writePrimArray MutablePrimArray RealWorld Word8
arr Int
out Word8
8
Int -> Ptr Word8 -> IO Text
state_start (Int
out forall a. Num a => a -> a -> a
+ Int
1) (forall a b. Ptr a -> Int -> Ptr b
plusPtr Ptr Word8
inp Int
1)
Word8
102 -> do
forall a (m :: * -> *).
(Prim a, PrimMonad m) =>
MutablePrimArray (PrimState m) a -> Int -> a -> m ()
P.writePrimArray MutablePrimArray RealWorld Word8
arr Int
out Word8
12
Int -> Ptr Word8 -> IO Text
state_start (Int
out forall a. Num a => a -> a -> a
+ Int
1) (forall a b. Ptr a -> Int -> Ptr b
plusPtr Ptr Word8
inp Int
1)
Word8
110 -> do
forall a (m :: * -> *).
(Prim a, PrimMonad m) =>
MutablePrimArray (PrimState m) a -> Int -> a -> m ()
P.writePrimArray MutablePrimArray RealWorld Word8
arr Int
out Word8
10
Int -> Ptr Word8 -> IO Text
state_start (Int
out forall a. Num a => a -> a -> a
+ Int
1) (forall a b. Ptr a -> Int -> Ptr b
plusPtr Ptr Word8
inp Int
1)
Word8
114 -> do
forall a (m :: * -> *).
(Prim a, PrimMonad m) =>
MutablePrimArray (PrimState m) a -> Int -> a -> m ()
P.writePrimArray MutablePrimArray RealWorld Word8
arr Int
out Word8
13
Int -> Ptr Word8 -> IO Text
state_start (Int
out forall a. Num a => a -> a -> a
+ Int
1) (forall a b. Ptr a -> Int -> Ptr b
plusPtr Ptr Word8
inp Int
1)
Word8
116 -> do
forall a (m :: * -> *).
(Prim a, PrimMonad m) =>
MutablePrimArray (PrimState m) a -> Int -> a -> m ()
P.writePrimArray MutablePrimArray RealWorld Word8
arr Int
out Word8
9
Int -> Ptr Word8 -> IO Text
state_start (Int
out forall a. Num a => a -> a -> a
+ Int
1) (forall a b. Ptr a -> Int -> Ptr b
plusPtr Ptr Word8
inp Int
1)
Word8
117 ->
Int -> Ptr Word8 -> IO Text
state_u1 Int
out (forall a b. Ptr a -> Int -> Ptr b
plusPtr Ptr Word8
inp Int
1)
Word8
_ -> forall a. IO a
throwDecodeError
state_input4c :: Int -> Ptr Word8 -> Word8 -> Word8 -> Word8 -> IO Text
state_input4c :: Int -> Ptr Word8 -> Word8 -> Word8 -> Word8 -> IO Text
state_input4c !Int
out !Ptr Word8
inp !Word8
b1 !Word8
b2 !Word8
b3
| Ptr Word8
inp forall a. Eq a => a -> a -> Bool
== Ptr Word8
end = forall a. IO a
throwDecodeError
| Bool
otherwise = do
Word8
w8 <- forall a. Storable a => Ptr a -> IO a
peek Ptr Word8
inp
if | (Word8
w8 forall a. Bits a => a -> a -> a
.&. Word8
192) forall a. Eq a => a -> a -> Bool
== Word8
128
, let acc :: Word32
acc = forall a. Bits a => a -> Int -> a
shiftL (forall a b. (Integral a, Num b) => a -> b
fromIntegral (Word8
b1 forall a. Bits a => a -> a -> a
.&. Word8
7)) Int
18
, let acc' :: Word32
acc' = Word32
acc forall a. Bits a => a -> a -> a
.|. forall a. Bits a => a -> Int -> a
shiftL (forall a b. (Integral a, Num b) => a -> b
fromIntegral (Word8
b2 forall a. Bits a => a -> a -> a
.&. Word8
63)) Int
12
, let acc'' :: Word32
acc'' = Word32
acc' forall a. Bits a => a -> a -> a
.|. forall a. Bits a => a -> Int -> a
shiftL (forall a b. (Integral a, Num b) => a -> b
fromIntegral (Word8
b3 forall a. Bits a => a -> a -> a
.&. Word8
63)) Int
6
, let acc''' :: Word32
acc''' = Word32
acc'' forall a. Bits a => a -> a -> a
.|. forall a b. (Integral a, Num b) => a -> b
fromIntegral (Word8
w8 forall a. Bits a => a -> a -> a
.&. Word8
63) :: Word32
, Word32
acc''' forall a. Ord a => a -> a -> Bool
>= Word32
65536 Bool -> Bool -> Bool
&& Word32
acc''' forall a. Ord a => a -> a -> Bool
< Word32
1114112 -> do
forall a (m :: * -> *).
(Prim a, PrimMonad m) =>
MutablePrimArray (PrimState m) a -> Int -> a -> m ()
P.writePrimArray MutablePrimArray RealWorld Word8
arr Int
out Word8
b1
Int -> Word8 -> Word8 -> Word8 -> Ptr Word8 -> IO Text
write3bytes (Int
out forall a. Num a => a -> a -> a
+ Int
1) Word8
b2 Word8
b3 Word8
w8 (forall a b. Ptr a -> Int -> Ptr b
plusPtr Ptr Word8
inp Int
1)
| Bool
otherwise ->
forall a. IO a
throwDecodeError
state_input4b :: Int -> Ptr Word8 -> Word8 -> Word8 -> IO Text
state_input4b :: Int -> Ptr Word8 -> Word8 -> Word8 -> IO Text
state_input4b !Int
out !Ptr Word8
inp !Word8
b1 !Word8
b2
| Ptr Word8
inp forall a. Eq a => a -> a -> Bool
== Ptr Word8
end = forall a. IO a
throwDecodeError
| Bool
otherwise = do
Word8
w8 <- forall a. Storable a => Ptr a -> IO a
peek Ptr Word8
inp
if | (Word8
w8 forall a. Bits a => a -> a -> a
.&. Word8
192) forall a. Eq a => a -> a -> Bool
== Word8
128 ->
Int -> Ptr Word8 -> Word8 -> Word8 -> Word8 -> IO Text
state_input4c Int
out (forall a b. Ptr a -> Int -> Ptr b
plusPtr Ptr Word8
inp Int
1) Word8
b1 Word8
b2 Word8
w8
| Bool
otherwise ->
forall a. IO a
throwDecodeError
state_input4 :: Int -> Ptr Word8 -> Word8 -> IO Text
state_input4 :: Int -> Ptr Word8 -> Word8 -> IO Text
state_input4 !Int
out !Ptr Word8
inp !Word8
b1
| Ptr Word8
inp forall a. Eq a => a -> a -> Bool
== Ptr Word8
end = forall a. IO a
throwDecodeError
| Bool
otherwise = do
Word8
w8 <- forall a. Storable a => Ptr a -> IO a
peek Ptr Word8
inp
if | (Word8
w8 forall a. Bits a => a -> a -> a
.&. Word8
192) forall a. Eq a => a -> a -> Bool
== Word8
128 ->
Int -> Ptr Word8 -> Word8 -> Word8 -> IO Text
state_input4b Int
out (forall a b. Ptr a -> Int -> Ptr b
plusPtr Ptr Word8
inp Int
1) Word8
b1 Word8
w8
| Bool
otherwise ->
forall a. IO a
throwDecodeError
state_input3b :: Int -> Ptr Word8 -> Word8 -> Word8 -> IO Text
state_input3b :: Int -> Ptr Word8 -> Word8 -> Word8 -> IO Text
state_input3b !Int
out !Ptr Word8
inp !Word8
b1 !Word8
b2
| Ptr Word8
inp forall a. Eq a => a -> a -> Bool
== Ptr Word8
end = forall a. IO a
throwDecodeError
| Bool
otherwise = do
Word8
w8 <- forall a. Storable a => Ptr a -> IO a
peek Ptr Word8
inp
if | (Word8
w8 forall a. Bits a => a -> a -> a
.&. Word8
192) forall a. Eq a => a -> a -> Bool
== Word8
128
, let acc :: Word32
acc = forall a. Bits a => a -> Int -> a
shiftL (forall a b. (Integral a, Num b) => a -> b
fromIntegral (Word8
b1 forall a. Bits a => a -> a -> a
.&. Word8
15)) Int
12
, let acc' :: Word32
acc' = Word32
acc forall a. Bits a => a -> a -> a
.|. forall a. Bits a => a -> Int -> a
shiftL (forall a b. (Integral a, Num b) => a -> b
fromIntegral (Word8
b2 forall a. Bits a => a -> a -> a
.&. Word8
63)) Int
6
, let acc'' :: Word32
acc'' = Word32
acc' forall a. Bits a => a -> a -> a
.|. forall a b. (Integral a, Num b) => a -> b
fromIntegral (Word8
w8 forall a. Bits a => a -> a -> a
.&. Word8
63) :: Word32
, (Word32
acc'' forall a. Ord a => a -> a -> Bool
>= Word32
2048 Bool -> Bool -> Bool
&& Word32
acc'' forall a. Ord a => a -> a -> Bool
< Word32
55296) Bool -> Bool -> Bool
|| Word32
acc'' forall a. Ord a => a -> a -> Bool
> Word32
57343 -> do
forall a (m :: * -> *).
(Prim a, PrimMonad m) =>
MutablePrimArray (PrimState m) a -> Int -> a -> m ()
P.writePrimArray MutablePrimArray RealWorld Word8
arr Int
out Word8
b1
Int -> Word8 -> Word8 -> Ptr Word8 -> IO Text
write2bytes (Int
out forall a. Num a => a -> a -> a
+ Int
1) Word8
b2 Word8
w8 (forall a b. Ptr a -> Int -> Ptr b
plusPtr Ptr Word8
inp Int
1)
| Bool
otherwise ->
forall a. IO a
throwDecodeError
state_input3 :: Int -> Ptr Word8 -> Word8 -> IO Text
state_input3 :: Int -> Ptr Word8 -> Word8 -> IO Text
state_input3 !Int
out !Ptr Word8
inp !Word8
b1
| Ptr Word8
inp forall a. Eq a => a -> a -> Bool
== Ptr Word8
end = forall a. IO a
throwDecodeError
| Bool
otherwise = do
Word8
w8 <- forall a. Storable a => Ptr a -> IO a
peek Ptr Word8
inp
if | (Word8
w8 forall a. Bits a => a -> a -> a
.&. Word8
192) forall a. Eq a => a -> a -> Bool
== Word8
128 ->
Int -> Ptr Word8 -> Word8 -> Word8 -> IO Text
state_input3b Int
out (forall a b. Ptr a -> Int -> Ptr b
plusPtr Ptr Word8
inp Int
1) Word8
b1 Word8
w8
| Bool
otherwise ->
forall a. IO a
throwDecodeError
state_input2 :: Int -> Ptr Word8 -> Word8 -> IO Text
state_input2 :: Int -> Ptr Word8 -> Word8 -> IO Text
state_input2 !Int
out !Ptr Word8
inp !Word8
b1
| Ptr Word8
inp forall a. Eq a => a -> a -> Bool
== Ptr Word8
end = forall a. IO a
throwDecodeError
| Bool
otherwise = do
Word8
w8 <- forall a. Storable a => Ptr a -> IO a
peek Ptr Word8
inp
if | (Word8
w8 forall a. Bits a => a -> a -> a
.&. Word8
192) forall a. Eq a => a -> a -> Bool
== Word8
128,
let acc :: Word32
acc = forall a. Bits a => a -> Int -> a
shiftL (forall a b. (Integral a, Num b) => a -> b
fromIntegral (Word8
b1 forall a. Bits a => a -> a -> a
.&. Word8
63)) Int
6 :: Word32
acc' :: Word32
acc' = Word32
acc forall a. Bits a => a -> a -> a
.|. forall a b. (Integral a, Num b) => a -> b
fromIntegral (Word8
w8 forall a. Bits a => a -> a -> a
.&. Word8
63) :: Word32
, Word32
acc' forall a. Ord a => a -> a -> Bool
>= Word32
128 -> do
forall a (m :: * -> *).
(Prim a, PrimMonad m) =>
MutablePrimArray (PrimState m) a -> Int -> a -> m ()
P.writePrimArray MutablePrimArray RealWorld Word8
arr Int
out Word8
b1
Int -> Word8 -> Ptr Word8 -> IO Text
write1byte (Int
out forall a. Num a => a -> a -> a
+ Int
1) Word8
w8 (forall a b. Ptr a -> Int -> Ptr b
plusPtr Ptr Word8
inp Int
1)
| Bool
otherwise ->
forall a. IO a
throwDecodeError
state_start :: Int -> Ptr Word8 -> IO Text
state_start :: Int -> Ptr Word8 -> IO Text
state_start !Int
out !Ptr Word8
inp
| Ptr Word8
inp forall a. Eq a => a -> a -> Bool
== Ptr Word8
end = do
forall (m :: * -> *) a.
(PrimMonad m, Prim a) =>
MutablePrimArray (PrimState m) a -> Int -> m ()
P.shrinkMutablePrimArray MutablePrimArray RealWorld Word8
arr Int
out
PrimArray Word8
frozenArr <- forall (m :: * -> *) a.
PrimMonad m =>
MutablePrimArray (PrimState m) a -> m (PrimArray a)
P.unsafeFreezePrimArray MutablePrimArray RealWorld Word8
arr
forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ case PrimArray Word8
frozenArr of
P.PrimArray ByteArray#
ba -> Array -> Int -> Int -> Text
T.Text (ByteArray# -> Array
T.ByteArray ByteArray#
ba) Int
0 Int
out
| Bool
otherwise = do
Word8
w8 <- forall a. Storable a => Ptr a -> IO a
peek Ptr Word8
inp
if | Word8
w8 forall a. Eq a => a -> a -> Bool
== Word8
92 -> Int -> Ptr Word8 -> IO Text
state_escape Int
out (forall a b. Ptr a -> Int -> Ptr b
plusPtr Ptr Word8
inp Int
1)
| Word8
w8 forall a. Ord a => a -> a -> Bool
< Word8
128 -> do
forall a (m :: * -> *).
(Prim a, PrimMonad m) =>
MutablePrimArray (PrimState m) a -> Int -> a -> m ()
P.writePrimArray MutablePrimArray RealWorld Word8
arr Int
out Word8
w8
Int -> Ptr Word8 -> IO Text
state_start (Int
out forall a. Num a => a -> a -> a
+ Int
1) (forall a b. Ptr a -> Int -> Ptr b
plusPtr Ptr Word8
inp Int
1)
| Word8
w8 forall a. Ord a => a -> a -> Bool
< Word8
192 -> forall a. IO a
throwDecodeError
| Word8
w8 forall a. Ord a => a -> a -> Bool
< Word8
224 -> Int -> Ptr Word8 -> Word8 -> IO Text
state_input2 Int
out (forall a b. Ptr a -> Int -> Ptr b
plusPtr Ptr Word8
inp Int
1) Word8
w8
| Word8
w8 forall a. Ord a => a -> a -> Bool
< Word8
240 -> Int -> Ptr Word8 -> Word8 -> IO Text
state_input3 Int
out (forall a b. Ptr a -> Int -> Ptr b
plusPtr Ptr Word8
inp Int
1) Word8
w8
| Word8
w8 forall a. Ord a => a -> a -> Bool
< Word8
248 -> Int -> Ptr Word8 -> Word8 -> IO Text
state_input4 Int
out (forall a b. Ptr a -> Int -> Ptr b
plusPtr Ptr Word8
inp Int
1) Word8
w8
| Bool
otherwise -> forall a. IO a
throwDecodeError
Int -> Ptr Word8 -> IO Text
state_start (Int
0 :: Int) Ptr Word8
begin
#else
unescapeTextIO bs = withBS bs $ \fptr len ->
withForeignPtr fptr $ \begin -> do
let end :: Ptr Word8
end = plusPtr begin len
arr <- P.newPrimArray len
let writeLowSurrogate :: Int -> Word16 -> Ptr Word8 -> IO Text
writeLowSurrogate !out !w16 !inp = do
P.writePrimArray arr out w16
state_start (out + 1) inp
state_sudone :: Int -> Ptr Word8 -> Word32 -> IO Text
state_sudone !out !inp !lo
| 56320 <= lo, lo <= 57343 = do
P.writePrimArray arr out (fromIntegral lo)
state_start (out + 1) (plusPtr inp 1)
| otherwise =
throwDecodeError
state_su4 :: Int -> Ptr Word8 -> Word32 -> IO Text
state_su4 !out !inp !acc
| inp == end = throwDecodeError
| otherwise = do
w8 <- peek inp
if | 48 <= w8, w8 <= 57 ->
state_sudone out inp (shiftL acc 4 .|. fromIntegral (w8 - 48))
| 65 <= w8, w8 <= 70 ->
state_sudone out inp (shiftL acc 4 .|. fromIntegral (w8 - 55))
| 97 <= w8, w8 <= 102 ->
state_sudone out inp (shiftL acc 4 .|. fromIntegral (w8 - 87))
| otherwise ->
throwDecodeError
state_su3 :: Int -> Ptr Word8 -> Word32 -> IO Text
state_su3 !out !inp !acc
| inp == end = throwDecodeError
| otherwise = do
w8 <- peek inp
if | 48 <= w8, w8 <= 57 ->
state_su4 out (plusPtr inp 1) (shiftL acc 4 .|. fromIntegral (w8 - 48))
| 65 <= w8, w8 <= 70 ->
state_su4 out (plusPtr inp 1) (shiftL acc 4 .|. fromIntegral (w8 - 55))
| 97 <= w8, w8 <= 102 ->
state_su4 out (plusPtr inp 1) (shiftL acc 4 .|. fromIntegral (w8 - 87))
| otherwise -> throwDecodeError
state_su2 :: Int -> Ptr Word8 -> Word32 -> IO Text
state_su2 !out !inp !acc
| inp == end = throwDecodeError
| otherwise = do
w8 <- peek inp
if | 48 <= w8, w8 <= 57 ->
state_su3 out (plusPtr inp 1) (shiftL acc 4 .|. fromIntegral (w8 - 48))
| 65 <= w8, w8 <= 70 ->
state_su3 out (plusPtr inp 1) (shiftL acc 4 .|. fromIntegral (w8 - 55))
| 97 <= w8, w8 <= 102 ->
state_su3 out (plusPtr inp 1) (shiftL acc 4 .|. fromIntegral (w8 - 87))
| otherwise ->
throwDecodeError
state_su1 :: Int -> Ptr Word8 -> IO Text
state_su1 !out !inp
| inp == end = throwDecodeError
| otherwise = do
w8 <- peek inp
if | 48 <= w8, w8 <= 57 ->
state_su2 out (plusPtr inp 1) (fromIntegral (w8 - 48))
| 65 <= w8, w8 <= 70 ->
state_su2 out (plusPtr inp 1) (fromIntegral (w8 - 55))
| 97 <= w8, w8 <= 102 ->
state_su2 out (plusPtr inp 1) (fromIntegral (w8 - 87))
| otherwise ->
throwDecodeError
state_su :: Int -> Ptr Word8 -> IO Text
state_su !out !inp
| inp == end = throwDecodeError
| otherwise = do
w8 <- peek inp
case w8 of
117 -> state_su1 out (plusPtr inp 1)
_ -> throwDecodeError
state_ss :: Int -> Ptr Word8 -> IO Text
state_ss !out !inp
| inp == end = throwDecodeError
| otherwise = do
w8 <- peek inp
case w8 of
92 -> state_su out (plusPtr inp 1)
_ -> throwDecodeError
state_udone :: Int -> Ptr Word8 -> Word32 -> IO Text
state_udone !out !inp !acc
| acc < 55296 || acc > 57343 = do
P.writePrimArray arr out (fromIntegral acc)
state_start (out + 1) (plusPtr inp 1)
| acc < 56320 = do
P.writePrimArray arr out (fromIntegral acc)
state_ss (out + 1) (plusPtr inp 1)
| otherwise =
throwDecodeError
state_u4 :: Int -> Ptr Word8 -> Word32 -> IO Text
state_u4 !out !inp !acc
| inp == end = throwDecodeError
| otherwise = do
w8 <- peek inp
if | 48 <= w8, w8 <= 57 ->
state_udone out inp (shiftL acc 4 .|. fromIntegral (w8 - 48))
| 65 <= w8, w8 <= 70 ->
state_udone out inp (shiftL acc 4 .|. fromIntegral (w8 - 55))
| 97 <= w8, w8 <= 102 ->
state_udone out inp (shiftL acc 4 .|. fromIntegral (w8 - 87))
| otherwise ->
throwDecodeError
state_u3 :: Int -> Ptr Word8 -> Word32 -> IO Text
state_u3 !out !inp !acc
| inp == end = throwDecodeError
| otherwise = do
w8 <- peek inp
if | 48 <= w8, w8 <= 57 ->
state_u4 out (plusPtr inp 1) (shiftL acc 4 .|. fromIntegral (w8 - 48))
| 65 <= w8, w8 <= 70 ->
state_u4 out (plusPtr inp 1) (shiftL acc 4 .|. fromIntegral (w8 - 55))
| 97 <= w8, w8 <= 102 ->
state_u4 out (plusPtr inp 1) (shiftL acc 4 .|. fromIntegral (w8 - 87))
| otherwise ->
throwDecodeError
state_u2 :: Int -> Ptr Word8 -> Word32 -> IO Text
state_u2 !out !inp !acc
| inp == end = throwDecodeError
| otherwise = do
w8 <- peek inp
if | 48 <= w8, w8 <= 57 ->
state_u3 out (plusPtr inp 1) (shiftL acc 4 .|. fromIntegral (w8 - 48))
| 65 <= w8, w8 <= 70 ->
state_u3 out (plusPtr inp 1) (shiftL acc 4 .|. fromIntegral (w8 - 55))
| 97 <= w8, w8 <= 102 ->
state_u3 out (plusPtr inp 1) (shiftL acc 4 .|. fromIntegral (w8 - 87))
| otherwise ->
throwDecodeError
state_u1 :: Int -> Ptr Word8 -> IO Text
state_u1 !out !inp
| inp == end = throwDecodeError
| otherwise = do
w8 <- peek inp
if | 48 <= w8, w8 <= 57 ->
state_u2 out (plusPtr inp 1) (fromIntegral (w8 - 48))
| 65 <= w8, w8 <= 70 ->
state_u2 out (plusPtr inp 1) (fromIntegral (w8 - 55))
| 97 <= w8, w8 <= 102 ->
state_u2 out (plusPtr inp 1) (fromIntegral (w8 - 87))
| otherwise ->
throwDecodeError
state_escape :: Int -> Ptr Word8 -> IO Text
state_escape out inp
| inp == end = throwDecodeError
| otherwise = do
w8 <- peek inp
case w8 of
34 -> do
P.writePrimArray arr out 34
state_start (out + 1) (plusPtr inp 1)
92 -> do
P.writePrimArray arr out 92
state_start (out + 1) (plusPtr inp 1)
47 -> do
P.writePrimArray arr out 47
state_start (out + 1) (plusPtr inp 1)
98 -> do
P.writePrimArray arr out 8
state_start (out + 1) (plusPtr inp 1)
102 -> do
P.writePrimArray arr out 12
state_start (out + 1) (plusPtr inp 1)
110 -> do
P.writePrimArray arr out 10
state_start (out + 1) (plusPtr inp 1)
114 -> do
P.writePrimArray arr out 13
state_start (out + 1) (plusPtr inp 1)
116 -> do
P.writePrimArray arr out 9
state_start (out + 1) (plusPtr inp 1)
117 ->
state_u1 out (plusPtr inp 1)
_ -> throwDecodeError
state_input4c :: Int -> Ptr Word8 -> Word32 -> IO Text
state_input4c !out !inp !acc
| inp == end = throwDecodeError
| otherwise = do
w8 <- peek inp
if | (w8 .&. 0xc0) == 0x80
, let acc' = acc .|. fromIntegral (w8 .&. 63)
, acc' >= 65536 && acc' < 1114112 ->
if
| otherwise -> do
let u = acc' - 65536
hi = fromIntegral (55296 + shiftR u 10) :: Word16
lo = fromIntegral (56320 + (u .&. 1023)) :: Word16
P.writePrimArray arr out hi
writeLowSurrogate (out + 1) lo (plusPtr inp 1)
| otherwise -> throwDecodeError
state_input4b :: Int -> Ptr Word8 -> Word32 -> IO Text
state_input4b !out !inp !acc
| inp == end = throwDecodeError
| otherwise = do
w8 <- peek inp
if | (w8 .&. 0xc0) == 0x80 ->
state_input4c out (plusPtr inp 1) (acc .|. shiftL (fromIntegral (w8 .&. 63)) 6)
| otherwise -> throwDecodeError
state_input4 :: Int -> Ptr Word8 -> Word32 -> IO Text
state_input4 !out !inp !acc
| inp == end = throwDecodeError
| otherwise = do
w8 <- peek inp
if | (w8 .&. 0xc0) == 0x80 ->
state_input4b out (plusPtr inp 1) (acc .|. shiftL (fromIntegral (w8 .&. 63)) 12)
| otherwise -> throwDecodeError
state_input3b :: Int -> Ptr Word8 -> Word32 -> IO Text
state_input3b !out !inp !acc
| inp == end = throwDecodeError
| otherwise = do
w8 <- peek inp
if | (w8 .&. 0xc0) == 0x80
, let acc' = acc .|. fromIntegral (w8 .&. 63)
, (acc' >= 2048 && acc' < 55296) || acc' > 57343 ->
if | acc' < 65536 -> do
P.writePrimArray arr out (fromIntegral acc')
state_start (out + 1) (plusPtr inp 1)
| otherwise -> do
let u = acc' - 65536
hi = fromIntegral (55296 + shiftR u 10) :: Word16
lo = fromIntegral (56320 + (u .&. 1023)) :: Word16
P.writePrimArray arr out hi
writeLowSurrogate (out + 1) lo (plusPtr inp 1)
| otherwise -> throwDecodeError
state_input3 :: Int -> Ptr Word8 -> Word32 -> IO Text
state_input3 !out !inp !acc
| inp == end = throwDecodeError
| otherwise = do
w8 <- peek inp
if | (w8 .&. 0xc0) == 0x80 ->
state_input3b out (plusPtr inp 1) (acc .|. shiftL (fromIntegral (w8 .&. 63)) 6)
| otherwise -> throwDecodeError
state_input2 :: Int -> Ptr Word8 -> Word32 -> IO Text
state_input2 !out !inp !acc
| inp == end = throwDecodeError
| otherwise = do
w8 <- peek inp
if | (w8 .&. 0xc0) == 0x80
, let acc' = acc .|. fromIntegral (w8 .&. 63)
, acc' >= 0x80 -> do
P.writePrimArray arr out (fromIntegral acc' :: Word16)
state_start (out + 1) (plusPtr inp 1)
| otherwise -> throwDecodeError
state_start :: Int -> Ptr Word8 -> IO Text
state_start !out !inp
| inp == end = do
P.shrinkMutablePrimArray arr out
frozenArr <- P.unsafeFreezePrimArray arr
return $ case frozenArr of
P.PrimArray ba -> T.Text (T.Array ba) 0 out
| otherwise = do
w8 <- peek inp
if | w8 == 92 -> state_escape out (plusPtr inp 1)
| w8 < 0x80 -> do
P.writePrimArray arr out (fromIntegral w8 :: Word16)
state_start (out + 1) (plusPtr inp 1)
| w8 < 0xc0 -> throwDecodeError
| w8 < 224 -> state_input2 out (plusPtr inp 1) (shiftL (fromIntegral (w8 .&. 63)) 6)
| w8 < 240 -> state_input3 out (plusPtr inp 1) (shiftL (fromIntegral (w8 .&. 15)) 12)
| w8 < 248 -> state_input4 out (plusPtr inp 1) (shiftL (fromIntegral (w8 .&. 7)) 18)
| otherwise -> throwDecodeError
state_start (0 :: Int) begin
#endif