@@ -101,7 +101,7 @@ import Data.Text.Encoding.Error (OnDecodeError, UnicodeException, strictDecode)
101101import Data.Text.Internal (Text(..), safe, textP)
102102import Data.Text.Internal.Private (runText)
103103import Data.Text.Internal.Unsafe.Char (ord, unsafeWrite)
104−import Data.Word (Word8, Word32)
104+import Data.Word (Word8, Word16, Word32)
105105import Foreign.C.Types (CSize)
106106import Foreign.ForeignPtr (ForeignPtr, touchForeignPtr, withForeignPtr)
107107import Foreign.Marshal.Utils (with)
@@ -450,6 +450,10 @@ encodeUtf8_1 (Text arr off len)
450450 fp' <- resize k m fp
451451 go n m fp' (unsafeForeignPtrToPtr fp')
452452 do1 ptr n m w k = poke8 ptr m w >> k (n+1) (m+1)
453+ loop :: (Word16 -> Int -> Int -> ForeignPtr Word8 -> Ptr Word8
454+ -> (Int -> Int -> IO ByteString)
455+ -> IO ByteString) -> Int -> Int -> ForeignPtr Word8 -> Ptr Word8
456+ -> IO ByteString
453457 loop act !n0 !m0 fp ptr = hot n0 m0
454458 where hot !n !m
455459 | n == offLen = touchForeignPtr fp >> return (PS fp 0 m)
@@ -478,17 +482,14 @@ encodeUtf8_1 (Text arr off len)
478482 poke8 ptr (m+1) $ ((w `shiftR` 6) .&. 0x3F) + 0x80
479483 poke8 ptr (m+2) $ (w .&. 0x3F) + 0x80
480484 k (n+1) (m+3)
481− go3 !n0 !m0 fp ptr = do
482− let hot !n !m
483− | n == offLen = touchForeignPtr fp >> return (PS fp 0 m)
484− | otherwise = do
485− case A.unsafeIndex arr n of
486− w| w <= 0x7F -> do1 ptr n m w hot
487− | w <= 0x7FF -> do2 ptr n m w hot
488− | w < 0xD800 -> do3 ptr n m w hot
489− | w > 0xDBFF -> do3 ptr n m w hot
490− | otherwise -> ensure 4 n m fp go4
491− hot n0 m0
485+ go3 = loop body where body w !n !m fp ptr cont =
486+ case w of
487+ _| w <= 0x7F -> do1 ptr n m w cont
488+ | w <= 0x7FF -> do2 ptr n m w cont
489+ | w < 0xD800 -> do3 ptr n m w cont
490+ | w > 0xDBFF -> do3 ptr n m w cont
491+ | otherwise -> ensure 4 n m fp go4
492+ {-# INLINE body #-}
492493 go4 !n0 !m0 fp ptr = do
493494 let hot !n !m
494495 | n == offLen = touchForeignPtr fp >> return (PS fp 0 m)