]> Repositorios git - scryer-prolog.git/commitdiff
ADDED: read_line_to_chars/3, reading up to and including "\n" from a stream, yielding...
authorMarkus Triska <[email protected]>
Sat, 13 Jun 2020 07:06:57 +0000 (09:06 +0200)
committerMarkus Triska <[email protected]>
Sat, 13 Jun 2020 07:14:44 +0000 (09:14 +0200)
This works for both binary and text streams.

src/lib/charsio.pl

index e57a40aaa0f9b5706b091e65eae1b24fe6e2cc83..4e82f23be8eaa50d026a2446176b3574ad1f635a 100644 (file)
@@ -1,6 +1,7 @@
 :- module(charsio, [char_type/2,
                     chars_utf8bytes/2,
                     get_single_char/1,
+                    read_line_to_chars/3,
                     read_term_from_chars/2,
                     write_term_to_chars/3]).
 
@@ -182,3 +183,14 @@ continuation(Code, Chars, Nb) --> [Byte],
 % invalid continuation byte
 % each remaining continuation byte (if any) will raise 0xFFFD too
 continuation(_, ['\xFFFD\'|T], _) --> [_], decode_utf8(T).
+
+
+read_line_to_chars(Stream, Cs0, Cs) :-
+        '$get_n_chars'(Stream, 1, Char), % this also works for binary streams
+        (   Char == [] -> Cs0 = Cs
+        ;   Char = [C],
+            Cs0 = [C|Rest],
+            (   C == '\n' -> Rest = Cs
+            ;   read_line_to_chars(Stream, Rest, Cs)
+            )
+        ).