]> gitweb.factorcode.org Git - factor.git/blob - basis/io/crlf/crlf.factor
43f6ae0cd6c6bc1515201bb27efc3b45deff8147
[factor.git] / basis / io / crlf / crlf.factor
1 ! Copyright (C) 2009 Daniel Ehrenberg, Slava Pestov
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: byte-vectors io io.private kernel locals math namespaces
4 sbufs sequences splitting ;
5 IN: io.crlf
6
7 : crlf ( -- )
8     "\r\n" write ;
9
10 :: stream-read-crlf ( stream -- seq )
11     "\r" stream stream-read-until [
12         CHAR: \r assert= stream stream-read1 CHAR: \n assert=
13     ] [ f like ] if* ;
14
15 : read-crlf ( -- seq )
16     input-stream get stream-read-crlf ;
17
18 :: stream-read-?crlf ( stream -- seq )
19     "\r\n" stream stream-read-until [
20         CHAR: \r = [ stream stream-read1 CHAR: \n assert= ] when
21     ] [ f like ] if* ;
22
23 : read-?crlf ( -- seq )
24     input-stream get stream-read-?crlf ;
25
26 : crlf>lf ( str -- str' )
27     CHAR: \r swap remove ;
28
29 : lf>crlf ( str -- str' )
30     split-lines "\r\n" join ;
31
32 :: stream-read1-ignoring-crlf ( stream -- ch )
33     stream stream-read1 dup "\r\n" member?
34     [ drop stream stream-read1-ignoring-crlf ] when ; inline recursive
35
36 : read1-ignoring-crlf ( -- ch )
37     input-stream get stream-read1-ignoring-crlf ;
38
39 : push-ignoring-crlf ( elt seq -- )
40     [ "\r\n" member? not ] swap push-if ;
41
42 : push-all-ignoring-crlf ( src dst -- )
43     [ push-ignoring-crlf ] curry each ;
44
45 :: stream-read-ignoring-crlf ( n stream -- seq/f )
46     n stream stream-read dup [
47         dup [ "\r\n" member? ] any? [
48             stream stream-element-type +byte+ =
49             [ n <byte-vector> ] [ n <sbuf> ] if :> accum
50             accum push-all-ignoring-crlf
51
52             [ accum length n < and ] [
53                 n accum length - stream stream-read
54                 [ accum push-all-ignoring-crlf ] keep
55             ] do while
56
57             accum stream stream-exemplar like
58         ] when
59     ] when ;
60
61 : read-ignoring-crlf ( n -- seq/f )
62     input-stream get stream-read-ignoring-crlf ;