]> gitweb.factorcode.org Git - factor.git/commitdiff
io.crlf: add a word that reads an optional CR.
authorJohn Benediktsson <mrjbq7@gmail.com>
Wed, 12 Oct 2011 19:36:54 +0000 (12:36 -0700)
committerJohn Benediktsson <mrjbq7@gmail.com>
Wed, 12 Oct 2011 19:36:54 +0000 (12:36 -0700)
basis/io/crlf/crlf-docs.factor
basis/io/crlf/crlf-tests.factor
basis/io/crlf/crlf.factor

index ac7c8c324ebb39437cbdc36356c1051703951b9b..3b965a3867b6d8ae9ee0718009c30a08edb732b5 100644 (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." } ;
index 2412945ab3ec006b575e45acff7463a039aa4b5b..780a32d5017cf44bca95eaa11539f12f33e20d9b 100644 (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
index 29f10300de44283b42dde7d7ad0229909a29b378..25319200cde6ca6e4271e568bf45791c1f4f2443 100644 (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* ;