Skip to content

Add chars_stream/1 and chars_to_stream/{2,3} #2968

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 16 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 5 additions & 24 deletions src/lib/charsio.pl
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,9 @@
read_term_from_chars/3,
write_term_to_chars/3,
chars_base64/3,
char_stream/1,
chars_stream/1,
chars_to_stream/2,
chars_to_stream/3,
put_chars/2]).
chars_to_stream/3]).

:- use_module(library(dcgs)).
:- use_module(library(iso_ext)).
Expand Down Expand Up @@ -398,28 +397,10 @@
)
).

%% put_chars(+Stream, +Chars). %
%
% Writes a list of characters to a stream.
%
% `Stream` is the output stream, and `Chars` is the list of characters to write.
%
% Example:
%
% ```
% ?- put_chars(current_output, "hello").
% hello
% true.
% ```

put_chars(Stream, Chars) :-
must_be(chars, Chars),
maplist(put_char(Stream), Chars).

%% char_stream(-Stream)
%% chars_stream(-Stream)
% Stream is a character stream.

char_stream(Stream) :-
chars_stream(Stream) :-
'$memory_stream'(Stream).

%% chars_to_stream(+Chars, -Stream) :-
Expand All @@ -445,4 +426,4 @@
chars_to_stream(Chars, Stream, _) :-
must_be(chars, Chars),
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why delete this? The interface should be checked prior to any side-effects.

'$memory_stream'(Stream),
put_chars(Stream, Chars).
maplist(put_char(Stream), Chars).
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

format/3 should make this much faster, since it writes more at once.

6 changes: 3 additions & 3 deletions src/tests/charsio.pl
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@


test("can create string char stream",
( char_stream(Stream),
( chars_stream(Stream),
put_char(Stream, a),
get_char(Stream, C),
C=a
Expand All @@ -17,7 +17,7 @@

test("can spell simple word with char stream",
(
char_stream(Stream),
chars_stream(Stream),
put_char(Stream, c),
put_char(Stream, a),
put_char(Stream, t),
Expand All @@ -27,7 +27,7 @@

test("can read from and write to char stream",
(
char_stream(Stream),
chars_stream(Stream),
put_char(Stream, c),
put_char(Stream, a),
get_char(Stream, _C),
Expand Down