@@ -64,6 +64,7 @@ import Data.ByteString as B
6464import Data.ByteString.Internal as B
6565import Data.ByteString.Lazy.Builder.Internal as B
6666import qualified Data.ByteString.Lazy.Builder.BasicEncoding.Internal as B
67+import qualified Data.ByteString.Lazy.Builder.BasicEncoding as B
6768import Data.Text.Encoding.Error (OnDecodeError, UnicodeException, strictDecode)
6869import Data.Text.Internal (Text(..), textP)
6970import Data.Text.UnsafeChar (ord, unsafeWrite)
@@ -315,50 +316,7 @@ encodeTextWithB be = encodeStreamWithB be . F.stream
315316
316317-- | Encode text using UTF-8 encoding.
317318encodeUtf8Builder :: Text -> B.Builder
318−encodeUtf8Builder (Text arr off len) =
319− B.builder step
320− where
321− bound = 4
322− iend = off + len
323− step !k =
324− outerLoop off
325− where
326− outerLoop !i0 !br@(BufferRange op0 ope)
327− | i0 >= iend = k br
328− | op0 `plusPtr` bound < ope =
329− goPartial (i0 + min outRemaining inpRemaining)
330− | otherwise = return $ bufferFull bound op0 (outerLoop i0)
331− where
332− outRemaining = (ope `minusPtr` op0) `div` bound
333− inpRemaining = iend - i0
334−
335− goPartial !iendTmp = go i0 op0
336− where
337− go !i !op
338− | i < iendTmp = case A.unsafeIndex arr i of
339− w | w <= 0x7F -> do
340− poke8 0 w
341− go (i + 1) (op `plusPtr` 1)
342− | w <= 0x7FF -> do
343− poke8 0 $ (w `shiftR` 6) + 0xC0
344− poke8 1 $ (w .&. 0x3f) + 0x80
345− go (i + 1) (op `plusPtr` 2)
346− | 0xD800 <= w && w <= 0xDBFF -> do
347− let c = ord $ U16.chr2 w (A.unsafeIndex arr (i+1))
348− poke8 0 $ (c `shiftR` 18) + 0xF0
349− poke8 1 $ ((c `shiftR` 12) .&. 0x3F) + 0x80
350− poke8 2 $ ((c `shiftR` 6) .&. 0x3F) + 0x80
351− poke8 3 $ (c .&. 0x3F) + 0x80
352− go (i + 2) (op `plusPtr` 4)
353− | otherwise -> do
354− poke8 0 $ (w `shiftR` 12) + 0xE0
355− poke8 1 $ ((w `shiftR` 6) .&. 0x3F) + 0x80
356− poke8 2 $ (w .&. 0x3F) + 0x80
357− go (i + 1) (op `plusPtr` 3)
358− | otherwise =
359− outerLoop i (BufferRange op ope)
360− where
361− poke8 j v = poke (op `plusPtr` j) (fromIntegral v :: Word8)
319+encodeUtf8Builder = encodeUtf8Escaped (B.fromF B.word8)
362320
363321-- | Encode text using UTF-8 encoding and escape the ASCII characters using
364322-- a 'BoundedEncoding'.