[m-rev.] for review: factoring code in string.m

Zoltan Somogyi zs at csse.unimelb.edu.au
Thu Dec 13 19:43:00 AEDT 2007


library/string.m:
	Factor out duplicate tests in an effort to make the code more
	understandable (though it should be faster too).

Zoltan.

cvs diff: Diffing library
Index: library/string.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/library/string.m,v
retrieving revision 1.268
diff -u -b -r1.268 string.m
--- library/string.m	23 Nov 2007 07:35:58 -0000	1.268
+++ library/string.m	13 Dec 2007 05:28:57 -0000
@@ -4577,6 +4577,7 @@
     split_at_string(0, length(Needle), Needle, Total).
 
 :- func split_at_string(int, int, string, string) = list(string).
+
 split_at_string(StartAt, NeedleLen, Needle, Total) = Out :-
     ( sub_string_search_start(Total, Needle, StartAt, NeedlePos) ->
         BeforeNeedle = substring(Total, StartAt, NeedlePos-StartAt),
@@ -4741,12 +4742,11 @@
 :- mode value_to_revstrings_prio(in, in, in, in, in, out) is cc_multi.
 
 value_to_revstrings_prio(NonCanon, OpsTable, Priority, X, !Rs) :-
-    %
     % We need to special-case the builtin types:
     %   int, char, float, string
     %   type_info, univ, c_pointer, array
     %   and private_builtin.type_info
-    %
+
     ( dynamic_cast(X, String) ->
         add_revstring(term_io.quoted_string(String), !Rs)
     ; dynamic_cast(X, Char) ->
@@ -4840,22 +4840,29 @@
     ->
         add_revstring("[]", !Rs)
     ;
-        Functor = "{}",
-        Args = [BracedTerm]
+        Functor = "{}"
     ->
+        (
+            Args = [],
+            add_revstring("{}", !Rs)
+        ;
+            Args = [BracedTerm],
         add_revstring("{ ", !Rs),
-        value_to_revstrings(NonCanon, OpsTable, univ_value(BracedTerm), !Rs),
+            value_to_revstrings(NonCanon, OpsTable, univ_value(BracedTerm),
+                !Rs),
         add_revstring(" }", !Rs)
     ;
-        Functor = "{}",
-        Args = [BracedHead | BracedTail]
-    ->
+            Args = [BracedHead | BracedTail],
+            BracedTail = [_ | _],
         add_revstring("{", !Rs),
         arg_to_revstrings(NonCanon, OpsTable, BracedHead, !Rs),
         term_args_to_revstrings(NonCanon, OpsTable, BracedTail, !Rs),
         add_revstring("}", !Rs)
+        )
     ;
-        Args = [PrefixArg],
+        Args = [Arg]
+    ->
+        (
         ops.lookup_prefix_op(OpsTable, Functor, OpPriority, OpAssoc)
     ->
         maybe_add_revstring("(", Priority, OpPriority, !Rs),
@@ -4863,21 +4870,26 @@
         add_revstring(" ", !Rs),
         adjust_priority(OpPriority, OpAssoc, NewPriority),
         value_to_revstrings_prio(NonCanon, OpsTable, NewPriority,
-            univ_value(PrefixArg), !Rs),
+                univ_value(Arg), !Rs),
         maybe_add_revstring(")", Priority, OpPriority, !Rs)
     ;
-        Args = [PostfixArg],
         ops.lookup_postfix_op(OpsTable, Functor, OpPriority, OpAssoc)
     ->
         maybe_add_revstring("(", Priority, OpPriority, !Rs),
         adjust_priority(OpPriority, OpAssoc, NewPriority),
         value_to_revstrings_prio(NonCanon, OpsTable, NewPriority,
-            univ_value(PostfixArg), !Rs),
+                univ_value(Arg), !Rs),
         add_revstring(" ", !Rs),
         add_revstring(term_io.quoted_atom(Functor), !Rs),
         maybe_add_revstring(")", Priority, OpPriority, !Rs)
     ;
-        Args = [Arg1, Arg2],
+            plain_term_to_revstrings(NonCanon, OpsTable, Priority,
+                Functor, Args, !Rs)
+        )
+    ;
+        Args = [Arg1, Arg2]
+    ->
+        (
         ops.lookup_infix_op(OpsTable, Functor, OpPriority,
             LeftAssoc, RightAssoc)
     ->
@@ -4897,7 +4909,6 @@
             univ_value(Arg2), !Rs),
         maybe_add_revstring(")", Priority, OpPriority, !Rs)
     ;
-        Args = [Arg1, Arg2],
         ops.lookup_binary_prefix_op(OpsTable, Functor,
             OpPriority, FirstAssoc, SecondAssoc)
     ->
@@ -4913,6 +4924,26 @@
             univ_value(Arg2), !Rs),
         maybe_add_revstring(")", Priority, OpPriority, !Rs)
     ;
+            plain_term_to_revstrings(NonCanon, OpsTable, Priority,
+                Functor, Args, !Rs)
+        )
+    ;
+        plain_term_to_revstrings(NonCanon, OpsTable, Priority, Functor, Args,
+            !Rs)
+    ).
+
+:- pred plain_term_to_revstrings(noncanon_handling, ops.table,
+    ops.priority, string, list(univ), revstrings, revstrings).
+:- mode plain_term_to_revstrings(in(do_not_allow), in, in, in, in, in, out)
+    is det.
+:- mode plain_term_to_revstrings(in(canonicalize), in, in, in, in, in, out)
+    is det.
+:- mode plain_term_to_revstrings(in(include_details_cc), in, in, in, in,
+    in, out) is cc_multi.
+:- mode plain_term_to_revstrings(in, in, in, in, in, in, out)
+    is cc_multi.
+
+plain_term_to_revstrings(NonCanon, OpsTable, Priority, Functor, Args, !Rs) :-
         (
             Args = [],
             ops.lookup_op(OpsTable, Functor),
@@ -4936,7 +4967,6 @@
             add_revstring(")", !Rs)
         ;
             Args = []
-        )
     ).
 
 :- pred maybe_add_revstring(string::in, ops.priority::in, ops.priority::in,
--------------------------------------------------------------------------
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