]> Repositorios git - scryer-prolog.git/commitdiff
ADDED: format/3, writing formatted output to a stream
authorMarkus Triska <[email protected]>
Fri, 22 May 2020 15:21:50 +0000 (17:21 +0200)
committerMarkus Triska <[email protected]>
Fri, 22 May 2020 15:23:51 +0000 (17:23 +0200)
Both binary and text streams are supported.

README.md
src/prolog/lib/format.pl

index 9ad292398e80079533f8f782ce4c10cbf5ca8728..2a477a283a5bd4649a741a2ba48e64bc211b66ac 100644 (file)
--- a/README.md
+++ b/README.md
@@ -353,7 +353,7 @@ The modules that ship with Scryer&nbsp;Prolog are also called
 * [`format`](src/prolog/lib/format.pl)
   The nonterminal `format_//2` is used to describe formatted output,
   arranging arguments according to a given format&nbsp;string.
-  The predicates `format/2`, `portray_clause/1` and `listing/1`
+  The predicates `format/[2,3]`, `portray_clause/1` and `listing/1`
   provide formatted *impure* output.
 * [`assoc`](src/prolog/lib/assoc.pl)
   providing `empty_assoc/1`, `get_assoc/3`, `put_assoc/4` etc.
index 17ab0bfc1f1dcb7c2c8658ee021e07ccbb3ed7f3..c0ef87c110b54ebb1750af412056f750312249a2 100644 (file)
    The predicate format/2 is like format_//2, except that it outputs
    the text on the terminal instead of describing it declaratively.
 
+   format/3, used as format(Stream, FormatString, Arguments), outputs
+   the described string to the given Stream. If Stream is a binary
+   stream, then the code of each emitted character must be in 0..255.
+
    If at all possible, format_//2 should be used, to stress pure parts
    that enable easy testing etc. If necessary, you can emit the list Ls
    with maplist(write, Ls).
@@ -68,6 +72,7 @@
 
 :- module(format, [format_//2,
                    format/2,
+                   format/3,
                    portray_clause/1,
                    listing/1
                   ]).
@@ -359,6 +364,14 @@ format(Fs, Args) :-
         phrase(format_(Fs, Args), Cs),
         maplist(write, Cs).
 
+format(Stream, Fs, Args) :-
+        phrase(format_(Fs, Args), Cs),
+        (   stream_property(Stream, type(binary)) ->
+            maplist(char_code, Cs, Bytes),
+            maplist(put_byte(Stream), Bytes)
+        ;   maplist(put_char(Stream), Cs)
+        ).
+
 /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 ?- phrase(cells("hello", [], 0, []), Cs).