Commits

Anonymous committed a2719dc

implement 'encodeUtf8Builder' using 'encodeUtf8Escaped'

  • Participants
  • Parent commits 2b0ee45

Comments (0)

Files changed (1)

File Data/Text/Encoding.hs

 import Data.ByteString.Internal as B
 import Data.ByteString.Lazy.Builder.Internal as B
 import qualified Data.ByteString.Lazy.Builder.BasicEncoding.Internal as B
+import qualified Data.ByteString.Lazy.Builder.BasicEncoding          as B
 import Data.Text.Encoding.Error (OnDecodeError, UnicodeException, strictDecode)
 import Data.Text.Internal (Text(..), textP)
 import Data.Text.UnsafeChar (ord, unsafeWrite)
 
 -- | Encode text using UTF-8 encoding.
 encodeUtf8Builder :: Text -> B.Builder
-encodeUtf8Builder (Text arr off len) =
-    B.builder step
-  where
-    bound   = 4
-    iend    = off + len
-    step !k =
-        outerLoop off
-      where
-        outerLoop !i0 !br@(BufferRange op0 ope)
-          | i0 >= iend                = k br
-          | op0 `plusPtr` bound < ope =
-              goPartial (i0 + min outRemaining inpRemaining)
-          | otherwise  = return $ bufferFull bound op0 (outerLoop i0)
-          where
-            outRemaining = (ope `minusPtr` op0) `div` bound
-            inpRemaining = iend - i0
-
-            goPartial !iendTmp = go i0 op0
-              where
-                go !i !op
-                  | i < iendTmp = case A.unsafeIndex arr i of
-                      w | w <= 0x7F -> do
-                            poke8 0 w
-                            go (i + 1) (op `plusPtr` 1)
-                        | w <= 0x7FF -> do
-                            poke8 0 $ (w `shiftR` 6) + 0xC0
-                            poke8 1 $ (w .&. 0x3f) + 0x80
-                            go (i + 1) (op `plusPtr` 2)
-                        | 0xD800 <= w && w <= 0xDBFF -> do
-                            let c = ord $ U16.chr2 w (A.unsafeIndex arr (i+1))
-                            poke8 0 $ (c `shiftR` 18) + 0xF0
-                            poke8 1 $ ((c `shiftR` 12) .&. 0x3F) + 0x80
-                            poke8 2 $ ((c `shiftR` 6) .&. 0x3F) + 0x80
-                            poke8 3 $ (c .&. 0x3F) + 0x80
-                            go (i + 2) (op `plusPtr` 4)
-                        | otherwise -> do
-                            poke8 0 $ (w `shiftR` 12) + 0xE0
-                            poke8 1 $ ((w `shiftR` 6) .&. 0x3F) + 0x80
-                            poke8 2 $ (w .&. 0x3F) + 0x80
-                            go (i + 1) (op `plusPtr` 3)
-                  | otherwise =
-                      outerLoop i (BufferRange op ope)
-                  where
-                    poke8 j v = poke (op `plusPtr` j) (fromIntegral v :: Word8)
+encodeUtf8Builder = encodeUtf8Escaped (B.fromF B.word8)
 
 -- | Encode text using UTF-8 encoding and escape the ASCII characters using
 -- a 'BoundedEncoding'.