factor/core/io/encodings/encodings.factor

57 lines
1.6 KiB
Factor
Raw Normal View History

2007-09-20 18:09:08 -04:00
! Copyright (C) 2006, 2007 Daniel Ehrenberg.
! See http://factorcode.org/license.txt for BSD license.
USING: math kernel sequences sbufs vectors
2008-02-13 02:02:37 -05:00
namespaces unicode.syntax growable strings io ;
2007-09-20 18:09:08 -04:00
IN: io.encodings
TUPLE: encode-error ;
: encode-error ( -- * ) \ encode-error construct-empty throw ;
TUPLE: decode-error ;
2008-02-02 00:59:46 -05:00
: decode-error ( -- * ) \ decode-error construct-empty throw ;
2007-09-20 18:09:08 -04:00
SYMBOL: begin
: decoded ( buf ch -- buf ch state )
over push 0 begin ;
2008-02-01 18:45:35 -05:00
: push-replacement ( buf -- buf ch state )
UNICHAR: replacement-character decoded ;
2007-09-20 18:09:08 -04:00
: finish-decoding ( buf ch state -- str )
2008-02-01 17:21:42 -05:00
begin eq? [ decode-error ] unless drop "" like ;
2007-09-20 18:09:08 -04:00
2008-02-13 02:02:37 -05:00
: start-decoding ( seq length -- buf ch state seq )
<sbuf> 0 begin roll ;
2008-02-11 00:14:42 -05:00
2008-02-13 02:02:37 -05:00
: decode ( seq quot -- string )
>r dup length start-decoding r>
[ -rot ] swap compose each
finish-decoding ; inline
: space ( resizable -- room-left )
dup underlying swap [ length ] 2apply - ;
: full? ( resizable -- ? ) space zero? ;
: decode-part-loop ( buf ch state stream quot -- string )
>r >r pick r> r> rot full?
[ 2drop 2drop >string ]
[ [ >r stream-read1 -rot r> call ] 2keep decode-part-loop ] if ; inline
: decode-part ( length stream quot -- string )
>r swap start-decoding r>
decode-part-loop ; inline
2008-02-11 00:14:42 -05:00
GENERIC: init-decoding ( stream encoding -- decoded-stream )
: <decoding> ( stream decoding-class -- decoded-stream )
construct-empty init-decoding ;
GENERIC: init-encoding ( stream encoding -- encoded-stream )
: <encoding> ( stream encoding-class -- encoded-stream )
construct-empty init-encoding ;