Partially Revert "Data.ByteString.Char8 uses IO functions exported from Data.ByteString"
authorBen Gamari <ben@smart-cactus.org>
Thu, 9 Feb 2017 00:27:59 +0000 (19:27 -0500)
committerBen Gamari <ben@smart-cactus.org>
Thu, 9 Feb 2017 15:31:17 +0000 (10:31 -0500)
While several of these functions are indeed exported from Data.ByteString, they
are justifiably marked as deprecated there. Ultimately it is the Data.ByteString
functions that ought to be removed, not those in Data.ByteString.Char8. Namely,
hPutStrLn and putStrLn rightly belong in Data.ByteString.Char8.

This partially reverts commit 2fba115881b2da32ce076c1114ad6fe8d893086b.

Data/ByteString/Char8.hs

index f548a5e..9b0793c 100644 (file)
@@ -250,8 +250,8 @@ import Data.ByteString (empty,null,length,tail,init,append
                        ,getLine, getContents, putStr, interact
                        ,readFile, writeFile, appendFile
                        ,hGetContents, hGet, hGetSome, hPut, hPutStr
-                       ,hPutStrLn, putStrLn, hGetLine, hGetNonBlocking
-                       ,hPutNonBlocking, packCString, packCStringLen
+                       ,hGetLine, hGetNonBlocking, hPutNonBlocking
+                       ,packCString,packCStringLen
                        ,useAsCString,useAsCStringLen
                        )
 
@@ -264,6 +264,7 @@ import GHC.Char (eqChar)
 #endif
 import qualified Data.List as List (intersperse)
 
+import System.IO    (Handle,stdout)
 import Foreign
 
 
@@ -963,3 +964,16 @@ readInteger as
 
           combine2 b (n:m:ns) = let t = m*b + n in t `seq` (t : combine2 b ns)
           combine2 _ ns       = ns
+
+------------------------------------------------------------------------
+-- For non-binary text processing:
+
+-- | Write a ByteString to a handle, appending a newline byte
+hPutStrLn :: Handle -> ByteString -> IO ()
+hPutStrLn h ps
+    | length ps < 1024 = hPut h (ps `B.snoc` 0x0a)
+    | otherwise        = hPut h ps >> hPut h (B.singleton 0x0a) -- don't copy
+
+-- | Write a ByteString to stdout, appending a newline byte
+putStrLn :: ByteString -> IO ()
+putStrLn = hPutStrLn stdout