on master help sign in
encodeUtf8_1: hoist ensure up a level
71a54fdBryan O'Sullivan
Data/Text/Encoding.hs+13−13
@@ -435,34 +435,34 @@ encodeUtf8_1 (Text arr off len)
435 where
435 where
436 offLen = off + len
436 offLen = off + len
437 poke8 p k v = poke (p `plusPtr` k) (fromIntegral v :: Word8)
437 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 #-}
438 go !n !m ptr
447 go !n !m ptr
439 | n == offLen = return (PS fp 0 m)
448 | n == offLen = return (PS fp 0 m)
440 | otherwise = do
449 | 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 #-}
450 case A.unsafeIndex arr n of
450 case A.unsafeIndex arr n of
451− w| w <= 0x7F -> ensure 1 $ do
451+ w| w <= 0x7F -> ensure 1 n m ptr $ do
452 poke8 ptr m (fromIntegral w :: Word8)
452 poke8 ptr m (fromIntegral w :: Word8)
453 go (n+1) (m+1) ptr
453 go (n+1) (m+1) ptr
454− | w <= 0x7FF -> ensure 2 $ do
454+ | w <= 0x7FF -> ensure 2 n m ptr $ do
455 poke8 ptr m $ (w `shiftR` 6) + 0xC0
455 poke8 ptr m $ (w `shiftR` 6) + 0xC0
456 poke8 ptr (m+1) $ (w .&. 0x3f) + 0x80
456 poke8 ptr (m+1) $ (w .&. 0x3f) + 0x80
457 go (n+1) (m+2) ptr
457 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
459 let c = ord $ U16.chr2 w (A.unsafeIndex arr (n+1))
459 let c = ord $ U16.chr2 w (A.unsafeIndex arr (n+1))
460 poke8 ptr m $ (c `shiftR` 18) + 0xF0
460 poke8 ptr m $ (c `shiftR` 18) + 0xF0
461 poke8 ptr (m+1) $ ((c `shiftR` 12) .&. 0x3F) + 0x80
461 poke8 ptr (m+1) $ ((c `shiftR` 12) .&. 0x3F) + 0x80
462 poke8 ptr (m+2) $ ((c `shiftR` 6) .&. 0x3F) + 0x80
462 poke8 ptr (m+2) $ ((c `shiftR` 6) .&. 0x3F) + 0x80
463 poke8 ptr (m+3) $ (c .&. 0x3F) + 0x80
463 poke8 ptr (m+3) $ (c .&. 0x3F) + 0x80
464 go (n+2) (m+4) ptr
464 go (n+2) (m+4) ptr
465− | otherwise -> ensure 3 $ do
465+ | otherwise -> ensure 3 n m ptr $ do
466 poke8 ptr m $ (w `shiftR` 12) + 0xE0
466 poke8 ptr m $ (w `shiftR` 12) + 0xE0
467 poke8 ptr (m+1) $ ((w `shiftR` 6) .&. 0x3F) + 0x80
467 poke8 ptr (m+1) $ ((w `shiftR` 6) .&. 0x3F) + 0x80
468 poke8 ptr (m+2) $ (w .&. 0x3F) + 0x80
468 poke8 ptr (m+2) $ (w .&. 0x3F) + 0x80