Browse files

Early lazy I/O support.

--HG--
extra : convert_revision : 645a0e8abf2391dadebc155b81056309f9f74651
  • Loading branch information...
1 parent 7728ce8 commit 527b021df02b9e4d45ecef6e40b11e47ec663054 @bos committed Dec 15, 2009
Showing with 141 additions and 1 deletion.
  1. +1 −1 Data/Text/IO.hs
  2. +139 −0 Data/Text/Lazy/IO.hs
  3. +1 −0 text.cabal
View
2 Data/Text/IO.hs
@@ -37,7 +37,7 @@ import Prelude hiding (appendFile, getContents, getLine, interact, putStr,
import System.IO (Handle, IOMode(..), hPutChar, openFile, stdin, stdout,
withFile)
#if __GLASGOW_HASKELL__ <= 610
-import qualified Data.ByteString as B
+import qualified Data.ByteString.Char8 as B
import Data.Text.Encoding (decodeUtf8, encodeUtf8)
#else
import Control.Exception (throw)
View
139 Data/Text/Lazy/IO.hs
@@ -0,0 +1,139 @@
+{-# LANGUAGE BangPatterns, CPP, RecordWildCards #-}
+-- |
+-- Module : Data.Text.Lazy.IO
+-- Copyright : (c) Bryan O'Sullivan 2009,
+-- (c) Simon Marlow 2009
+-- License : BSD-style
+-- Maintainer : bos@serpentine.com
+-- Stability : experimental
+-- Portability : GHC
+--
+-- Efficient locale-sensitive support for text I\/O.
+
+module Data.Text.Lazy.IO
+ (
+ -- * Locale support
+ -- $locale
+ -- * File-at-a-time operations
+ readFile
+ , writeFile
+ , appendFile
+ -- * Operations on handles
+ , hGetContents
+ , hGetLine
+ , hPutStr
+ , hPutStrLn
+ -- * Special cases for standard input and output
+ , interact
+ , getContents
+ , getLine
+ , putStr
+ , putStrLn
+ ) where
+
+import Data.Text.Lazy (Text)
+import Prelude hiding (appendFile, getContents, getLine, interact, putStr,
+ putStrLn, readFile, writeFile)
+import System.IO (Handle, IOMode(..), hPutChar, openFile, stdin, stdout,
+ withFile)
+#if __GLASGOW_HASKELL__ <= 610
+import Data.Text.Lazy.Encoding (decodeUtf8, encodeUtf8)
+import qualified Data.ByteString.Char8 as S8
+import qualified Data.ByteString.Lazy.Char8 as L8
+#else
+#endif
+
+-- | The 'readFile' function reads a file and returns the contents of
+-- the file as a string. The entire file is read strictly, as with
+-- 'getContents'.
+readFile :: FilePath -> IO Text
+readFile name = openFile name ReadMode >>= hGetContents
+
+-- | Write a string to a file. The file is truncated to zero length
+-- before writing begins.
+writeFile :: FilePath -> Text -> IO ()
+writeFile p = withFile p WriteMode . flip hPutStr
+
+-- | Write a string the end of a file.
+appendFile :: FilePath -> Text -> IO ()
+appendFile p = withFile p AppendMode . flip hPutStr
+
+hGetContents :: Handle -> IO Text
+#if __GLASGOW_HASKELL__ <= 610
+hGetContents = fmap decodeUtf8 . L8.hGetContents
+#else
+hGetContents = undefined
+#endif
+
+-- | Read a single line from a handle.
+hGetLine :: Handle -> IO Text
+#if __GLASGOW_HASKELL__ <= 610
+hGetLine = fmap decodeUtf8 . S8.hGetLine
+#else
+hGetLine h = undefined
+#endif
+
+-- | Write a string to a handle.
+hPutStr :: Handle -> Text -> IO ()
+#if __GLASGOW_HASKELL__ <= 610
+hPutStr h = L8.hPut h . encodeUtf8
+#else
+hPutStr h = undefined
+#endif
+
+-- | Write a string to a handle, followed by a newline.
+hPutStrLn :: Handle -> Text -> IO ()
+hPutStrLn h t = hPutStr h t >> hPutChar h '\n'
+
+-- | The 'interact' function takes a function of type @Text -> Text@
+-- as its argument. The entire input from the standard input device is
+-- passed to this function as its argument, and the resulting string
+-- is output on the standard output device.
+interact :: (Text -> Text) -> IO ()
+interact f = putStr . f =<< getContents
+
+-- | Read all user input on 'stdin' as a single string.
+getContents :: IO Text
+getContents = hGetContents stdin
+
+-- | Read a single line of user input from 'stdin'.
+getLine :: IO Text
+getLine = hGetLine stdin
+
+-- | Write a string to 'stdout'.
+putStr :: Text -> IO ()
+putStr = hPutStr stdout
+
+-- | Write a string to 'stdout', followed by a newline.
+putStrLn :: Text -> IO ()
+putStrLn = hPutStrLn stdout
+
+-- $locale
+--
+-- /Note/: The behaviour of functions in this module depends on the
+-- version of GHC you are using.
+--
+-- Beginning with GHC 6.12, text I\/O is performed using the system or
+-- handle's current locale and line ending conventions.
+--
+-- Under GHC 6.10 and earlier, the system I\/O libraries /do not
+-- support/ locale-sensitive I\/O or line ending conversion. On these
+-- versions of GHC, functions in this library all use UTF-8. What
+-- does this mean in practice?
+--
+-- * All data that is read will be decoded as UTF-8.
+--
+-- * Before data is written, it is first encoded as UTF-8.
+--
+-- * On both reading and writing, the platform's native newline
+-- conversion is performed.
+--
+-- If you must use a non-UTF-8 locale on an older version of GHC, you
+-- will have to perform the transcoding yourself, e.g. as follows:
+--
+-- > import qualified Data.ByteString.Lazy as B
+-- > import Data.Text.Lazy (Text)
+-- > import Data.Text.Lazy.Encoding (encodeUtf16)
+-- >
+-- > putStr_Utf16LE :: Text -> IO ()
+-- > putStr_Utf16LE t = B.putStr (encodeUtf16LE t)
View
1 text.cabal
@@ -41,6 +41,7 @@ library
Data.Text.IO
Data.Text.Lazy
Data.Text.Lazy.Encoding
+ Data.Text.Lazy.IO
other-modules:
Data.Text.Array
Data.Text.Encoding.Fusion

0 comments on commit 527b021

Please sign in to comment.