HORIZON HASKELLDocslts/ghc-10.0.xaa25b892026-10-10Search names, modules, packages, or :: a typeCtrl K

GHC 10.0.0.20260917 · lts/ghc-10.0.x · aa25b89 · 2026-10-10

Moduletext-2.1.4Haskell2010

Data.Text.IO

Efficient locale-sensitive support for text I/O.

The functions in this module obey the runtime system's locale, character set encoding, and line ending conversion settings.

If you want to do I/O using the UTF-8 encoding, use Data.Text.IO.Utf8, which is faster than this module.

If you know in advance that you will be working with data that has a specific encoding, and your application is highly performance sensitive, you may find that it is faster to perform I/O with bytestrings and to encode and decode yourself than to use the functions in this module.

  • 13 values
  • Packagetext-2.1.4
  • Exports13
  • LanguageHaskell2010
  • LicenceBSD-2-Clause
  • SourceIO.hs

File-at-a-time operations

3 declarations
valuereadFile :: FilePath -> IO Text
#

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.

Beware that this function (similarly to readFile) is locale-dependent. Unexpected system locale may cause your application to read corrupted data or throw runtime exceptions about "invalid argument (invalid byte sequence)" or "invalid argument (invalid character)". This is also slow, because GHC first converts an entire input to UTF-32, which is afterwards converted to UTF-8.

If your data is UTF-8, using decodeUtf8 . readFile is a much faster and safer alternative.

valuewriteFile :: FilePath -> Text -> IO ()
#

Write a string to a file. The file is truncated to zero length before writing begins.

Operations on handles

5 declarations
valuehGetContents :: Handle -> IO Text
#

Read the remaining contents of a Handle as a string. The Handle is closed once the contents have been read, or if an exception is thrown.

Internally, this function reads a chunk at a time from the lower-level buffering abstraction, and concatenates the chunks into a single string once the entire file has been read.

As a result, it requires approximately twice as much memory as its result to construct its result. For files more than a half of available RAM in size, this may result in memory exhaustion.

valuehGetChunk :: Handle -> IO Text
#

Read a single chunk of strict text from a Handle. The size of the chunk depends on the amount of input currently buffered.

This function blocks only if there is no data available, and EOF has not yet been reached. Once EOF is reached, this function returns an empty string instead of throwing an exception.

Behavior

Unlike byte-oriented functions, hGetChunk operates on complete UTF-8 characters. Since UTF-8 characters can occupy 1 to 4 bytes, this function cannot guarantee reading an exact number of bytes. Instead, it reads complete characters up to the handle's internal buffer limit.

Buffer Size

The maximum chunk size is determined by the handle's internal character buffer, which is set to 8192 bytes (2048 characters) by the GHC runtime constant dEFAULT_CHAR_BUFFER_SIZE. This buffer size cannot be modified through any public API.

UTF-8 Considerations

When working with UTF-8 encoded text:

  • The function will never return a partial character

  • The actual number of bytes read may vary depending on the character encoding (ASCII characters = 1 byte, other Unicode characters = 2-4 bytes)

Special cases for standard input and output

5 declarations
valueinteract :: (Text -> Text) -> IO ()
#

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.