Decode a stream of bytes to Unicode characters by mapping each byte to a corresponding Unicode Char in 0-255 range.
Modulestreamly-core-0.3.1Haskell2010
Streamly.Internal.Unicode.Stream
Contents
- Setup
- Construction (Decoding)
- UTF-8 Byte Stream Decoding
- UTF-16 Byte Stream Decoding
- Resumable UTF-8 Byte Stream Decoding
- UTF-8 Array Stream Decoding
- Elimination (Encoding)
- Latin1 Encoding to Byte Stream
- UTF-8 Encoding to Byte Stream
- UTF-8 Encoding to Chunk Stream
- UTF-16 Encoding to Byte Stream
- Transformation
- StreamD UTF8 Encoding / Decoding transformations.
- Decoding String Literals
- Word16 Utilities
- Deprecations
Synopsis
- value decodeLatin1
- data CodingFailureMode
- value writeCharUtf8'
- value parseCharUtf8With
- value decodeUtf8
- value decodeUtf8'
- value decodeUtf8_
- value decodeUtf16le'
- value decodeUtf16le
- data DecodeError
- type DecodeState
- type CodePoint
- value decodeUtf8Either
- value resumeDecodeUtf8Either
- value decodeUtf8Chunks
- value decodeUtf8Chunks'
- value decodeUtf8Chunks_
- value encodeLatin1
- value encodeLatin1'
- value encodeLatin1_
- value readCharUtf8'
- value readCharUtf8
- value readCharUtf8_
- value encodeUtf8
- value encodeUtf8'
- value encodeUtf8_
- value encodeStrings
- value encodeUtf16le'
- value encodeUtf16le
- value stripHead
- value lines
- value words
- value unlines
- value unwords
- value decodeUtf8D
- value decodeUtf8D'
- value decodeUtf8D_
- value encodeUtf8D
- value encodeUtf8D'
- value encodeUtf8D_
- value decodeUtf8EitherD
- value resumeDecodeUtf8EitherD
- value fromStr#
- value mkEvenW8Chunks
- value swapByteOrder
- value decodeUtf8Lax
- value encodeLatin1Lax
- value encodeUtf8Lax
- Packagestreamly-core-0.3.1
- Exports48
- LanguageHaskell2010
- LicenceBSD-3-Clause
- SourceStream.hs
Setup
To execute the code examples provided in this module in ghci, please run the following commands first.
:mimport qualified Streamly.Data.Fold as Foldimport qualified Streamly.Data.Stream as Streamimport qualified Streamly.Unicode.Stream as Unicode
For APIs that have not been released yet.
:set -XMagicHashimport qualified Streamly.Internal.Unicode.Stream as Unicode
Construction (Decoding)
UTF-8 Byte Stream Decoding
Instances1Show
Show CodingFailureModeDefined in streamly-core-0.3.1 · Streamly.Internal.Unicode.Stream
Decode a UTF-8 encoded bytestream to a stream of Unicode characters. Any invalid codepoint encountered is replaced with the unicode replacement character.
Decode a UTF-8 encoded bytestream to a stream of Unicode characters. The function throws an error if an invalid codepoint is encountered.
Decode a UTF-8 encoded bytestream to a stream of Unicode characters. Any invalid codepoint encountered is dropped.
UTF-16 Byte Stream Decoding
Similar to decodeUtf16le but throws an error if an invalid codepoint is encountered.
Decode a UTF-16 encoded stream to a stream of Unicode characters. Any invalid codepoint encountered is replaced with the unicode replacement character.
The Word16s are expected to be in the little-endian byte order.
Resumable UTF-8 Byte Stream Decoding
Constructors
Instances1Show
Show DecodeErrorDefined in streamly-core-0.3.1 · Streamly.Internal.Unicode.Stream
Pre-release
Pre-release
UTF-8 Array Stream Decoding
Like decodeUtf8 but for a chunked stream. It may be slightly faster than flattening the stream and then decoding with decodeUtf8.
Like 'decodeUtf8'' but for a chunked stream. It may be slightly faster than flattening the stream and then decoding with 'decodeUtf8''.
Like decodeUtf8_ but for a chunked stream. It may be slightly faster than flattening the stream and then decoding with decodeUtf8_.
Elimination (Encoding)
Latin1 Encoding to Byte Stream
Like encodeLatin1' but silently maps input codepoints beyond 255 to arbitrary Latin1 chars in 0-255 range. No error or exception is thrown when such mapping occurs.
Encode a stream of Unicode characters to bytes by mapping each character to a byte in 0-255 range. Throws an error if the input stream contains characters beyond 255.
Like encodeLatin1 but drops the input characters beyond 255.
UTF-8 Encoding to Byte Stream
Encode a stream of Unicode characters to a UTF-8 encoded bytestream. Any Invalid characters (U+D800-U+D8FF) in the input stream are replaced by the Unicode replacement character U+FFFD.
Encode a stream of Unicode characters to a UTF-8 encoded bytestream. When any invalid character (U+D800-U+D8FF) is encountered in the input stream the function errors out.
Encode a stream of Unicode characters to a UTF-8 encoded bytestream. Any Invalid characters (U+D800-U+D8FF) in the input stream are dropped.
Encode a stream of String using the supplied encoding scheme. Each
string is encoded as an Array Word8.
UTF-8 Encoding to Chunk Stream
UTF-16 Encoding to Byte Stream
Similar to encodeUtf16le but throws an error if any invalid character is encountered.
Encode a stream of Unicode characters to a UTF-16 encoded stream. Any invalid characters in the input stream are replaced by the Unicode replacement character U+FFFD.
The resulting Word16s are encoded in little-endian byte order.
Transformation
Remove leading whitespace from a string.
stripHead = Stream.dropWhile Char.isSpacePre-release
Fold each line of the stream using the supplied Fold and stream the result.
Definition:
lines f = Stream.foldMany (Fold.takeEndBy_ (== '\n') f)Usage:
Stream.toList $ Unicode.lines Fold.toList (Stream.fromList "line1\nline2\nline3\n\n\n")["line1","line2","line3","",""]
Pre-release
Fold each word of the stream using the supplied Fold.
Definition:
words = Stream.wordsBy Char.isSpaceUsage:
Stream.toList $ Unicode.words Fold.toList (Stream.fromList " ab cd ef ")["ab","cd","ef"]
Pre-release
Unfold a stream to character streams using the supplied Unfold
and concat the results suffixing a newline character \n to each stream.
Definition:
unlines = Stream.unfoldEachEndBy '\n'unlines = Stream.unfoldEachEndBySeq "\n" Unfold.fromList
Pre-release
Unfold the elements of a stream to character streams using the supplied Unfold and concat the results with a whitespace character infixed between the streams.
unwords = Stream.unfoldEachSepBy ' 'unwords = Stream.unfoldEachSepBySeq " " Unfold.fromList
Pre-release
StreamD UTF8 Encoding / Decoding transformations.
See section "3.9 Unicode Encoding Forms" in https://www.unicode.org/versions/Unicode13.0.0/UnicodeStandard-13.0.pdf
Decoding String Literals
Read UTF-8 encoded bytes as chars from an Addr# until a 0 byte is encountered, the 0 byte is not included in the stream.
Unsafe: The caller is responsible for safe addressing.
Note that this is completely safe when reading from Haskell string literals because they are guaranteed to be NULL terminated:
Stream.fold Fold.toList (Unicode.fromStr# "Haskell"#)"Haskell"
Word16 Utilities
Ensure chunks of even length. This can be used before casting the arrays to Word16. Use this API when interacting with external data.
The chunks are split and merged accordingly to create arrays of even length. If the sum of length of all the arrays in the stream is odd then the trailing byte of the last array is dropped.
Swap the byte order of Word16
swapByteOrder 0xABCD == 0xCDAB
swapByteOrder . swapByteOrder == idDeprecations
Deprecated. Please use decodeUtf8 instead
Same as decodeUtf8
Deprecated. Please use encodeLatin1 instead
Same as encodeLatin1
Deprecated. Please use encodeUtf8 instead
Same as encodeUtf8