@@ -84,10 +84,10 @@ module Data.ByteString.Builder.Internal (
8484 -- , sizedChunksInsert
8585
8686 , byteStringCopy
87+ , ascLiteralCopy
88+ , modUtf8LitCopy
8789 , byteStringInsert
8890 , byteStringThreshold
89- , cstringLiteral
90- , cstringUtf8Literal
9191
9292 , lazyByteStringCopy
9393 , lazyByteStringInsert
@@ -872,62 +872,52 @@ byteStringInsert =
872872-- @0xC0 0x80@) null characters.
873873--
874874-- @since 0.11.5.0
875- {-# INLINABLE cstringLiteral #-}
876- cstringLiteral :: Addr # -> Builder
877- cstringLiteral = \ addr -> builder $ \ k br -> do
878- let ip = Ptr addr
879- #if __GLASGOW_HASKELL__ >= 811
880- ipe = Ptr (addr `plusAddr# ` (cstringLength# addr))
881- #else
882- ! ipe <- plusPtr ip . fromIntegral <$> S. c_strlen ip
883- #endif
875+ {-# INLINABLE ascLiteralCopy #-}
876+ ascLiteralCopy :: Ptr Word8 -> Int -> Builder
877+ ascLiteralCopy = \ ! ip ! len -> builder $ \ k br -> do
878+ let ! ipe = ip `plusPtr` len
884879 wrappedBytesCopyStep (BufferRange ip ipe) k br
885880
886881-- | Builder for raw 'Addr#' pointers to null-terminated primitive UTF-8
887882-- encoded strings that may contain embedded overlong-encodings (as the
888883-- two-byte sequence @0xC0 0x80@) of null characters.
889884--
890885-- @since 0.11.5.0
891- {-# INLINABLE cstringUtf8Literal #-}
892- cstringUtf8Literal :: Addr # -> Builder
893- cstringUtf8Literal = \ addr0 -> builder $ \ k br -> do
894- #if __GLASGOW_HASKELL__ >= 811
895- let len = I # (cstringLength# addr0)
896- #else
897- len <- fromIntegral <$> S. c_strlen (Ptr addr0)
898- #endif
899- nullAt <- c_strstr (Ptr addr0) (Ptr " \xc0\x80 " # )
900- cstringUtf8_step (Ptr addr0) len nullAt k br
901-
902- cstringUtf8_step :: Ptr Word8 -> Int -> Ptr Word8 -> BuildStep r -> BuildStep r
903- cstringUtf8_step ! ip ! len ((== nullPtr) -> True ) k br =
886+ {-# INLINABLE modUtf8LitCopy #-}
887+ modUtf8LitCopy :: Ptr Word8 -> Int -> Builder
888+ modUtf8LitCopy = \ ! ip ! len -> builder $ \ k br -> do
889+ nullAt <- c_strstr (castPtr ip) (Ptr " \xc0\x80 " # )
890+ modUtf8_step ip len nullAt k br
891+
892+ modUtf8_step :: Ptr Word8 -> Int -> Ptr Word8 -> BuildStep r -> BuildStep r
893+ modUtf8_step ! ip ! len ((== nullPtr) -> True ) k br =
904894 -- Contains no encoded nulls, use simple copy codepath
905895 wrappedBytesCopyStep (BufferRange ip ipe) k br
906896 where
907897 ! ipe = ip `plusPtr` len
908- cstringUtf8_step ! ip ! len ! nullAt k (BufferRange op0 ope)
898+ modUtf8_step ! ip ! len ! nullAt k (BufferRange op0 ope)
909899 -- Copy as much of the null-free portion of the string as fits into the
910900 -- available buffer space. If the string is long enough, we may have asked
911901 -- for less than its full length, filling the buffer with the rest will go
912902 -- into the next builder step.
913903 | avail > nullFree = do
914- when (nullFree > 0 ) (S. memcpy op0 ip nullFree)
904+ when (nullFree > 0 ) (copyBytes op0 ip nullFree)
915905 pokeElemOff op0 nullFree 0
916- let ! op' = op0 `plusPtr` ( nullFree + 1 )
917- nread = nullFree + 2
918- ! ip' = ip `plusPtr` nread
919- len ' = len - nread
906+ let used = nullFree + 2
907+ len' = len - used
908+ ! ip' = ip `plusPtr` used
909+ ! op ' = op0 `plusPtr` (nullFree + 1 )
920910 nullAt' <- c_strstr ip' (Ptr " \xc0\x80 " # )
921- cstringUtf8_step ip' len' nullAt' k (BufferRange op' ope)
911+ modUtf8_step ip' len' nullAt' k (BufferRange op' ope)
922912 | avail > 0 = do
923913 -- avail <= nullFree
924- S. memcpy op0 ip avail
925- let ! op ' = op0 `plusPtr` avail
914+ copyBytes op0 ip avail
915+ let len ' = len - avail
926916 ! ip' = ip `plusPtr` avail
927- len ' = len - avail
928- return $ bufferFull 1 op' (cstringUtf8_step ip' len' nullAt k)
917+ ! op ' = op0 `plusPtr` avail
918+ return $ bufferFull 1 op' (modUtf8_step ip' len' nullAt k)
929919 | otherwise =
930- return $ bufferFull 1 op0 (cstringUtf8_step ip len nullAt k)
920+ return $ bufferFull 1 op0 (modUtf8_step ip len nullAt k)
931921 where
932922 ! avail = ope `minusPtr` op0
933923 ! nullFree = nullAt `minusPtr` ip
0 commit comments