]> gitweb.factorcode.org Git - factor.git/commitdiff
io.crlf: add use-crlf to support output streams with CRLF for EOL
authorAlexander Ilin <alex.ilin@protonmail.com>
Thu, 22 Jun 2023 16:58:43 +0000 (18:58 +0200)
committerJohn Benediktsson <mrjbq7@gmail.com>
Sun, 30 Jul 2023 22:50:52 +0000 (15:50 -0700)
basis/io/crlf/authors.txt
basis/io/crlf/crlf-docs.factor
basis/io/crlf/crlf-tests.factor
basis/io/crlf/crlf.factor
basis/io/crlf/summary.txt

index 33616a2d6aa065f7d11f75093520b59b37bd6b05..15f585ec3f7f67c9611c859a93aed72e428335b8 100644 (file)
@@ -1,2 +1,3 @@
 Daniel Ehrenberg
 Slava Pestov
+Alexander Ilin
index b1f4e0ee976281203fc787a143ae56e9de1e244d..3dac9c57b852cde47c9f636cd0e6ef26b2de4588 100644 (file)
@@ -1,6 +1,6 @@
-! Copyright (C) 2009 Daniel Ehrenberg
+! Copyright (C) 2009, 2023 Daniel Ehrenberg, Alexander Ilin
 ! See https://factorcode.org/license.txt for BSD license.
-USING: help.syntax help.markup sequences ;
+USING: help.syntax help.markup io sequences ;
 IN: io.crlf
 
 HELP: crlf
@@ -15,4 +15,7 @@ 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
+{ $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 f23eb55b6356e4004dcf6b8cf2e38a82ad07d908..5f06b076b391e028eb944eb9ac98a3e4c3f118a6 100644 (file)
@@ -26,3 +26,7 @@ USING: io.crlf tools.test io.streams.string io ;
 { "abcd" } [ "a\nb\r\ncd" [ 5 read-ignoring-crlf ] with-string-reader ] unit-test
 { "abcde" } [ "a\nb\r\ncd\r\ne" [ 5 read-ignoring-crlf ] with-string-reader ] unit-test
 { "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
+] unit-test
index e173a80173fca920a73325d768edbc36922c14d5..7d19113309f3d75293e5ca666276f195c601b759 100644 (file)
@@ -1,7 +1,8 @@
-! Copyright (C) 2009 Daniel Ehrenberg, Slava Pestov
+! Copyright (C) 2009, 2023 Daniel Ehrenberg, Slava Pestov, Alexander Ilin
 ! See https://factorcode.org/license.txt for BSD license.
-USING: byte-vectors io io.private kernel math namespaces sbufs
-sequences splitting ;
+USING: accessors byte-vectors delegate delegate.protocols
+destructors io io.private kernel math namespaces sbufs sequences
+splitting ;
 IN: io.crlf
 
 : crlf ( -- )
@@ -61,3 +62,18 @@ IN: io.crlf
 
 : read-ignoring-crlf ( n -- seq/f )
     input-stream get stream-read-ignoring-crlf ;
+
+TUPLE: crlf-stream underlying ;
+INSTANCE: crlf-stream output-stream
+CONSULT: output-stream-protocol crlf-stream underlying>> ;
+
+C: <crlf-stream> crlf-stream
+
+M: crlf-stream dispose underlying>> dispose ;
+
+M: crlf-stream stream-nl
+    CHAR: \r over stream-write1
+    CHAR: \n swap stream-write1 ;
+
+: use-crlf ( -- )
+    output-stream [ <crlf-stream> ] change ;
index 2fa6a6e2c1f65e9223519bd4aebe075f6f7ba6ee..09e84194614d5cbf1f904647e56ec17dfc514d4e 100644 (file)
@@ -1 +1 @@
-Writing and reading until \r\n
+Writing with and reading until \r\n