]> gitweb.factorcode.org Git - factor.git/commitdiff
io.crlf: tweak the interface slightly
authorJohn Benediktsson <mrjbq7@gmail.com>
Sun, 30 Jul 2023 22:56:53 +0000 (15:56 -0700)
committerJohn Benediktsson <mrjbq7@gmail.com>
Sun, 30 Jul 2023 22:56:53 +0000 (15:56 -0700)
basis/io/crlf/crlf-docs.factor
basis/io/crlf/crlf-tests.factor
basis/io/crlf/crlf.factor

index 3dac9c57b852cde47c9f636cd0e6ef26b2de4588..c9a5be4c7277538d795c0fd01292a2304103c630 100644 (file)
@@ -1,6 +1,6 @@
 ! Copyright (C) 2009, 2023 Daniel Ehrenberg, Alexander Ilin
 ! See https://factorcode.org/license.txt for BSD license.
-USING: help.syntax help.markup io sequences ;
+USING: help.syntax help.markup io quotations sequences ;
 IN: io.crlf
 
 HELP: crlf
@@ -15,7 +15,11 @@ 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." } ;
 
-HELP: use-crlf
+HELP: use-crlf-stream
+{ $description "Substitutes the current " { $link output-stream } " with a wrapper that outputs CR followed by LF for every " { $link stream-nl } " call (words like " { $link print } " and " { $link nl } " use that internally)." } ;
+
+HELP: with-crlf-stream
+{ $values { "quot" quotation } }
 { $description "Substitutes the current " { $link output-stream } " with a wrapper that outputs CR followed by LF for every " { $link stream-nl } " call (words like " { $link print } " and " { $link nl } " use that internally)." } ;
 
 { crlf>lf lf>crlf } related-words
index 5f06b076b391e028eb944eb9ac98a3e4c3f118a6..fbac840d7ead28a7c497bb8adbb478155aeb5a36 100644 (file)
@@ -28,5 +28,13 @@ USING: io.crlf tools.test io.streams.string io ;
 { "abcde" } [ "a\nb\r\ncd\r\ne\nfghi" [ 5 read-ignoring-crlf ] with-string-reader ] unit-test
 
 { "Hello\r\nworld.\r\n" } [
-    [ use-crlf "Hello" print "world." write nl ] with-string-writer
+    [ use-crlf-stream "Hello" print "world." write nl ] with-string-writer
+] unit-test
+
+{ "A\nB\r\nC\n" } [
+    [
+        "A" print
+        [ "B" print ] with-crlf-stream
+        "C" print
+    ] with-string-writer
 ] unit-test
index 7d19113309f3d75293e5ca666276f195c601b759..776eda394b8f213b6ab03d095553620ab4cbed73 100644 (file)
@@ -75,5 +75,8 @@ M: crlf-stream stream-nl
     CHAR: \r over stream-write1
     CHAR: \n swap stream-write1 ;
 
-: use-crlf ( -- )
+: with-crlf-stream ( quot -- )
+    [ output-stream [ get <crlf-stream> ] keep ] dip with-variable ; inline
+
+: use-crlf-stream ( -- )
     output-stream [ <crlf-stream> ] change ;