on master help sign in
Flesh out IO functions for 6.10 and older.
9474e21Bryan O'Sullivan
Data/Text/IO.hs+55−8
@@ -7,11 +7,18 @@
7-- Portability : GHC
7-- Portability : GHC
8--
8--
9-- Support for text I\/O.
9-- Support for text I\/O.
10+--
10
11
11module Data.Text.IO
12module Data.Text.IO
12 (
13 (
14+ -- * Locale support
15+ -- $locale
16+ -- * File-at-a-time operations
17+ readFile
18+ , writeFile
19+ , appendFile
13 -- * Operations on handles
20 -- * Operations on handles
14− hGetContents
21+ , hGetContents
15 , hGetLine
22 , hGetLine
16 , hPutStr
23 , hPutStr
17 , hPutStrLn
24 , hPutStrLn
@@ -24,32 +31,72 @@ module Data.Text.IO
24 ) where
31 ) where
25
32
26import Data.Text (Text)
33import Data.Text (Text)
27−import Prelude hiding (getContents, getLine, interact, putStr, putStrLn)
34+import Prelude hiding (appendFile, getContents, getLine, interact, putStr,
35+ putStrLn, readFile, writeFile)
28−import System.IO (Handle, stdin, stdout)
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
29
54
55+-- | Read the remaining contents of a handle as a string.
30hGetContents :: Handle -> IO Text
56hGetContents :: Handle -> IO Text
31−hGetContents h = undefined
57+hGetContents = fmap decodeUtf8 . B.hGetContents
32
58
59+-- | Read a single line from a handle.
33hGetLine :: Handle -> IO Text
60hGetLine :: Handle -> IO Text
34−hGetLine h = undefined
61+hGetLine = fmap decodeUtf8 . B.hGetLine
35
62
63+-- | Write a string to a handle.
36hPutStr :: Handle -> Text -> IO ()
64hPutStr :: Handle -> Text -> IO ()
37−hPutStr h t = undefined
65+hPutStr h = B.hPutStr h . encodeUtf8
38
66
67+-- | Write a string to a handle, followed by a newline.
39hPutStrLn :: Handle -> Text -> IO ()
68hPutStrLn :: Handle -> Text -> IO ()
40−hPutStrLn h t = undefined
69+hPutStrLn h t = B.hPutStrLn h (encodeUtf8 t) >> hPutChar h '\n'
41
70
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.
42interact :: (Text -> Text) -> IO ()
75interact :: (Text -> Text) -> IO ()
43−interact f = undefined
76+interact f = B.interact (encodeUtf8 . f . decodeUtf8)
44
77
78+-- | Read all user input on 'stdin' as a single string.
45getContents :: IO Text
79getContents :: IO Text
46getContents = hGetContents stdin
80getContents = hGetContents stdin
47
81
82+-- | Read a single line of user input from 'stdin'.
48getLine :: IO Text
83getLine :: IO Text
49getLine = hGetLine stdin
84getLine = hGetLine stdin
50
85
86+-- | Write a string to 'stdout'.
51putStr :: Text -> IO ()
87putStr :: Text -> IO ()
52putStr = hPutStr stdout
88putStr = hPutStr stdout
53
89
90+-- | Write a string to 'stdout', followed by a newline.
54putStrLn :: Text -> IO ()
91putStrLn :: Text -> IO ()
55putStrLn = hPutStrLn stdout
92putStrLn = 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.