mirror of
https://github.com/serokell/haskell-with-utf8.git
synced 2024-09-11 06:45:36 +03:00
Add lazy versions of Text functions
We had `readFile` and `writeFile` fot strict `Text`, but not for lazy. Add lazy versions of the same functions, basically, by copy-pasting.
This commit is contained in:
parent
5b98b490bc
commit
83f7b5967e
@ -3,7 +3,7 @@
|
||||
- SPDX-License-Identifier: MPL-2.0
|
||||
-}
|
||||
|
||||
-- | Data Text IO for the modern world.
|
||||
-- | "Data.Text.IO" for the modern world.
|
||||
--
|
||||
-- Wrappers around simple file reading/writing functions from the
|
||||
-- @text@ package that reset the handle encoding to UTF-8.
|
||||
|
35
lib/Data/Text/Lazy/IO/Utf8.hs
Normal file
35
lib/Data/Text/Lazy/IO/Utf8.hs
Normal file
@ -0,0 +1,35 @@
|
||||
{- SPDX-FileCopyrightText: 2020 Serokell <https://serokell.io/>
|
||||
-
|
||||
- SPDX-License-Identifier: MPL-2.0
|
||||
-}
|
||||
|
||||
-- | "Data.Text.Lazy.IO" for the modern world.
|
||||
--
|
||||
-- Wrappers around simple file reading/writing functions from the
|
||||
-- @text@ package that reset the handle encoding to UTF-8.
|
||||
module Data.Text.Lazy.IO.Utf8
|
||||
( readFile
|
||||
, writeFile
|
||||
) where
|
||||
|
||||
import Prelude hiding (readFile, writeFile)
|
||||
|
||||
import Control.Exception.Safe (MonadMask)
|
||||
import Control.Monad.IO.Class (MonadIO, liftIO)
|
||||
import Data.Text.Lazy (Text)
|
||||
|
||||
import qualified Data.Text.Lazy.IO as T
|
||||
import qualified System.IO as IO
|
||||
|
||||
import qualified System.IO.Utf8 as Utf8
|
||||
|
||||
|
||||
-- | Like @readFile@, but assumes the file is encoded in UTF-8, regardless
|
||||
-- of the current locale.
|
||||
readFile :: MonadIO m => IO.FilePath -> m Text
|
||||
readFile path = Utf8.openFile path IO.ReadMode >>= liftIO . T.hGetContents
|
||||
|
||||
-- | Like @writeFile@, but encodes the data in UTF-8, regardless
|
||||
-- of the current locale.
|
||||
writeFile :: (MonadIO m, MonadMask m) => IO.FilePath -> Text -> m ()
|
||||
writeFile path = Utf8.withFile path IO.WriteMode . (liftIO .) . flip T.hPutStr
|
Loading…
Reference in New Issue
Block a user