A stream oriented decoding result.
Constructors
Some !Text !ByteString (ByteString -> Decoding)
:: a typeCtrl KGHC 9.10.3 · lts/ghc-9.10.x · c74966e · 2026-09-27
Modulerebase-1.21.2Haskell2010
A stream oriented decoding result.
Some !Text !ByteString (ByteString -> Decoding)Decode a ByteString containing UTF-8 encoded text.
If the input contains any invalid UTF-8 data, the relevant exception will be returned, otherwise the decoded text.
Encode text to a ByteString Builder using UTF-8 encoding.
Decode a ByteString containing UTF-8 encoded text.
Surrogate code points in replacement character returned by OnDecodeError
will be automatically remapped to the replacement char U+FFFD.
Decode a ByteString containing UTF-8 encoded text that is known to be valid.
If the input contains any invalid UTF-8 data, an exception will be thrown that cannot be caught in pure code. For more control over the handling of invalid data, use decodeUtf8' or decodeUtf8With.
This is a partial function: it checks that input is a well-formed UTF-8 sequence and copies buffer or throws an error otherwise.
Encode text using UTF-8 encoding.
Decode a ByteString containing 7-bit ASCII encoded text.
This is a partial function: it checks that input does not contain anything except ASCII and copies buffer or throws an error otherwise.
Decode a ByteString containing Latin-1 (aka ISO-8859-1) encoded text.
decodeLatin1 is semantically equivalent to
Data.Text.pack . Data.ByteString.Char8.unpack
This is a total function. However, bear in mind that decoding Latin-1 (non-ASCII) characters to UTf-8 requires actual work and is not just buffer copying.
Encode text using big endian UTF-16 encoding.
Encode text using little endian UTF-16 encoding.
Encode text using big endian UTF-32 encoding.
Encode text using little endian UTF-32 encoding.
Encode text using UTF-8 encoding and escape the ASCII characters using a BoundedPrim.
Use this function is to implement efficient encoders for text-based formats like JSON or HTML.
Initial Utf8State.
Use StrictBuilder to build Text.
A delayed representation of strict Text.
Semigroup StrictTextBuilderDefined in text-2.1.3 · Data.Text.Internal.StrictBuilderConcatenation of StrictBuilder is right-biased: the right builder will be run first. This allows a builder to run tail-recursively when it was accumulated left-to-right.
Monoid StrictTextBuilderDefined in text-2.1.3 · Data.Text.Internal.StrictBuilderDecode text from big endian UTF-16 encoding.
If the input contains any invalid big endian UTF-16 data, an exception will be thrown. For more control over the handling of invalid data, use decodeUtf16BEWith.
Decode text from big endian UTF-16 encoding.
Decode text from little endian UTF-16 encoding.
If the input contains any invalid little endian UTF-16 data, an exception will be thrown. For more control over the handling of invalid data, use decodeUtf16LEWith.
Decode text from little endian UTF-16 encoding.
Decode text from big endian UTF-32 encoding.
If the input contains any invalid big endian UTF-32 data, an exception will be thrown. For more control over the handling of invalid data, use decodeUtf32BEWith.
Decode text from big endian UTF-32 encoding.
Decode text from little endian UTF-32 encoding.
If the input contains any invalid little endian UTF-32 data, an exception will be thrown. For more control over the handling of invalid data, use decodeUtf32LEWith.
Decode text from little endian UTF-32 encoding.
State of decoding a ByteString in UTF-8. Enables incremental decoding (validateUtf8Chunk, validateUtf8More, decodeUtf8Chunk, decodeUtf8More).
Decode a chunk of UTF-8 text. To be continued with decodeUtf8More.
See decodeUtf8More for details on the result.
decodeUtf8Chunk = decodeUtf8More startUtf8State
Given:
decodeUtf8Chunk chunk = (builder, rest, ms)
builder is a prefix and rest is a suffix of chunk.
encodeUtf8 (Data.Text.Encoding.strictBuilderToText builder) <> rest = chunk
Decode another chunk in an ongoing UTF-8 stream.
Returns a triple:
A StrictBuilder for the decoded chunk of text. You can accumulate
chunks with (<>) or output them with toText.
The undecoded remainder of the given chunk, for diagnosing errors and resuming (presumably after skipping some bytes).
Just the new state, or Nothing if an invalid byte was encountered (it will be within the first 4 bytes of the undecoded remainder).
Given:
(pre, suf, ms) = decodeUtf8More s chunk
If the output pre is nonempty (alternatively, if length chunk > length suf)
s2b pre `append` suf = p2b s `append` chunk
where
s2b = encodeUtf8 . toText
p2b = partUtf8ToByteString
If the output pre is empty (alternatively, if length chunk = length suf)
suf = chunkDecoding chunks separately is equivalent to decoding their concatenation.
Given:
(pre1, suf1, Just s1) = decodeUtf8More s chunk1
(pre2, suf2, ms2) = decodeUtf8More s1 chunk2
(pre3, suf3, ms3) = decodeUtf8More s (chunk1 `B.append` chunk2)
we have:
s2b (pre1 <> pre2) = s2b pre3
ms2 = ms3
Copy Text in a StrictBuilder
Validate a ByteString as UTF-8-encoded text. To be continued using validateUtf8More.
See also validateUtf8More for details on the result of this function.
validateUtf8Chunk = validateUtf8More startUtf8State
Given:
validateUtf8Chunk chunk = (n, ms)
The prefix is valid UTF-8. In particular, it should be accepted by this validation:
validateUtf8Chunk (take n chunk) = (n, Just startUtf8State)
Validate another ByteString chunk in an ongoing stream of UTF-8-encoded text.
Returns a pair:
The first component n is the end position, relative to the current
chunk, of the longest prefix of the accumulated bytestring which is valid UTF-8.
n may be negative: that happens when an incomplete code point started in
a previous chunk and is not completed by the current chunk (either
that code point is still incomplete, or it is broken by an invalid byte).
The second component ms indicates the following:
if ms = Nothing, the remainder of the chunk contains an invalid byte,
within four bytes from position n;
if ms = Just s', you can carry on validating another chunk
by calling validateUtf8More with the new state s'.
Given:
validateUtf8More s chunk = (n, ms)
If the chunk is invalid, it cannot be extended to be valid.
ms = Nothing
==> validateUtf8More s (chunk <> more) = (n, Nothing)
Validating two chunks sequentially is the same as validating them together at once:
ms = Just s'
==> validateUtf8More s (chunk <> more) = first (length chunk +) (validateUtf8More s' more)
Deprecated. Use StrictTextBuilder instead
A delayed representation of strict Text.
Decode a ByteString containing 7-bit ASCII encoded text.
This is a total function which returns either the ByteString converted to a Text containing ASCII text, or Nothing.
Use decodeASCIIPrefix to retain the longest ASCII prefix for an invalid input instead of discarding it.
Decode a ByteString containing ASCII text.
This is a total function which returns a pair of the longest ASCII prefix as Text, and the remaining suffix as ByteString.
Important note: the pair is lazy. This lets you check for errors by testing
whether the second component is empty, without forcing the first component
(which does a copy).
To drop references to the input bytestring, force the prefix
(using seq or BangPatterns) and drop references to the suffix.
If (prefix, suffix) = decodeAsciiPrefix s, then encodeUtf8 prefix <> suffix = s.
Either suffix is empty, or head suffix > 127.
Decode a ByteString containing UTF-8 encoded text.
Any invalid input bytes will be replaced with the Unicode replacement character U+FFFD.
Decode, in a stream oriented way, a ByteString containing UTF-8 encoded text that is known to be valid.
If the input contains any invalid UTF-8 data, an exception will be thrown (either by this function or a continuation) that cannot be caught in pure code. For more control over the handling of invalid data, use streamDecodeUtf8With.
Decode, in a stream oriented way, a lazy ByteString containing UTF-8 encoded text.