@@ -64,7 +64,7 @@ import Data.Word (Word8)
64import Foreign.C.Types (CSize)
64import Foreign.C.Types (CSize)
65import Foreign.ForeignPtr (withForeignPtr)
65import Foreign.ForeignPtr (withForeignPtr)
66import Foreign.Marshal.Utils (with)
66import Foreign.Marshal.Utils (with)
67−import Foreign.Ptr (Ptr, plusPtr)
67+import Foreign.Ptr (Ptr, minusPtr, plusPtr)
68import Foreign.Storable (peek, poke)
68import Foreign.Storable (peek, poke)
69import GHC.Base (MutableByteArray#)
69import GHC.Base (MutableByteArray#)
70import System.IO.Unsafe (unsafePerformIO)
70import System.IO.Unsafe (unsafePerformIO)
@@ -173,13 +173,14 @@ encodeUtf8 (Text arr off len) = unsafePerformIO $ do
173 -- A single ASCII octet is likely to start a run of
173 -- A single ASCII octet is likely to start a run of
174 -- them. We see better performance when we
174 -- them. We see better performance when we
175 -- special-case this assumption.
175 -- special-case this assumption.
176+ let end = ptr `plusPtr` size
176− let ascii !t !u
177+ ascii !t !u
177− | t == offLen || u == size || v >= 0x80 = go t u
178+ | t == offLen || u == end || v >= 0x80 = go t (u `minusPtr` ptr)
178 | otherwise = do
179 | otherwise = do
179− poke (ptr `plusPtr` u) (fromIntegral v :: Word8)
180+ poke u (fromIntegral v :: Word8)
180− ascii (t+1) (u+1)
181+ ascii (t+1) (u `plusPtr` 1)
181 where v = A.unsafeIndex arr t
182 where v = A.unsafeIndex arr t
182− ascii (n+1) (m+1)
183+ ascii (n+1) (ptr `plusPtr` (m+1))
183 | w <= 0x7FF -> ensure 2 $ do
184 | w <= 0x7FF -> ensure 2 $ do
184 poke8 m $ (w `shiftR` 6) + 0xC0
185 poke8 m $ (w `shiftR` 6) + 0xC0
185 poke8 (m+1) $ (w .&. 0x3f) + 0x80
186 poke8 (m+1) $ (w .&. 0x3f) + 0x80