Skip to content

Commit 19725eb

Browse files
BodigrimLysxia
authored andcommitted
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)
1 parent 495f013 commit 19725eb

File tree

1 file changed

+6
-5
lines changed

1 file changed

+6
-5
lines changed

src/Data/Text/IO/Utf8.hs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,16 @@ module Data.Text.IO.Utf8
2525
, putStrLn
2626
) where
2727

28-
import Prelude hiding (readFile, writeFile, appendFile, interact, getContents, getLine, putStr, putStrLn)
28+
import Prelude ()
2929
import Control.Exception (evaluate)
30-
import Control.Monad ((<=<))
30+
import Control.Monad ((<=<), (=<<))
3131
import Data.ByteString (ByteString)
3232
import qualified Data.ByteString.Char8 as B
33+
import Data.Function ((.))
3334
import Data.Text (Text)
3435
import Data.Text.Encoding (decodeUtf8, encodeUtf8)
3536
import GHC.IO.Handle (Handle)
36-
import qualified Data.ByteString.Char8 as B.Char8
37+
import System.IO (IO, FilePath)
3738

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

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

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

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

0 commit comments

Comments
 (0)