-
Notifications
You must be signed in to change notification settings - Fork 157
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
19725eb
commit 80746c5
Showing
3 changed files
with
78 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
{-# LANGUAGE MagicHash #-} | ||
{-# LANGUAGE NamedFieldPuns #-} | ||
{-# LANGUAGE RecordWildCards #-} | ||
module Data.Text.Lazy.Builder.IO | ||
( hPutBuilder | ||
, hPutBuilderUtf8 | ||
) where | ||
|
||
import System.IO (hPutBuf) | ||
import Data.Text.Internal (Text(Text)) | ||
import GHC.IO.Handle.Types (Handle, Handle__(..), BufferMode(BlockBuffering)) | ||
import Data.Text.Internal.Builder (Builder, getTexts) | ||
import Data.Text.Array (newPinned) | ||
import Data.Array.Byte (ByteArray(ByteArray)) | ||
import GHC.IO.Handle.Internals (flushWriteBuffer, wantWritableHandle, flushWriteBuffer) | ||
import Data.Foldable (for_) | ||
import GHC.Exts (byteArrayContents#) | ||
import Control.Monad.ST (runST) | ||
import GHC.Ptr (Ptr(Ptr)) | ||
import GHC.IO.Encoding (textEncodingName) | ||
|
||
hPutBuilder :: Handle -> Builder -> IO () | ||
hPutBuilder h b = do | ||
(mode, nl, isUtf8) <- wantWritableHandle "hPutStr" h $ \(Handle__ {..}) -> do | ||
let isUtf8 = maybe False (("UTF-8" ==) . textEncodingName) haCodec | ||
return (haBufferMode, haOutputNL, isUtf8) | ||
case mode of | ||
--NoBuffering -> hPutChars h b | ||
--LineBuffering -> writeLines h nl buf b | ||
BlockBuffering _ | ||
-- | nl == CRLF -> writeBlocksCRLF h buf b | ||
| isUtf8 -> hPutBuilderUtf8 h b | ||
-- | otherwise -> writeBlocksRaw h buf b | ||
|
||
|
||
hPutBuilderUtf8 :: Handle -> Builder -> IO () | ||
hPutBuilderUtf8 h b = do | ||
flushBytes | ||
-- ????? Does the text ByteArray have a chance of being garbage collected before the flush finishes? | ||
for_ textBuffers $ \(Text (ByteArray a#) _ bufR) -> hPutBuf h (Ptr (byteArrayContents# a#)) bufR | ||
where | ||
flushBytes = wantWritableHandle "hPutBuilder" h flushWriteBuffer | ||
textBuffers = runST $ getTexts bufferSize newPinned b | ||
|
||
bufferSize :: Int | ||
bufferSize = 1024 -- I don't know what this should be |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters