fold_lines: more tuning, avoiding extra split_last;
authorwenzelm
Mon, 06 Oct 2008 22:41:21 +0200
changeset 28510 66b95e857bde
parent 28509 0ef08aa52f2e
child 28511 e79fad5c16a6
fold_lines: more tuning, avoiding extra split_last;
src/Pure/General/file.ML
--- a/src/Pure/General/file.ML	Mon Oct 06 22:35:03 2008 +0200
+++ b/src/Pure/General/file.ML	Mon Oct 06 22:41:21 2008 +0200
@@ -150,15 +150,15 @@
 (*scalable iterator -- avoid size limit of TextIO.inputAll, and overhead of many TextIO.inputLine*)
 fun fold_lines f path a = open_input (fn file =>
   let
-    val split = split_last o String.fields (fn c => str c = "\n");
-    fun read buf =
-      (case split (TextIO.input file) of
-        ([], "") => (case Buffer.content buf of "" => I | line => f line)
-      | ([], rest) => read (Buffer.add rest buf)
-      | (line :: lines, rest) =>
-          f (Buffer.content (Buffer.add line buf))
-          #> fold f lines
-          #> read (Buffer.add rest Buffer.empty));
+    fun read buf x =
+      (case TextIO.input file of
+        "" => (case Buffer.content buf of "" => x | line => f line x)
+      | input =>
+          (case String.fields (fn c => c = #"\n") input of
+            [rest] => read (Buffer.add rest buf) x
+          | line :: more => read_lines more (f (Buffer.content (Buffer.add line buf)) x)))
+    and read_lines [rest] x = read (Buffer.add rest Buffer.empty) x
+      | read_lines (line :: more) x = read_lines more (f line x);
   in read Buffer.empty a end) path;
 
 val read = open_input TextIO.inputAll;