-
Notifications
You must be signed in to change notification settings - Fork 159
Add Text.IO.Utf8 module #503
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
2ec84e2
Add Text.IO.Utf8 module
oberblastmeister a868f4b
add more functions
oberblastmeister 66dab03
make decoding strict in interact
oberblastmeister 306f7fa
move back into Data.Text.IO
oberblastmeister 7e9d7e4
Revert "move back into Data.Text.IO"
oberblastmeister 5e0bd99
remove unnecessary documentation
oberblastmeister acec5dc
add module to cabal file and add note
oberblastmeister a59e97e
add tests
oberblastmeister e29b635
fix import
oberblastmeister 23c69de
add docs
oberblastmeister File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
-- | | ||
-- Module : Data.Text.IO.Utf8 | ||
-- License : BSD-style | ||
-- Portability : GHC | ||
-- | ||
-- Efficient UTF-8 support for text I\/O. | ||
-- Unlike @Data.Text.IO@, these functions do not depend on the locale | ||
-- and do not do line ending conversion. | ||
module Data.Text.IO.Utf8 | ||
( | ||
-- * 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 Prelude hiding (readFile, writeFile, appendFile, interact, getContents, getLine, putStr, putStrLn) | ||
import Control.Exception (evaluate) | ||
import Control.Monad ((<=<)) | ||
import Data.ByteString (ByteString) | ||
import qualified Data.ByteString as B | ||
import Data.Text (Text) | ||
import Data.Text.Encoding (decodeUtf8, encodeUtf8) | ||
import GHC.IO.Handle (Handle) | ||
import qualified Data.ByteString.Char8 as B.Char8 | ||
|
||
decodeUtf8IO :: ByteString -> IO Text | ||
decodeUtf8IO = evaluate . decodeUtf8 | ||
|
||
-- | The 'readFile' function reads a file and returns the contents of | ||
-- the file as a string. The entire file is read strictly, as with | ||
oberblastmeister marked this conversation as resolved.
Show resolved
Hide resolved
|
||
-- 'getContents'. | ||
readFile :: FilePath -> IO Text | ||
readFile = decodeUtf8IO <=< B.readFile | ||
oberblastmeister marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
-- | Write a string to a file. The file is truncated to zero length | ||
-- before writing begins. | ||
writeFile :: FilePath -> Text -> IO () | ||
writeFile fp = B.writeFile fp . encodeUtf8 | ||
|
||
-- | Write a string to the end of a file. | ||
appendFile :: FilePath -> Text -> IO () | ||
appendFile fp = B.appendFile fp . encodeUtf8 | ||
|
||
-- | Read the remaining contents of a 'Handle' as a string. | ||
hGetContents :: Handle -> IO Text | ||
hGetContents = decodeUtf8IO <=< B.hGetContents | ||
|
||
-- | Read a single line from a handle. | ||
hGetLine :: Handle -> IO Text | ||
hGetLine = decodeUtf8IO <=< B.hGetLine | ||
|
||
-- | Write a string to a handle. | ||
hPutStr :: Handle -> Text -> IO () | ||
hPutStr h = B.hPutStr h . encodeUtf8 | ||
|
||
-- | Write a string to a handle, followed by a newline. | ||
hPutStrLn :: Handle -> Text -> IO () | ||
hPutStrLn h t = hPutStr h t >> B.hPutStr h (B.Char8.singleton '\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 = decodeUtf8IO =<< B.getContents | ||
|
||
-- | Read a single line of user input from 'stdin'. | ||
getLine :: IO Text | ||
getLine = decodeUtf8IO =<< B.getLine | ||
|
||
-- | Write a string to 'stdout'. | ||
putStr :: Text -> IO () | ||
putStr = B.putStr . encodeUtf8 | ||
|
||
-- | Write a string to 'stdout', followed by a newline. | ||
putStrLn :: Text -> IO () | ||
putStrLn t = B.putStr (encodeUtf8 t) >> B.putStr (B.Char8.singleton '\n') |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.