@@ -39,67 +39,65 @@ where
3939
4040import qualified Data.ByteString.Char8 as B
4141import qualified Data.ByteString.Lazy.Char8 as BL
42+import Data.Text (Text)
4243import qualified Data.Text as T
44+import qualified Data.Text.IO as TIO
4345import qualified Data.Text.Encoding as T
4446import qualified Data.Text.Lazy as TL
4547import qualified Data.Text.Lazy.Encoding as TL
4648import Prelude hiding (getContents, putStr, putStrLn, readFile, writeFile)
4749import System.IO hiding (getContents, hGetContents, hPutStr, hPutStrLn, putStr,
4850 putStrLn, readFile, writeFile)
49−import qualified System.IO as IO
5051
51−readFile :: FilePath -> IO String
52+readFile :: FilePath -> IO Text
5253readFile f = do
5354 h <- openFile (encodePath f) ReadMode
5455 hGetContents h
5556
56−getContents :: IO String
57+getContents :: IO Text
5758getContents = hGetContents stdin
5859
59−writeFileWith :: Newline -> FilePath -> String -> IO ()
60+writeFileWith :: Newline -> FilePath -> Text -> IO ()
6061writeFileWith eol f s =
6162 withFile (encodePath f) WriteMode $ \h -> hPutStrWith eol h s
6263
63−writeFile :: FilePath -> String -> IO ()
64+writeFile :: FilePath -> Text -> IO ()
6465writeFile = writeFileWith nativeNewline
6566
66−putStrWith :: Newline -> String -> IO ()
67+putStrWith :: Newline -> Text -> IO ()
6768putStrWith eol s = hPutStrWith eol stdout s
6869
69−putStr :: String -> IO ()
70+putStr :: Text -> IO ()
7071putStr = putStrWith nativeNewline
7172
72−putStrLnWith :: Newline -> String -> IO ()
73+putStrLnWith :: Newline -> Text -> IO ()
7374putStrLnWith eol s = hPutStrLnWith eol stdout s
7475
75−putStrLn :: String -> IO ()
76+putStrLn :: Text -> IO ()
7677putStrLn = putStrLnWith nativeNewline
7778
78−hPutStrWith :: Newline -> Handle -> String -> IO ()
79+hPutStrWith :: Newline -> Handle -> Text -> IO ()
7980hPutStrWith eol h s =
8081 hSetNewlineMode h (NewlineMode eol eol) >>
81− hSetEncoding h utf8 >> IO.hPutStr h s
82+ hSetEncoding h utf8 >> TIO.hPutStr h s
8283
83−hPutStr :: Handle -> String -> IO ()
84+hPutStr :: Handle -> Text -> IO ()
8485hPutStr = hPutStrWith nativeNewline
8586
86−hPutStrLnWith :: Newline -> Handle -> String -> IO ()
87+hPutStrLnWith :: Newline -> Handle -> Text -> IO ()
8788hPutStrLnWith eol h s =
8889 hSetNewlineMode h (NewlineMode eol eol) >>
89− hSetEncoding h utf8 >> IO.hPutStrLn h s
90+ hSetEncoding h utf8 >> TIO.hPutStrLn h s
9091
91−hPutStrLn :: Handle -> String -> IO ()
92+hPutStrLn :: Handle -> Text -> IO ()
9293hPutStrLn = hPutStrLnWith nativeNewline
9394
94−hGetContents :: Handle -> IO String
95−hGetContents = fmap toString . B.hGetContents
96−-- hGetContents h = hSetEncoding h utf8_bom
97−-- >> hSetNewlineMode h universalNewlineMode
98−-- >> IO.hGetContents h
95+hGetContents :: Handle -> IO Text
96+hGetContents = fmap toText . B.hGetContents
9997
10098-- | Convert UTF8-encoded ByteString to Text, also
10199-- removing '\r' characters.
102−toText :: B.ByteString -> T.Text
100+toText :: B.ByteString -> Text
103101toText = T.decodeUtf8 . filterCRs . dropBOM
104102 where dropBOM bs =
105103 if "\xEF\xBB\xBF" `B.isPrefixOf` bs
@@ -127,7 +125,7 @@ toTextLazy = TL.decodeUtf8 . filterCRs . dropBOM
127125toStringLazy :: BL.ByteString -> String
128126toStringLazy = TL.unpack . toTextLazy
129127
130−fromText :: T.Text -> B.ByteString
128+fromText :: Text -> B.ByteString
131129fromText = T.encodeUtf8
132130
133131fromTextLazy :: TL.Text -> BL.ByteString