io.crlf: add a word that reads an optional CR.

db4
John Benediktsson 2011-10-12 12:36:54 -07:00
parent 557f8355de
commit 24232cb095
3 changed files with 14 additions and 1 deletions

View File

@ -9,4 +9,8 @@ HELP: crlf
HELP: read-crlf
{ $values { "seq" sequence } }
{ $description "Reads until the next CRLF (carriage return followed by line feed) from the current input stream, throwing an error if there is not a CRLF remaining, or if CR is present without immediately being followed by LF." } ;
{ $description "Reads until the next CRLF (carriage return followed by line feed) from the current input stream, throwing an error if CR is present without immediately being followed by LF." } ;
HELP: read-?crlf
{ $values { "seq" sequence } }
{ $description "Reads until the next LF (line feed) or CRLF (carriage return followed by line feed) from the current input stream, throwing an error if CR is present without immediately being followed by LF." } ;

View File

@ -6,3 +6,8 @@ USING: io.crlf tools.test io.streams.string io ;
[ "Hello, world.\r" [ read-crlf ] with-string-reader ] must-fail
[ f ] [ "" [ read-crlf ] with-string-reader ] unit-test
[ "" ] [ "\r\n" [ read-crlf ] with-string-reader ] unit-test
[ "foo\r" [ read-?crlf ] with-string-reader ] must-fail
[ f ] [ "" [ read-?crlf ] with-string-reader ] unit-test
[ "" ] [ "\n" [ read-?crlf ] with-string-reader ] unit-test
[ "foo" ] [ "foo\n" [ read-?crlf ] with-string-reader ] unit-test

View File

@ -9,3 +9,7 @@ IN: io.crlf
: read-crlf ( -- seq )
"\r" read-until
[ CHAR: \r assert= read1 CHAR: \n assert= ] [ f like ] if* ;
: read-?crlf ( -- seq )
"\r\n" read-until
[ CHAR: \r = [ read1 CHAR: \n assert= ] when ] [ f like ] if* ;