@@ -7,11 +7,18 @@
77-- Portability : GHC
88--
99-- Support for text I\/O.
10+--
1011
1112module Data.Text.IO
1213 (
14+ -- * Locale support
15+ -- $locale
16+ -- * File-at-a-time operations
17+ readFile
18+ , writeFile
19+ , appendFile
1320 -- * Operations on handles
14− hGetContents
21+ , hGetContents
1522 , hGetLine
1623 , hPutStr
1724 , hPutStrLn
@@ -24,32 +31,72 @@ module Data.Text.IO
2431 ) where
2532
2633import Data.Text (Text)
27−import Prelude hiding (getContents, getLine, interact, putStr, putStrLn)
28−import System.IO (Handle, stdin, stdout)
34+import Prelude hiding (appendFile, getContents, getLine, interact, putStr,
35+ putStrLn, readFile, writeFile)
36+import System.IO (Handle, hPutChar, stdin, stdout)
37+import qualified Data.ByteString as B
38+import Data.Text.Encoding (decodeUtf8, encodeUtf8)
39+
40+-- | The 'readFile' function reads a file and returns the contents of
41+-- the file as a string. The entire file is read strictly, as with
42+-- 'getContents'.
43+readFile :: FilePath -> IO Text
44+readFile = fmap decodeUtf8 . B.readFile
45+
46+-- | Write a string to a file. The file is truncated to zero length
47+-- before writing begins.
48+writeFile :: FilePath -> Text -> IO ()
49+writeFile p = B.writeFile p . encodeUtf8
50+
51+-- | Write a string the end of a file.
52+appendFile :: FilePath -> Text -> IO ()
53+appendFile p = B.appendFile p . encodeUtf8
2954
55+-- | Read the remaining contents of a handle as a string.
3056hGetContents :: Handle -> IO Text
31−hGetContents h = undefined
57+hGetContents = fmap decodeUtf8 . B.hGetContents
3258
59+-- | Read a single line from a handle.
3360hGetLine :: Handle -> IO Text
34−hGetLine h = undefined
61+hGetLine = fmap decodeUtf8 . B.hGetLine
3562
63+-- | Write a string to a handle.
3664hPutStr :: Handle -> Text -> IO ()
37−hPutStr h t = undefined
65+hPutStr h = B.hPutStr h . encodeUtf8
3866
67+-- | Write a string to a handle, followed by a newline.
3968hPutStrLn :: Handle -> Text -> IO ()
40−hPutStrLn h t = undefined
69+hPutStrLn h t = B.hPutStrLn h (encodeUtf8 t) >> hPutChar h '\n'
4170
71+-- | The 'interact' function takes a function of type @Text -> Text@
72+-- as its argument. The entire input from the standard input device is
73+-- passed to this function as its argument, and the resulting string
74+-- is output on the standard output device.
4275interact :: (Text -> Text) -> IO ()
43−interact f = undefined
76+interact f = B.interact (encodeUtf8 . f . decodeUtf8)
4477
78+-- | Read all user input on 'stdin' as a single string.
4579getContents :: IO Text
4680getContents = hGetContents stdin
4781
82+-- | Read a single line of user input from 'stdin'.
4883getLine :: IO Text
4984getLine = hGetLine stdin
5085
86+-- | Write a string to 'stdout'.
5187putStr :: Text -> IO ()
5288putStr = hPutStr stdout
5389
90+-- | Write a string to 'stdout', followed by a newline.
5491putStrLn :: Text -> IO ()
5592putStrLn = hPutStrLn stdout
93+
94+-- $locale
95+--
96+-- Under GHC 6.10 and earlier, the system I\/O libraries do not
97+-- support locale-sensitive I\/O. All data read by functions in this
98+-- module is decoded as UTF-8, and before data is written, it is first
99+-- encoded as UTF-8.
100+--
101+-- Beginning with GHC 6.12, text I\/O is performed using the system or
102+-- handle's current locale.