1 ! Copyright (C) 2013 John Benediktsson
2 ! See http://factorcode.org/license.txt for BSD license
4 USING: io io.streams.string kernel locals make math math.bitwise
5 math.order namespaces sequences ;
11 ERROR: bad-length seq ;
13 : check-length ( seq -- seq )
14 dup length 45 > [ bad-length ] when ; inline
16 :: binary>ascii ( seq -- seq' )
20 dup length CHAR: \s + ,
22 [ dup empty? bits zero? and ] [
28 unclip-slice char bitor char!
33 [ char swap neg shift 0x3f bitand CHAR: \s + , ]
40 ERROR: illegal-character ch ;
42 : check-illegal-character ( ch -- ch )
43 dup CHAR: \s dup 64 + between? [ illegal-character ] unless ;
45 :: ascii>binary ( seq -- seq' )
49 seq unclip-slice CHAR: \s - :> len!
52 [ dup empty? not len 0 > and ] [
53 dup empty? [ 0 ] [ unclip-slice ] if
54 dup "\r\n\0" member? [
57 check-illegal-character
61 char 6 shift bitor char!
66 [ char swap neg shift 0xff bitand , ]
67 [ on-bits char bitand char! ]
79 input-stream get [ binary>ascii print ] 45 (each-stream-block)
82 : string>uu ( seq -- seq' )
83 [ [ uu-encode ] with-string-writer ] with-string-reader ;
86 [ [ "begin" head? ] [ not ] bi or ] [ readln ] do until
88 dup [ "end" head? ] [ not ] bi or
89 [ drop t ] [ ascii>binary write f ] if
90 ] [ readln ] do until ;
92 : uu>string ( seq -- seq )
93 [ [ uu-decode ] with-string-writer ] with-string-reader ;