[m-rev.] diff: string__substring in Mercury

Yoann Dubreuil ydu at miscrit.be
Sat Sep 8 00:20:07 AEST 2001


Hi,

Thanks Ralph, it's really nicer than what i wrote.

===================================================================


Estimated hours taken: 1
User: ydu
Branches: main

library/string.m:
	Implementation of string__substring in Mercury


Index: string.m
===================================================================
RCS file: /home/mercury1/repository/mercury/library/string.m,v
retrieving revision 1.158
diff -u -r1.158 string.m
--- string.m	6 Sep 2001 12:05:20 -0000	1.158
+++ string.m	7 Sep 2001 14:15:47 -0000
@@ -449,7 +449,7 @@
 %-----------------------------------------------------------------------------%
 
 :- implementation.
-:- import_module bool, std_util, int, float, require.
+:- import_module bool, std_util, int, float, require, integer.
 
 :- pred string__to_int_list(string, list(int)).
 :- mode string__to_int_list(out, in) is det.
@@ -2115,6 +2115,18 @@
 %	string__substring(String, Start, Count, Substring):
 */
 
+string__substring(Str::in, Start::in, Count::in, SubStr::out) :-
+	End = min(Start + Count, string__length(Str)),
+	SubStr = string__from_char_list(strchars(Start, End, Str)).
+
+:- func strchars(int, int, string) = list(char).
+strchars(I, End, Str) =
+	( if ( I < 0 ; End =< I )
+		then []
+		else [string__index_det(Str, I) | 
+			strchars(I + 1, End, Str)]
+	).
+
 :- pragma foreign_proc("C",
 	string__substring(Str::in, Start::in, Count::in,
 		SubString::out),
@@ -2136,14 +2148,6 @@
 		SubString[Count] = '\\0';
 	}
 }").
-:- pragma foreign_proc("MC++",
-	string__substring(_Str::in, _Start::in, _Count::in,
-		_SubString::out),
-		[will_not_call_mercury, thread_safe],
-"{
-	mercury::runtime::Errors::SORRY(""c code for this function"");
-}").
-
 
 /*
 :- pred string__unsafe_substring(string, int, int, string).


--------------------------------------------------------------------------
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