Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Data.Text.IO.Utf8: use B.putStrLn instead of B.putStr t >> B.putStr "\n" #579

Merged
merged 1 commit into from
Apr 12, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Data.Text.IO.Utf8: use B.putStrLn instead of B.putStr t >> B.putStr "\n"
This is not just a stylistic change: it also improves atomicity of putStrLn
in concurrent environment, when multiple threads attempt to execute it at once.
See https://www.snoyman.com/blog/2016/11/haskells-missing-concurrency-basics/

(Now B.putStrLn is not perfect either, but that's the problem to solve in bytestring)
Bodigrim committed Apr 11, 2024
commit b32f8d95f85dc90e85c166dfde870bb081b436c2
11 changes: 6 additions & 5 deletions src/Data/Text/IO/Utf8.hs
Original file line number Diff line number Diff line change
@@ -25,15 +25,16 @@ module Data.Text.IO.Utf8
, putStrLn
) where

import Prelude hiding (readFile, writeFile, appendFile, interact, getContents, getLine, putStr, putStrLn)
import Prelude ()
import Control.Exception (evaluate)
import Control.Monad ((<=<))
import Control.Monad ((<=<), (=<<))
import Data.ByteString (ByteString)
import qualified Data.ByteString.Char8 as B
import Data.Function ((.))
import Data.Text (Text)
import Data.Text.Encoding (decodeUtf8, encodeUtf8)
import GHC.IO.Handle (Handle)
import qualified Data.ByteString.Char8 as B.Char8
import System.IO (IO, FilePath)

decodeUtf8IO :: ByteString -> IO Text
decodeUtf8IO = evaluate . decodeUtf8
@@ -67,7 +68,7 @@ hPutStr h = B.hPutStr h . encodeUtf8

-- | Write a string to a handle, followed by a newline.
hPutStrLn :: Handle -> Text -> IO ()
hPutStrLn h t = hPutStr h t >> B.hPutStr h (B.Char8.singleton '\n')
hPutStrLn h = B.hPutStrLn h . encodeUtf8

-- | The 'interact' function takes a function of type @Text -> Text@
-- as its argument. The entire input from the standard input device is
@@ -90,4 +91,4 @@ putStr = B.putStr . encodeUtf8

-- | Write a string to 'stdout', followed by a newline.
putStrLn :: Text -> IO ()
putStrLn t = B.putStr (encodeUtf8 t) >> B.putStr (B.Char8.singleton '\n')
putStrLn = B.putStrLn . encodeUtf8