[m-rev.] string__foldl_substring (two diffs).

Ralph Becket rafe at cs.mu.OZ.AU
Tue Oct 29 18:06:34 AEDT 2002


Estimated hours taken: 0.5
Branches: main

library/string.m:
	string__foldl_substring was previously "unsafe"; it has now been
	made "safe" in the sense that
	    foldl_substring(Closure, String, Start, Count, Acc0, Acc)
	is now equivalent to
	    foldl(Closure, substring(String, Start, Count), Acc0, Acc).
	rather than using unsafe_substring/3.

Index: string.m
===================================================================
RCS file: /home/mercury1/repository/mercury/library/string.m,v
retrieving revision 1.182
diff -u -r1.182 string.m
--- string.m	29 Oct 2002 03:37:16 -0000	1.182
+++ string.m	29 Oct 2002 06:28:59 -0000
@@ -656,7 +656,24 @@
 	string__length(String, Length),
 	string__foldl_substring(Closure, String, 0, Length, Acc0, Acc).
 
-string__foldl_substring(Closure, String, I, Count, Acc0, Acc) :-
+string__foldl_substring(Closure, String, Start0, Count0, Acc0, Acc) :-
+	Start = max(0, Start0),
+	Count = min(Count0, length(String) - Start),
+	string__foldl_substring_2(Closure, String,Start, Count, Acc0, Acc).
+
+:- pred string__foldl_substring_2(pred(char, T, T), string, int, int, T, T).
+:- mode string__foldl_substring_2(pred(in, in, out) is det, in, in, in,
+		in, out) is det.
+:- mode string__foldl_substring_2(pred(in, di, uo) is det, in, in, in,
+		di, uo) is det.
+:- mode string__foldl_substring_2(pred(in, in, out) is semidet, in, in, in,
+		in, out) is semidet.
+:- mode string__foldl_substring_2(pred(in, in, out) is nondet, in, in, in,
+		in, out) is nondet.
+:- mode string__foldl_substring_2(pred(in, in, out) is multi, in, in, in,
+		in, out) is multi.
+
+string__foldl_substring_2(Closure, String, I, Count, Acc0, Acc) :-
 	( if 0 < Count then
 		Closure(string__unsafe_index(String, I), Acc0, Acc1),
 		string__foldl_substring(Closure, String, I + 1, Count - 1,

Estimated hours taken: 0.5
Branches: main

tests/general/string_foldl_substring.m:
tests/general/string_foldl_substring.exp:
	Added.

tests/general/Mmakefile:
	Added string_foldl_substring to test suite.

Index: Mmakefile
===================================================================
RCS file: /home/mercury1/repository/tests/general/Mmakefile,v
retrieving revision 1.42
diff -u -r1.42 Mmakefile
--- Mmakefile	28 Oct 2002 06:06:08 -0000	1.42
+++ Mmakefile	29 Oct 2002 07:01:10 -0000
@@ -53,6 +53,7 @@
 		set_test \
 		state_vars_tests \
 		state_vars_typeclasses \
+		string_foldl_substring \
 		string_foldr_substring \
 		string_format_test \
 		string_format_test_2 \
Index: string_foldl_substring.exp
===================================================================
RCS file: string_foldl_substring.exp
diff -N string_foldl_substring.exp
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ string_foldl_substring.exp	29 Oct 2002 06:32:33 -0000
@@ -0,0 +1,7 @@
+rev("Hello, World!",  0,  5) = "olleH"
+rev("Hello, World!",  0, 50) = "!dlroW ,olleH"
+rev("Hello, World!",  0, -5) = ""
+rev("Hello, World!", -5, 12) = "dlroW ,olleH"
+rev("Hello, World!", -5, 50) = "!dlroW ,olleH"
+rev("Hello, World!",  7,  0) = ""
+rev("Hello, World!", 50, 10) = ""
Index: string_foldl_substring.m
===================================================================
RCS file: string_foldl_substring.m
diff -N string_foldl_substring.m
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ string_foldl_substring.m	29 Oct 2002 06:57:06 -0000
@@ -0,0 +1,53 @@
+%-----------------------------------------------------------------------------%
+% string_foldl_substring.m
+% Ralph Becket <rafe at cs.mu.oz.au>
+% Mon Oct 28 16:32:19 EST 2002
+% vim: ft=mercury ff=unix ts=4 sw=4 et wm=0 tw=0
+%
+%-----------------------------------------------------------------------------%
+
+:- module string_foldl_substring.
+
+:- interface.
+
+:- import_module io.
+
+
+
+:- pred main(io::di, io::uo) is det.
+
+%-----------------------------------------------------------------------------%
+%-----------------------------------------------------------------------------%
+
+:- implementation.
+
+:- import_module string, char, list, int.
+
+%-----------------------------------------------------------------------------%
+
+main(!IO) :-
+    io__write_strings([
+            "rev(\"Hello, World!\",  0,  5) = \"",
+               rev("Hello, World!",  0,  5),
+        "\"\nrev(\"Hello, World!\",  0, 50) = \"",
+               rev("Hello, World!",  0, 50),
+        "\"\nrev(\"Hello, World!\",  0, -5) = \"",
+               rev("Hello, World!",  0, -5),
+        "\"\nrev(\"Hello, World!\", -5, 12) = \"",
+               rev("Hello, World!", -5, 12),
+        "\"\nrev(\"Hello, World!\", -5, 50) = \"",
+               rev("Hello, World!", -5, 50),
+        "\"\nrev(\"Hello, World!\",  7,  0) = \"",
+               rev("Hello, World!",  7,  0),
+        "\"\nrev(\"Hello, World!\", 50, 10) = \"",
+               rev("Hello, World!", 50, 10),
+        "\"\n"
+    ], !IO).
+
+:- func rev(string, int, int) = string.
+
+rev(S, I, N) =
+    from_char_list(foldl_substring(func(X, Xs) = [X | Xs], S, I, N, [])).
+
+%-----------------------------------------------------------------------------%
+%-----------------------------------------------------------------------------%
--------------------------------------------------------------------------
mercury-reviews mailing list
post:  mercury-reviews at cs.mu.oz.au
administrative address: owner-mercury-reviews at cs.mu.oz.au
unsubscribe: Address: mercury-reviews-request at cs.mu.oz.au Message: unsubscribe
subscribe:   Address: mercury-reviews-request at cs.mu.oz.au Message: subscribe
--------------------------------------------------------------------------



More information about the reviews mailing list