]> Repositorios git - scryer-prolog.git/commitdiff
Use '$skip_max_list'/4 in string_get_n_chars/4
authorbakaq <[email protected]>
Sat, 23 Sep 2023 04:43:45 +0000 (01:43 -0300)
committerbakaq <[email protected]>
Sun, 24 Sep 2023 17:32:44 +0000 (14:32 -0300)
src/lib/pio.pl

index 781f14c8e3ea1d59825ee7e3d6cff1a22c360249..8688e1c9f18fe9f14c90c8ee7f80cba7bb1d1a9c 100644 (file)
@@ -68,6 +68,9 @@ phrase_from_file(NT, File, Options) :-
                            close(Stream))
     ).
 
+% How many chars to read from stream and buffer in each step
+chars_to_read(4096).
+
 stream_to_lazy_list(Stream, Xs) :-
     stream_property(Stream, reposition(Rep)),
     (   Rep = true ->
@@ -83,7 +86,8 @@ reader_step_repositionable(Stream, Pos, Xs0) :-
     set_stream_position(Stream, Pos),
     (   at_end_of_stream(Stream)
     ->  Xs0 = []
-    ;   get_n_chars(Stream, 4096, Cs),
+    ;   chars_to_read(CharsToRead),
+        get_n_chars(Stream, CharsToRead, Cs),
         partial_string(Cs, Xs0, Xs),
         stream_to_lazy_list_repositionable(Stream, Xs)
     ).
@@ -96,7 +100,8 @@ render_step_buffer(Stream, Pos, Ls) :-
     set_stream_buffer_position(Stream, Pos),
     (   buffer_at_end_of_stream(Stream) ->
         Ls = []
-    ;   buffer_get_n_chars(Stream, 4096, Chars),
+    ;   chars_to_read(CharsToRead),
+        buffer_get_n_chars(Stream, CharsToRead, Chars),
         partial_string(Chars, Ls, Ls0),
         stream_to_lazy_list_buffer(Stream, Ls0)
     ).
@@ -143,7 +148,8 @@ buffer_prepare_for_n(Stream, BufferId, BufferPosId, BufferLenId, N) :-
             (   at_end_of_stream(Stream) ->
                 BufferTail = [],
                 bb_put(BufferId, Buffer)
-            ;   get_n_chars(Stream, 4096, Chars),
+            ;   chars_to_read(CharsToRead),
+                get_n_chars(Stream, CharsToRead, Chars),
                 length(Chars, NChars),
                 partial_string(Chars, BufferTail, _),
                 bb_put(BufferId, Buffer),
@@ -163,13 +169,16 @@ partial_string_last_tail(PartialString, PartialStringTail) :-
     ;   partial_string_last_tail(PartialStringTail0, PartialStringTail)
     ).
 
-string_get_n_chars([], _, _, []).
-string_get_n_chars([S|Ss], BufferPos, N, Chars) :-
-    (   BufferPos = 0 ->
-        string_get_n_chars_([S|Ss], N, Chars)
-    ;   BufferPos1 is BufferPos - 1,
-        string_get_n_chars(Ss, BufferPos1, N, Chars)
-    ).
+string_get_n_chars(String, Pos, N, Chars) :-
+    chars_to_read(CharsToRead),
+    (   CharsToRead < Pos -> 
+        % I have absolutely no idea why this is needed (maybe it's a bug?),
+        % but hey, it works.
+        '$skip_max_list'(_, Pos, String, String0),
+        '$skip_max_list'(_, CharsToRead, String0, String1)
+    ;   '$skip_max_list'(_, Pos, String, String1)
+    ),
+    string_get_n_chars_(String1, N, Chars).
 
 string_get_n_chars_([], _, []).
 string_get_n_chars_([S|Ss], N, Chars) :-