! 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
{ $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
{ "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
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 ;