[m-rev.] for review: Java implementation of string library

James Goddard goddardjames at yahoo.com
Thu Dec 11 18:06:23 AEDT 2003


Estimated hours taken: 5
Branches: main

Implemented some library functions for the string library in java.

library/string.m:

	Implemented the following predicates in java:

		using_sprintf/0
		string__lowlevel_float_to_string/2
		string__to_float/2
		string__unsafe_set_char/4
		string__unsafe_substring/4

	Created error messages for the following predicates:
	(they should never be called except by C implementation)

		int_length_modifer/0
		native_format_float/2
		native_format_int/2
		native_format_string/2
		native_format_char/2

Index: string.m
===================================================================
RCS file: /home/mercury1/repository/mercury/library/string.m,v
retrieving revision 1.209
diff -u -d -r1.209 string.m
--- string.m	2 Dec 2003 10:02:07 -0000	1.209
+++ string.m	11 Dec 2003 06:29:00 -0000
@@ -1751,6 +1751,13 @@
 			Spec)
 	).
 
+	% Are we using C's sprintf?  All backends other than C return false.
+	% Note that any backends which return true for using_sprintf/0 must
+	% also implement:	int_length_modifer/0
+	%			native_format_float/2
+	%			native_format_int/2
+	%			native_format_string/2
+	%			native_format_char/2
 :- pred using_sprintf is semidet.
 
 :- pragma foreign_proc("C", using_sprintf,
@@ -1763,6 +1770,11 @@
 "
 	SUCCESS_INDICATOR = false;
 ").
+:- pragma foreign_proc("Java", using_sprintf,
+	[will_not_call_mercury, promise_pure, thread_safe],
+"
+	succeeded = false;
+").
 
 	% Construct a format string suitable to passing to sprintf.
 :- func make_format_sprintf(list(char), maybe(list(char)),
@@ -1829,6 +1841,9 @@
 	MR_make_aligned_string(LengthModifier,
 		(MR_String) (MR_Word) MR_INTEGER_LENGTH_MODIFIER);
 }").
+% This predicate is only called if using_sprintf/0, so we produce an error
+% by default.
+int_length_modifer = _ :- error("string.int_length_modifer/0 not defined").
 
 	% Create a string from a float using the format string.
 	% Note it is the responsibility of the caller to ensure that the
@@ -1842,6 +1857,10 @@
 	Str = MR_make_string(MR_PROC_LABEL, FormatStr, (double) Val);
 	MR_restore_transient_hp();
 }").
+% This predicate is only called if using_sprintf/0, so we produce an error
+% by default.
+native_format_float(_, _) = _ :-
+	error("string.native_format_float/2 not defined").
 
 	% Create a string from a int using the format string.
 	% Note it is the responsibility of the caller to ensure that the
@@ -1855,6 +1874,10 @@
 	Str = MR_make_string(MR_PROC_LABEL, FormatStr, Val);
 	MR_restore_transient_hp();
 }").
+% This predicate is only called if using_sprintf/0, so we produce an error
+% by default.
+native_format_int(_, _) = _ :-
+	error("string.native_format_int/2 not defined").
 
 	% Create a string from a string using the format string.
 	% Note it is the responsibility of the caller to ensure that the
@@ -1868,6 +1891,10 @@
 	Str = MR_make_string(MR_PROC_LABEL, FormatStr, Val);
 	MR_restore_transient_hp();
 }").
+% This predicate is only called if using_sprintf/0, so we produce an error
+% by default.
+native_format_string(_, _) = _ :-
+	error("string.native_format_string/2 not defined").
 
 	% Create a string from a char using the format string.
 	% Note it is the responsibility of the caller to ensure that the
@@ -1881,6 +1908,10 @@
 	Str = MR_make_string(MR_PROC_LABEL, FormatStr, Val);
 	MR_restore_transient_hp();
 }").
+% This predicate is only called if using_sprintf/0, so we produce an error
+% by default.
+native_format_char(_, _) = _ :-
+	error("string.native_format_char/2 not defined").
 
 %-----------------------------------------------------------------------------%
 
@@ -2857,6 +2888,13 @@
 	FloatString = FloatVal.ToString(""R"");
 ").
 
+:- pragma foreign_proc("Java",
+	string__lowlevel_float_to_string(FloatVal::in, FloatString::uo),
+	[will_not_call_mercury, promise_pure, thread_safe],
+"
+	FloatString = java.lang.Double.toString(FloatVal);
+").
+
 :- pragma export(string__to_float(in, out), "ML_string_to_float").
 :- pragma foreign_proc("C",
 	string__to_float(FloatString::in, FloatVal::out),
@@ -2897,6 +2935,27 @@
 	}
 }").
 
+:- pragma foreign_proc("Java",
+	string__to_float(FloatString::in, FloatVal::out),
+	[will_not_call_mercury, promise_pure, thread_safe],
+"
+	FloatVal = 0.0;		// FloatVal must be initialized to suppress
+				// error messages when the predicate fails.
+
+	// leading or trailing whitespace is not allowed
+	if (FloatString.length() == 0 || FloatString.trim() != FloatString)
+	{
+		succeeded = false;
+	} else {
+		try {
+			FloatVal = java.lang.Double.parseDouble(FloatString);
+			succeeded = true;
+		} catch(java.lang.NumberFormatException e) {
+			succeeded = false;
+		}
+	}
+").
+
 /*-----------------------------------------------------------------------*/
 
 /*
@@ -3137,6 +3196,12 @@
 		System.Convert.ToString(Ch), 
 		Str0.Substring(Index + 1));
 ").
+:- pragma foreign_proc("Java",
+	string__unsafe_set_char(Ch::in, Index::in, Str0::in, Str::out),
+	[will_not_call_mercury, promise_pure, thread_safe],
+"
+	Str = Str0.substring(0,Index) + Ch + Str0.substring(Index+1);
+").
 
 /*
 :- pred string__unsafe_set_char(char, int, string, string).
@@ -3158,6 +3223,12 @@
 		System.Convert.ToString(Ch), 
 		Str0.Substring(Index + 1));
 ").
+:- pragma foreign_proc("Java",
+	string__unsafe_set_char(Ch::in, Index::in, Str0::di, Str::uo),
+	[will_not_call_mercury, promise_pure, thread_safe],
+"
+	Str = Str0 = Str0.substring(0,Index) + Ch + Str0.substring(Index+1);
+").
 */
 
 /*-----------------------------------------------------------------------*/
@@ -3441,6 +3512,12 @@
 "{
 	SubString = Str.Substring(Start, Count);
 }").
+:- pragma foreign_proc("Java",
+	string__unsafe_substring(Str::in, Start::in, Count::in, SubString::uo),
+	[will_not_call_mercury, promise_pure, thread_safe],
+"
+	SubString = Str.substring(Start, Start+Count+1);
+").
 
 /*
 :- pred string__split(string, int, string, 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