]> gitweb.factorcode.org Git - factor.git/blob - core/io/encodings/encodings.factor
use radix literals
[factor.git] / core / io / encodings / encodings.factor
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
5 splitting strings ;
6 IN: io.encodings
7
8 ! The encoding descriptor protocol
9
10 GENERIC: guess-encoded-length ( string-length encoding -- byte-length )
11 GENERIC: guess-decoded-length ( byte-length encoding -- string-length )
12
13 M: object guess-decoded-length drop ; inline
14 M: object guess-encoded-length drop ; inline
15
16 GENERIC: decode-char ( stream encoding -- char/f )
17
18 GENERIC: encode-char ( char stream encoding -- )
19
20 GENERIC: encode-string ( string stream encoding -- )
21
22 M: object encode-string [ encode-char ] 2curry each ; inline
23
24 GENERIC: <decoder> ( stream encoding -- newstream )
25
26 CONSTANT: replacement-char 0xfffd
27
28 TUPLE: decoder { stream read-only } { code read-only } { cr boolean } ;
29 INSTANCE: decoder input-stream
30
31 ERROR: decode-error ;
32
33 GENERIC: <encoder> ( stream encoding -- newstream )
34
35 TUPLE: encoder { stream read-only } { code read-only } ;
36 INSTANCE: encoder output-stream
37
38 ERROR: encode-error ;
39
40 ! Decoding
41
42 M: object <decoder> f decoder boa ; inline
43
44 <PRIVATE
45
46 : cr+ ( stream -- ) t >>cr drop ; inline
47
48 : cr- ( stream -- ) f >>cr drop ; inline
49
50 : >decoder< ( decoder -- stream encoding )
51     [ stream>> ] [ code>> ] bi ; inline
52
53 M: decoder stream-element-type
54     drop +character+ ; inline
55
56 : (read1) ( decoder -- ch )
57     >decoder< decode-char ; inline
58
59 : fix-cr ( decoder c -- c' )
60     over cr>> [
61         over cr-
62         dup CHAR: \n eq? [ drop (read1) ] [ nip ] if
63     ] [ nip ] if ; inline
64
65 M: decoder stream-read1 ( decoder -- ch )
66     dup (read1) fix-cr ; inline
67
68 : (read-first) ( n buf decoder -- buf stream encoding n c )
69     [ rot [ >decoder< ] dip 2over decode-char ]
70     [ swap fix-cr ] bi ; inline
71
72 : (store-read) ( buf stream encoding n c i -- buf stream encoding n )
73     [ rot [ set-nth-unsafe ] keep ] 2curry 3dip ; inline
74
75 : (finish-read) ( buf stream encoding n i -- i )
76     2nip 2nip ; inline
77
78 : (read-next) ( stream encoding n i -- stream encoding n i c )
79     [ 2dup decode-char ] 2dip rot ; inline
80
81 : (read-rest) ( buf stream encoding n i -- count )
82     2dup = [ (finish-read) ] [
83         (read-next) [
84             swap [ (store-read) ] [ 1 + ] bi (read-rest)
85         ] [ (finish-read) ] if*
86     ] if ; inline recursive
87
88 M: decoder stream-read-unsafe
89     pick 0 = [ 3drop 0 ] [
90         (read-first) [
91             0 (store-read)
92             1 (read-rest)
93         ] [ 2drop 2drop 0 ] if*
94     ] if ; inline
95
96 M: decoder stream-contents
97     (stream-contents-by-element) ;
98
99 : line-ends/eof ( stream str -- str ) f like swap cr- ; inline
100
101 : line-ends\r ( stream str -- str ) swap cr+ ; inline
102
103 : line-ends\n ( stream str -- str )
104     over cr>> over empty? and
105     [ drop dup cr- stream-readln ] [ swap cr- ] if ; inline
106
107 : handle-readln ( stream str ch -- str )
108     {
109         { f [ line-ends/eof ] }
110         { CHAR: \r [ line-ends\r ] }
111         { CHAR: \n [ line-ends\n ] }
112     } case ; inline
113
114 ! If the stop? branch is taken convert the sbuf to a string
115 ! If sep is present, returns ``string sep'' (string can be "")
116 ! If sep is f, returns ``string f'' or ``f f''
117 : read-until-loop ( buf quot: ( -- char stop? ) -- string/f sep/f )
118     dup call
119     [ nip [ "" like ] dip [ f like f ] unless* ]
120     [ pick push read-until-loop ] if ; inline recursive
121
122 : (read-until) ( quot -- string/f sep/f )
123     [ 100 <sbuf> ] dip read-until-loop ; inline
124
125 : decoder-read-until ( seps stream encoding -- string/f sep/f )
126     [ decode-char dup [ dup rot member? ] [ 2drop f t ] if ] 3curry
127     (read-until) ;
128
129 M: decoder stream-read-until >decoder< decoder-read-until ;
130
131 : decoder-readln ( stream encoding -- string/f sep/f )
132     [ decode-char dup [ dup "\r\n" member? ] [ drop f t ] if ] 2curry
133     (read-until) ;
134
135 M: decoder stream-readln dup >decoder< decoder-readln handle-readln ;
136
137 M: decoder dispose stream>> dispose ;
138
139 ! Encoding
140 M: object <encoder> encoder boa ; inline
141
142 : >encoder< ( encoder -- stream encoding )
143     [ stream>> ] [ code>> ] bi ; inline
144
145 M: encoder stream-element-type
146     drop +character+ ; inline
147
148 M: encoder stream-write1
149     >encoder< encode-char ; inline
150
151 M: encoder stream-write
152     >encoder< encode-string ; inline
153
154 M: encoder dispose stream>> dispose ; inline
155
156 M: encoder stream-flush stream>> stream-flush ; inline
157
158 INSTANCE: encoder plain-writer
159 PRIVATE>
160
161 GENERIC# re-encode 1 ( stream encoding -- newstream )
162
163 M: object re-encode <encoder> ;
164
165 M: encoder re-encode [ stream>> ] dip re-encode ;
166
167 : encode-output ( encoding -- )
168     output-stream [ swap re-encode ] change ;
169
170 : with-encoded-output ( encoding quot -- )
171     [ [ output-stream get ] dip re-encode ] dip
172     with-output-stream* ; inline
173
174 GENERIC# re-decode 1 ( stream encoding -- newstream )
175
176 M: object re-decode <decoder> ;
177
178 M: decoder re-decode [ stream>> ] dip re-decode ;
179
180 : decode-input ( encoding -- )
181     input-stream [ swap re-decode ] change ;
182
183 : with-decoded-input ( encoding quot -- )
184     [ [ input-stream get ] dip re-decode ] dip
185     with-input-stream* ; inline