1 ! Copyright (C) 2008, 2010 Daniel Ehrenberg, Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors combinators destructors io io.streams.plain
4 kernel math namespaces sbufs sequences sequences.private
8 ! The encoding descriptor protocol
10 GENERIC: guess-encoded-length ( string-length encoding -- byte-length )
11 GENERIC: guess-decoded-length ( byte-length encoding -- string-length )
13 M: object guess-decoded-length drop ; inline
14 M: object guess-encoded-length drop ; inline
16 GENERIC: decode-char ( stream encoding -- char/f )
18 GENERIC: encode-char ( char stream encoding -- )
20 GENERIC: encode-string ( string stream encoding -- )
22 M: object encode-string [ encode-char ] 2curry each ; inline
24 GENERIC: <decoder> ( stream encoding -- newstream )
26 CONSTANT: replacement-char HEX: fffd
28 TUPLE: decoder { stream read-only } { code read-only } { cr boolean } ;
29 INSTANCE: decoder noncopying-reader
33 GENERIC: <encoder> ( stream encoding -- newstream )
35 TUPLE: encoder { stream read-only } { code read-only } ;
41 M: object <decoder> f decoder boa ; inline
45 : cr+ ( stream -- ) t >>cr drop ; inline
47 : cr- ( stream -- ) f >>cr drop ; inline
49 : >decoder< ( decoder -- stream encoding )
50 [ stream>> ] [ code>> ] bi ; inline
52 : fix-read1 ( stream char -- char )
60 M: decoder stream-element-type
61 drop +character+ ; inline
63 M: decoder stream-tell stream>> stream-tell ; inline
65 M: decoder stream-seek stream>> stream-seek ; inline
67 : (read1) ( decoder -- ch )
68 >decoder< decode-char ; inline
70 : fix-cr ( decoder c -- c' )
73 dup CHAR: \n eq? [ drop (read1) ] [ nip ] if
76 M: decoder stream-read1 ( decoder -- ch )
77 dup (read1) fix-cr ; inline
79 : (read-first) ( n buf decoder -- buf stream encoding n c )
80 [ rot [ >decoder< ] dip 2over decode-char ]
81 [ swap fix-cr ] bi ; inline
83 : (store-read) ( buf stream encoding n c i -- buf stream encoding n )
84 [ rot [ set-nth-unsafe ] keep ] 2curry 3dip ; inline
86 : (finish-read) ( buf stream encoding n i -- i )
89 : (read-next) ( stream encoding n i -- stream encoding n i c )
90 [ 2dup decode-char ] 2dip rot ; inline
92 : (read-rest) ( buf stream encoding n i -- count )
93 2dup = [ (finish-read) ] [
95 swap [ (store-read) ] [ 1 + ] bi (read-rest)
96 ] [ (finish-read) ] if*
97 ] if ; inline recursive
99 M: decoder stream-read-unsafe ( n buf decoder -- count )
100 pick 0 = [ 3drop 0 ] [
104 ] [ 2drop 2drop 0 ] if*
107 M: decoder stream-read-partial-unsafe stream-read-unsafe ; inline
109 : line-ends/eof ( stream str -- str ) f like swap cr- ; inline
111 : line-ends\r ( stream str -- str ) swap cr+ ; inline
113 : line-ends\n ( stream str -- str )
114 over cr>> over empty? and
115 [ drop dup cr- stream-readln ] [ swap cr- ] if ; inline
117 : handle-readln ( stream str ch -- str )
119 { f [ line-ends/eof ] }
120 { CHAR: \r [ line-ends\r ] }
121 { CHAR: \n [ line-ends\n ] }
124 ! If the stop? branch is taken convert the sbuf to a string
125 ! If sep is present, returns ``string sep'' (string can be "")
126 ! If sep is f, returns ``string f'' or ``f f''
127 : read-until-loop ( buf quot: ( -- char stop? ) -- string/f sep/f )
129 [ nip [ "" like ] dip [ f like f ] unless* ]
130 [ pick push read-until-loop ] if ; inline recursive
132 : (read-until) ( quot -- string/f sep/f )
133 [ 100 <sbuf> ] dip read-until-loop ; inline
135 : decoder-read-until ( seps stream encoding -- string/f sep/f )
136 [ decode-char dup [ dup rot member? ] [ 2drop f t ] if ] 3curry
139 M: decoder stream-read-until >decoder< decoder-read-until ;
141 : decoder-readln ( stream encoding -- string/f sep/f )
142 [ decode-char dup [ dup "\r\n" member? ] [ drop f t ] if ] 2curry
145 M: decoder stream-readln dup >decoder< decoder-readln handle-readln ;
147 M: decoder dispose stream>> dispose ;
150 M: object <encoder> encoder boa ; inline
152 : >encoder< ( encoder -- stream encoding )
153 [ stream>> ] [ code>> ] bi ; inline
155 M: encoder stream-element-type
156 drop +character+ ; inline
158 M: encoder stream-write1
159 >encoder< encode-char ; inline
161 M: encoder stream-write
162 >encoder< encode-string ; inline
164 M: encoder dispose stream>> dispose ; inline
166 M: encoder stream-flush stream>> stream-flush ; inline
168 INSTANCE: encoder plain-writer
171 GENERIC# re-encode 1 ( stream encoding -- newstream )
173 M: object re-encode <encoder> ;
175 M: encoder re-encode [ stream>> ] dip re-encode ;
177 : encode-output ( encoding -- )
178 output-stream [ swap re-encode ] change ;
180 : with-encoded-output ( encoding quot -- )
181 [ [ output-stream get ] dip re-encode ] dip
182 with-output-stream* ; inline
184 GENERIC# re-decode 1 ( stream encoding -- newstream )
186 M: object re-decode <decoder> ;
188 M: decoder re-decode [ stream>> ] dip re-decode ;
190 : decode-input ( encoding -- )
191 input-stream [ swap re-decode ] change ;
193 : with-decoded-input ( encoding quot -- )
194 [ [ input-stream get ] dip re-decode ] dip
195 with-input-stream* ; inline