bytestring-encoding-0.1.0.0: ByteString ↔ Text converter based on GHC.IO.Encoding

Copyright (c) Masahiro Sakai 2018 BSD-style masahiro.sakai@gmail.com provisional non-portable None Haskell2010

Data.ByteString.Lazy.Encoding

Description

ByteStringText converter based on GHC.IO.Encoding.

Synopsis

# Converter functions

Encode a lazy Text into a lazy ByteString using a given TextEncoding.

Decode a lazy ByteString to a lazy Text using a given TextEncoding.

# Text encodings (re-export from System.IO)

data TextEncoding #

A TextEncoding is a specification of a conversion scheme between sequences of bytes and sequences of Unicode characters.

For example, UTF-8 is an encoding of Unicode characters into a sequence of bytes. The TextEncoding for UTF-8 is utf8.

Instances
 Since: base-4.3.0.0 Instance detailsDefined in GHC.IO.Encoding.Types MethodsshowList :: [TextEncoding] -> ShowS #

The Latin1 (ISO8859-1) encoding. This encoding maps bytes directly to the first 256 Unicode code points, and is thus not a complete Unicode encoding. An attempt to write a character greater than '\255' to a Handle using the latin1 encoding will result in an error.

The UTF-8 Unicode encoding

The UTF-8 Unicode encoding, with a byte-order-mark (BOM; the byte sequence 0xEF 0xBB 0xBF). This encoding behaves like utf8, except that on input, the BOM sequence is ignored at the beginning of the stream, and on output, the BOM sequence is prepended.

The byte-order-mark is strictly unnecessary in UTF-8, but is sometimes used to identify the encoding of a file.

The UTF-16 Unicode encoding (a byte-order-mark should be used to indicate endianness).

The UTF-16 Unicode encoding (litte-endian)

The UTF-16 Unicode encoding (big-endian)

The UTF-32 Unicode encoding (a byte-order-mark should be used to indicate endianness).

The UTF-32 Unicode encoding (litte-endian)

The UTF-32 Unicode encoding (big-endian)

The Unicode encoding of the current locale

This is the initial locale encoding: if it has been subsequently changed by setLocaleEncoding this value will not reflect that change.

An encoding in which Unicode code points are translated to bytes by taking the code point modulo 256. When decoding, bytes are translated directly into the equivalent code point.

This encoding never fails in either direction. However, encoding discards information, so encode followed by decode is not the identity.

Since: base-4.4.0.0

Look up the named Unicode encoding. May fail with

• isDoesNotExistError if the encoding is unknown

The set of known encodings is system-dependent, but includes at least:

• UTF-8
• UTF-16, UTF-16BE, UTF-16LE
• UTF-32, UTF-32BE, UTF-32LE

There is additional notation (borrowed from GNU iconv) for specifying how illegal characters are handled:

• a suffix of //IGNORE, e.g. UTF-8//IGNORE, will cause all illegal sequences on input to be ignored, and on output will drop all code points that have no representation in the target encoding.
• a suffix of //TRANSLIT will choose a replacement character for illegal sequences or code points.
• a suffix of //ROUNDTRIP will use a PEP383-style escape mechanism to represent any invalid bytes in the input as Unicode codepoints (specifically, as lone surrogates, which are normally invalid in UTF-32). Upon output, these special codepoints are detected and turned back into the corresponding original byte.

In theory, this mechanism allows arbitrary data to be roundtripped via a String with no loss of data. In practice, there are two limitations to be aware of:

1. This only stands a chance of working for an encoding which is an ASCII superset, as for security reasons we refuse to escape any bytes smaller than 128. Many encodings of interest are ASCII supersets (in particular, you can assume that the locale encoding is an ASCII superset) but many (such as UTF-16) are not.
2. If the underlying encoding is not itself roundtrippable, this mechanism can fail. Roundtrippable encodings are those which have an injective mapping into Unicode. Almost all encodings meet this criteria, but some do not. Notably, Shift-JIS (CP932) and Big5 contain several different encodings of the same Unicode codepoint.

On Windows, you can access supported code pages with the prefix CP; for example, "CP1250".