From 24232cb095a2b545183b22d4c39fcce107a503b0 Mon Sep 17 00:00:00 2001 From: John Benediktsson Date: Wed, 12 Oct 2011 12:36:54 -0700 Subject: [PATCH] io.crlf: add a word that reads an optional CR. --- basis/io/crlf/crlf-docs.factor | 6 +++++- basis/io/crlf/crlf-tests.factor | 5 +++++ basis/io/crlf/crlf.factor | 4 ++++ 3 files changed, 14 insertions(+), 1 deletion(-) diff --git a/basis/io/crlf/crlf-docs.factor b/basis/io/crlf/crlf-docs.factor index ac7c8c324e..3b965a3867 100644 --- a/basis/io/crlf/crlf-docs.factor +++ b/basis/io/crlf/crlf-docs.factor @@ -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." } ; diff --git a/basis/io/crlf/crlf-tests.factor b/basis/io/crlf/crlf-tests.factor index 2412945ab3..780a32d501 100644 --- a/basis/io/crlf/crlf-tests.factor +++ b/basis/io/crlf/crlf-tests.factor @@ -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 diff --git a/basis/io/crlf/crlf.factor b/basis/io/crlf/crlf.factor index 29f10300de..25319200cd 100644 --- a/basis/io/crlf/crlf.factor +++ b/basis/io/crlf/crlf.factor @@ -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* ;