[m-rev.] for review: add string.line type

Peter Wang wangp at students.csse.unimelb.edu.au
Fri Apr 20 10:53:06 AEST 2007


On 2007-04-19, Julien Fischer <juliensf at csse.unimelb.edu.au> wrote:
> 
> I don't see why the text_file one should wait.

Ok.  Interdiff follows (committed).

diff -u NEWS NEWS
--- NEWS	19 Apr 2007 02:13:48 -0000
+++ NEWS	20 Apr 2007 00:47:53 -0000
@@ -137,9 +137,10 @@
 * We have added a function construct.get_functor_lex/2 which converts
   an ordinal functor number into a lexicographic functor number.
 
-* We have added a type `string.line' and made input streams an instance
-  of stream.reader/4 with that unit type.  This means stream.get/4
-  can be used to efficiently read lines.
+* We have added the types `string.line' and `string.text_file' and made
+  input streams instances of stream.reader/4 with those unit types.
+  This means stream.get/4 can be used to efficiently read lines
+  and files as string.
 
 Changes to the Mercury compiler:
 
diff -u library/io.m library/io.m
--- library/io.m	19 Apr 2007 02:13:48 -0000
+++ library/io.m	20 Apr 2007 00:47:53 -0000
@@ -148,7 +148,8 @@
 :- pred io.read_line(io.result(list(char))::out, io::di, io::uo) is det.
 
     % Reads a line from the current input stream, returns the result
-    % as a string.
+    % as a string. See the documentation for `string.line' for the
+    % definition of a line.
     %
 :- pred io.read_line_as_string(io.result(string)::out, io::di, io::uo) is det.
 
@@ -237,7 +238,8 @@
     io::di, io::uo) is det.
 
     % Reads a line from specified stream, returning the
-    % result as a string.
+    % result as a string. See the documentation for `string.line' for
+    % the definition of a line.
     %
 :- pred io.read_line_as_string(io.input_stream::in, io.result(string)::out,
     io::di, io::uo) is det.
@@ -1462,6 +1464,7 @@
 :- instance stream.input(io.input_stream, io, io.error).
 :- instance stream.reader(io.input_stream, char, io, io.error).
 :- instance stream.reader(io.input_stream, line, io, io.error).
+:- instance stream.reader(io.input_stream, text_file, io, io.error).
 
 :- instance stream.line_oriented(io.input_stream, io).
 :- instance stream.putback(io.input_stream, char, io, io.error).
@@ -9092,6 +9095,21 @@
     )
 ].
 
+:- instance stream.reader(io.input_stream, text_file, io, io.error)
+    where
+[
+    ( get(Stream, Result, !IO) :-
+        io.read_file_as_string(Stream, Result0, !IO),
+        (
+            Result0 = ok(String),
+            Result = ok(text_file(String))
+        ;
+            Result0 = error(_PartialString, Error),
+            Result = error(Error)
+        )
+    )
+].
+
 :- instance stream.putback(io.input_stream, char, io, io.error) where
 [
     pred(unget/4) is io.putback_char
diff -u library/string.m library/string.m
--- library/string.m	19 Apr 2007 02:13:48 -0000
+++ library/string.m	20 Apr 2007 00:47:53 -0000
@@ -41,11 +41,23 @@
 %-----------------------------------------------------------------------------%
 
     % This type is used for defining stream typeclass instances where the raw
-    % string type would be ambiguous.
+    % string type would be ambiguous. A line is:
+    %
+    % - a possibly empty sequence of non-newline characters terminated by a
+    %   newline character; or
+    % - a non-empty sequence of non-newline characters terminated by the end
+    %   of the file.
     %
 :- type line
     --->    line(string).
 
+    % This type is used for defining stream typeclass instances where the raw
+    % string type would be ambiguous. A text file is a possibly empty sequence
+    % of characters terminated by the end of file.
+    %
+:- type text_file
+    --->    text_file(string).
+
     % Determine the length of a string.
     % An empty string has length zero.
     %
--------------------------------------------------------------------------
mercury-reviews mailing list
Post messages to:       mercury-reviews at csse.unimelb.edu.au
Administrative Queries: owner-mercury-reviews at csse.unimelb.edu.au
Subscriptions:          mercury-reviews-request at csse.unimelb.edu.au
--------------------------------------------------------------------------



More information about the reviews mailing list