@@ -435,34 +435,34 @@ encodeUtf8_1 (Text arr off len)
435435 where
436436 offLen = off + len
437437 poke8 p k v = poke (p `plusPtr` k) (fromIntegral v :: Word8)
438+ ensure k n m p act
439+ | size-m >= k = act
440+ | otherwise = {-# SCC "resizeUtf8/ensure" #-} do
441+ let newSize = size `shiftL` 1
442+ fp' <- mallocByteString newSize
443+ withForeignPtr fp' $ \ptr' ->
444+ memcpy ptr' p (fromIntegral m)
445+ start newSize n m fp'
446+ {-# INLINE ensure #-}
438447 go !n !m ptr
439448 | n == offLen = return (PS fp 0 m)
440449 | otherwise = do
441− let ensure k act
442− | size-m >= k = act
443− | otherwise = {-# SCC "resizeUtf8/ensure" #-} do
444− let newSize = size `shiftL` 1
445− fp' <- mallocByteString newSize
446− withForeignPtr fp' $ \ptr' ->
447− memcpy ptr' ptr (fromIntegral m)
448− start newSize n m fp'
449− {-# INLINE ensure #-}
450450 case A.unsafeIndex arr n of
451− w| w <= 0x7F -> ensure 1 $ do
451+ w| w <= 0x7F -> ensure 1 n m ptr $ do
452452 poke8 ptr m (fromIntegral w :: Word8)
453453 go (n+1) (m+1) ptr
454− | w <= 0x7FF -> ensure 2 $ do
454+ | w <= 0x7FF -> ensure 2 n m ptr $ do
455455 poke8 ptr m $ (w `shiftR` 6) + 0xC0
456456 poke8 ptr (m+1) $ (w .&. 0x3f) + 0x80
457457 go (n+1) (m+2) ptr
458− | 0xD800 <= w && w <= 0xDBFF -> ensure 4 $ do
458+ | 0xD800 <= w && w <= 0xDBFF -> ensure 4 n m ptr $ do
459459 let c = ord $ U16.chr2 w (A.unsafeIndex arr (n+1))
460460 poke8 ptr m $ (c `shiftR` 18) + 0xF0
461461 poke8 ptr (m+1) $ ((c `shiftR` 12) .&. 0x3F) + 0x80
462462 poke8 ptr (m+2) $ ((c `shiftR` 6) .&. 0x3F) + 0x80
463463 poke8 ptr (m+3) $ (c .&. 0x3F) + 0x80
464464 go (n+2) (m+4) ptr
465− | otherwise -> ensure 3 $ do
465+ | otherwise -> ensure 3 n m ptr $ do
466466 poke8 ptr m $ (w `shiftR` 12) + 0xE0
467467 poke8 ptr (m+1) $ ((w `shiftR` 6) .&. 0x3F) + 0x80
468468 poke8 ptr (m+2) $ (w .&. 0x3F) + 0x80