]> gitweb.factorcode.org Git - factor.git/blob - basis/io/encodings/string/string.factor
Switch to https urls
[factor.git] / basis / io / encodings / string / string.factor
1 ! Copyright (C) 2008 Daniel Ehrenberg.
2 ! See https://factorcode.org/license.txt for BSD license.
3 USING: accessors byte-arrays byte-vectors io io.encodings
4 io.streams.byte-array io.streams.string kernel locals
5 sbufs sequences io.private io.encodings.ascii
6 io.encodings.binary io.encodings.private io.encodings.utf8 ;
7 IN: io.encodings.string
8
9 :: decode ( byte-array encoding -- string )
10     encoding binary eq? [ byte-array ] [
11         byte-array byte-array? encoding ascii eq? and [
12             byte-array byte-array>string-fast
13         ] [
14             byte-array encoding <byte-reader> :> reader
15             byte-array length encoding guess-decoded-length <sbuf> :> buf
16             [ reader stream-read1 ] [ buf push ] while*
17             buf "" like
18         ] if
19     ] if ; inline
20
21 :: encode ( string encoding -- byte-array )
22     encoding binary eq? [ string ] [
23         string aux>> not encoding { ascii utf8 } member-eq? and [
24             string string>byte-array-fast
25         ] [
26             string length encoding guess-encoded-length <byte-vector> :> vec
27             string vec encoding <encoder> stream-write
28             vec B{ } like
29         ] if
30     ] if ; inline