decode ( byte-array encoding -- string )
Factor documentation > Factor handbook > Input and output > I/O encodings > Encoding and decoding strings
Prev:encode ( string encoding -- byte-array )


Vocabulary
io.encodings.string

Inputs and outputs
byte-arraya byte-array
encodingan encoding descriptor
stringa string


Word description
Converts an array of bytes to a string, interpreting that array of bytes as a string with the given encoding.

Examples
USING: io.encodings.string io.encodings.utf8 prettyprint ; B{ 230 136 145 231 136 177 228 189 160 } utf8 decode .
"我爱你"


Definition
USING: io io.encodings io.encodings.binary io.private
io.streams.byte-array kernel locals sequences ;

IN: io.encodings.string

:: decode ( byte-array encoding -- string )
encoding binary eq?
[ byte-array ] [
byte-array encoding <byte-reader>
:> reader byte-array length encoding
guess-decoded-length
reader stream-exemplar-growable new-resizable :> buf
[ reader stream-read1 dup ] [ buf push ] while drop
buf reader stream-exemplar like
] if ; inline