on master help sign in
Early lazy I/O support.

--HG-- extra : convert_revision : 645a0e8abf2391dadebc155b81056309f9f74651

commit527b021df02b9e4d45ecef6e40b11e47ec663054 authorBryan O'Sullivan <bos@serpentine.com> dateTue 15 Dec 2009, 00:21 (17 years ago) parent7728ce8
3 files changed+141−1
Data/Text/IO.hs+1−1 Data/Text/Lazy/IO.hs+139 text.cabal+1
Data/Text/IO.hs+1−1
@@ -37,7 +37,7 @@ import Prelude hiding (appendFile, getContents, getLine, interact, putStr,
3737import System.IO (Handle, IOMode(..), hPutChar, openFile, stdin, stdout,
3838 withFile)
3939#if __GLASGOW_HASKELL__ <= 610
40−import qualified Data.ByteString as B
40+import qualified Data.ByteString.Char8 as B
4141import Data.Text.Encoding (decodeUtf8, encodeUtf8)
4242#else
4343import Control.Exception (throw)
Data/Text/Lazy/IO.hsnew file+139
@@ -0,0 +1,139 @@
1+{-# LANGUAGE BangPatterns, CPP, RecordWildCards #-}
2+-- |
3+-- Module : Data.Text.Lazy.IO
4+-- Copyright : (c) Bryan O'Sullivan 2009,
5+-- (c) Simon Marlow 2009
6+-- License : BSD-style
7+-- Maintainer : bos@serpentine.com
8+-- Stability : experimental
9+-- Portability : GHC
10+--
11+-- Efficient locale-sensitive support for text I\/O.
12+
13+module Data.Text.Lazy.IO
14+ (
15+ -- * Locale support
16+ -- $locale
17+ -- * File-at-a-time operations
18+ readFile
19+ , writeFile
20+ , appendFile
21+ -- * Operations on handles
22+ , hGetContents
23+ , hGetLine
24+ , hPutStr
25+ , hPutStrLn
26+ -- * Special cases for standard input and output
27+ , interact
28+ , getContents
29+ , getLine
30+ , putStr
31+ , putStrLn
32+ ) where
33+
34+import Data.Text.Lazy (Text)
35+import Prelude hiding (appendFile, getContents, getLine, interact, putStr,
36+ putStrLn, readFile, writeFile)
37+import System.IO (Handle, IOMode(..), hPutChar, openFile, stdin, stdout,
38+ withFile)
39+#if __GLASGOW_HASKELL__ <= 610
40+import Data.Text.Lazy.Encoding (decodeUtf8, encodeUtf8)
41+import qualified Data.ByteString.Char8 as S8
42+import qualified Data.ByteString.Lazy.Char8 as L8
43+#else
44+#endif
45+
46+-- | The 'readFile' function reads a file and returns the contents of
47+-- the file as a string. The entire file is read strictly, as with
48+-- 'getContents'.
49+readFile :: FilePath -> IO Text
50+readFile name = openFile name ReadMode >>= hGetContents
51+
52+-- | Write a string to a file. The file is truncated to zero length
53+-- before writing begins.
54+writeFile :: FilePath -> Text -> IO ()
55+writeFile p = withFile p WriteMode . flip hPutStr
56+
57+-- | Write a string the end of a file.
58+appendFile :: FilePath -> Text -> IO ()
59+appendFile p = withFile p AppendMode . flip hPutStr
60+
61+hGetContents :: Handle -> IO Text
62+#if __GLASGOW_HASKELL__ <= 610
63+hGetContents = fmap decodeUtf8 . L8.hGetContents
64+#else
65+hGetContents = undefined
66+#endif
67+
68+-- | Read a single line from a handle.
69+hGetLine :: Handle -> IO Text
70+#if __GLASGOW_HASKELL__ <= 610
71+hGetLine = fmap decodeUtf8 . S8.hGetLine
72+#else
73+hGetLine h = undefined
74+#endif
75+
76+-- | Write a string to a handle.
77+hPutStr :: Handle -> Text -> IO ()
78+#if __GLASGOW_HASKELL__ <= 610
79+hPutStr h = L8.hPut h . encodeUtf8
80+#else
81+hPutStr h = undefined
82+#endif
83+
84+-- | Write a string to a handle, followed by a newline.
85+hPutStrLn :: Handle -> Text -> IO ()
86+hPutStrLn h t = hPutStr h t >> hPutChar h '\n'
87+
88+-- | The 'interact' function takes a function of type @Text -> Text@
89+-- as its argument. The entire input from the standard input device is
90+-- passed to this function as its argument, and the resulting string
91+-- is output on the standard output device.
92+interact :: (Text -> Text) -> IO ()
93+interact f = putStr . f =<< getContents
94+
95+-- | Read all user input on 'stdin' as a single string.
96+getContents :: IO Text
97+getContents = hGetContents stdin
98+
99+-- | Read a single line of user input from 'stdin'.
100+getLine :: IO Text
101+getLine = hGetLine stdin
102+
103+-- | Write a string to 'stdout'.
104+putStr :: Text -> IO ()
105+putStr = hPutStr stdout
106+
107+-- | Write a string to 'stdout', followed by a newline.
108+putStrLn :: Text -> IO ()
109+putStrLn = hPutStrLn stdout
110+
111+-- $locale
112+--
113+-- /Note/: The behaviour of functions in this module depends on the
114+-- version of GHC you are using.
115+--
116+-- Beginning with GHC 6.12, text I\/O is performed using the system or
117+-- handle's current locale and line ending conventions.
118+--
119+-- Under GHC 6.10 and earlier, the system I\/O libraries /do not
120+-- support/ locale-sensitive I\/O or line ending conversion. On these
121+-- versions of GHC, functions in this library all use UTF-8. What
122+-- does this mean in practice?
123+--
124+-- * All data that is read will be decoded as UTF-8.
125+--
126+-- * Before data is written, it is first encoded as UTF-8.
127+--
128+-- * On both reading and writing, the platform's native newline
129+-- conversion is performed.
130+--
131+-- If you must use a non-UTF-8 locale on an older version of GHC, you
132+-- will have to perform the transcoding yourself, e.g. as follows:
133+--
134+-- > import qualified Data.ByteString.Lazy as B
135+-- > import Data.Text.Lazy (Text)
136+-- > import Data.Text.Lazy.Encoding (encodeUtf16)
137+-- >
138+-- > putStr_Utf16LE :: Text -> IO ()
139+-- > putStr_Utf16LE t = B.putStr (encodeUtf16LE t)
text.cabal+1
@@ -41,6 +41,7 @@ library
4141 Data.Text.IO
4242 Data.Text.Lazy
4343 Data.Text.Lazy.Encoding
44+ Data.Text.Lazy.IO
4445 other-modules:
4546 Data.Text.Array
4647 Data.Text.Encoding.Fusion