]> Repositorios git - scryer-prolog.git/commitdiff
Added \raw_chars//1 to library(pio). Began using setup_call_cleanup/3 as suggested...
authorpanasenco <[email protected]>
Sun, 25 Apr 2021 04:29:50 +0000 (21:29 -0700)
committerpanasenco <[email protected]>
Sun, 25 Apr 2021 04:29:50 +0000 (21:29 -0700)
src/lib/pio.pl
src/tests/json/test_json.pl

index 1df9a796e50a6998ebcc06feacb26e465f2f1465..9e57dabe85f93ffc6898bcd78064e65386afb5e0 100644 (file)
@@ -1,5 +1,6 @@
 :- module(pio, [phrase_from_file/2,
-                phrase_from_file/3]).
+                phrase_from_file/3,
+                raw_chars//1]).
 
 :- use_module(library(dcgs)).
 :- use_module(library(error)).
@@ -43,3 +44,15 @@ reader_step(Stream, Pos, Xs0) :-
             partial_string(Cs, Xs0, Xs),
             stream_to_lazy_list(Stream, Xs)
         ).
+
+/*  Relate a character list to itself greedily - for reading raw file contents using `phrase_from_file/2`
+    A quick benchmark:
+    Greedy (recurse first):
+    ?- time(phrase_from_file(raw_chars(_), '/mnt/544KB.json')).
+       % CPU time: 22.471 seconds   
+    Generous (empty list first):
+    ?- time(phrase_from_file(raw_chars(_), '/mnt/544KB.json')).
+       % CPU time: 44.119 seconds
+*/
+raw_chars([C|Cs]) --> [C], raw_chars(Cs).
+raw_chars([]) --> [].
index 1c2347588accaea4aa822fd8c4bf2d18fa22428c..e03a3cfdcab2e068ff9cf1c8fbd0a3e49cfc96c9 100644 (file)
@@ -7,6 +7,7 @@
 :- use_module(library(lists)).
 :- use_module(library(os)).
 :- use_module(library(pio)).
+:- use_module(library(iso_ext)).
 :- use_module(library(time)).
 
 test_path(TestName, TestPath) :-
@@ -33,15 +34,15 @@ minify_sample_json :-
     name_parse("pass_everything.json", Json),
     time(once(phrase(json_chars(Json), MinChars))),
     test_path("pass_everything.min.json", MinPath),
-    open(MinPath, write, Stream),
-    format(Stream, "~s", [MinChars]),
-    close(Stream).
+    setup_call_cleanup(
+        open(MinPath, write, Stream),
+        format(Stream, "~s", [MinChars]),
+        close(Stream)
+    ).
 
 test_json_minify :-
     test_path("pass_everything.min.json", MinPath),
-    open(MinPath, read, Stream),
-    read_line_to_chars(Stream, RefChars, []),
-    close(Stream),
+    once(phrase_from_file(raw_chars(RefChars), MinPath)),
     name_parse("pass_everything.json", Json),
     time(once(phrase(json_chars(Json), MinChars))),
     RefChars = MinChars.