[m-rev.] diff: assorted java foreign_procs

Peter Wang novalazy at gmail.com
Wed Jun 3 17:08:31 AEST 2009


Branches: main

compiler/timestamp.m:
        Add Java implementation of gmtime_to_timestamp_2.

library/io.m:
library/time.m:
        Make Java implementation of io.file_modification_time return the
        correct type.

library/string.m:
        Make generic version of string.to_char_list avoid bounds checks
        per character.

        Add Java implementations of string.semidet_from_char_list,
        string.append_list.

diff --git a/compiler/timestamp.m b/compiler/timestamp.m
index 7f7fd59..33d48d5 100644
--- a/compiler/timestamp.m
+++ b/compiler/timestamp.m
@@ -122,6 +122,19 @@ gmtime_to_timestamp(tm(Year, Month, MD, Hrs, Min,
Sec, YD, WD, DST)) =
     Result = t.ToString(format_str);
 }").

+:- pragma foreign_proc("Java",
+    gmtime_to_timestamp_2(Yr::in, Mnt::in, MD::in, Hrs::in, Min::in,
+        Sec::in, _YD::in, _WD::in, _N::in) = (Result::out),
+    [will_not_call_mercury, promise_pure],
+"
+    java.util.GregorianCalendar cal =
+        new java.util.GregorianCalendar(Yr + 1900, Mnt - 1, MD, Hrs, Min, Sec);
+    java.text.SimpleDateFormat sdf =
+        new java.text.SimpleDateFormat(""yyyy-MM-dd HH:mm:ss"");
+    java.util.Date date = new java.util.Date(cal.getTimeInMillis());
+    Result = sdf.format(date);
+").
+
 :- func maybe_dst_to_int(maybe(dst)) = int.

 maybe_dst_to_int(M) = N :-
diff --git a/library/io.m b/library/io.m
index b0e93a7..d97a0f4 100644
--- a/library/io.m
+++ b/library/io.m
@@ -2883,7 +2883,7 @@ io.file_modification_time(File, Result, !IO) :-
         Msg = ""lastModified() failed: "" + e.getMessage();
         Status = 0;
     }
-    Time = date;
+    Time = mercury.time.ML_construct_time_t(date);
 ").

 :- pragma foreign_proc("Erlang",
diff --git a/library/string.m b/library/string.m
index 0a96a1b..8ff79df 100644
--- a/library/string.m
+++ b/library/string.m
@@ -1343,16 +1343,18 @@ string.to_char_list(Str::uo, CharList::in) :-
 ").

 string.to_char_list_2(Str, CharList) :-
-    string.to_char_list_3(Str, 0, CharList).
+    string.to_char_list_3(Str, string.length(Str) - 1, [], CharList).

-:- pred string.to_char_list_3(string::in, int::in, list(char)::uo) is det.
+:- pred string.to_char_list_3(string::in, int::in,
+    list(char)::di, list(char)::uo) is det.

-string.to_char_list_3(Str, Index, CharList) :-
-    ( string.index(Str, Index, Char) ->
-        string.to_char_list_3(Str, Index + 1, CharList0),
-        CharList = [Char | CharList0]
+string.to_char_list_3(Str, Index, CharList0, CharList) :-
+    ( Index >= 0 ->
+        string.unsafe_index(Str, Index, Char),
+        CharList1 = [Char | CharList0],
+        string.to_char_list_3(Str, Index - 1, CharList1, CharList)
     ;
-        CharList = []
+        CharList = CharList0
     ).

 %-----------------------------------------------------------------------------%
@@ -1417,6 +1419,23 @@ string.from_char_list(Chars::in, Str::uo) :-
     Str[size] = '\\0';
 }").

+:- pragma foreign_proc("Java",
+    string.semidet_from_char_list(CharList::in, Str::uo),
+    [will_not_call_mercury, promise_pure, thread_safe, will_not_modify_trail,
+        does_not_affect_liveness],
+"
+    java.lang.StringBuilder sb = new StringBuilder();
+    while (CharList instanceof mercury.list.List_1.F_cons_2) {
+        mercury.list.List_1.F_cons_2 cons =
+            (mercury.list.List_1.F_cons_2) CharList;
+        char c = ((java.lang.Character) cons.F1).charValue();
+        sb.append(c);
+        CharList = cons.F2;
+    }
+    Str = sb.toString();
+    succeeded = true;
+").
+
 :- pragma foreign_proc("Erlang",
     string.semidet_from_char_list(CharList::in, Str::uo),
     [will_not_call_mercury, promise_pure, thread_safe, will_not_modify_trail,
@@ -1630,6 +1649,22 @@ string.append_list(Lists, string.append_list(Lists)).
     Str[len] = '\\0';
 }").

+:- pragma foreign_proc("Java",
+    string.append_list(Strs::in) = (Str::uo),
+    [will_not_call_mercury, promise_pure, thread_safe, will_not_modify_trail,
+        does_not_affect_liveness],
+"
+    java.lang.StringBuilder sb = new java.lang.StringBuilder();
+    while (Strs instanceof mercury.list.List_1.F_cons_2) {
+        mercury.list.List_1.F_cons_2 cons =
+            (mercury.list.List_1.F_cons_2) Strs;
+        java.lang.String s = (java.lang.String) cons.F1;
+        sb.append(s);
+        Strs = cons.F2;
+    }
+    Str = sb.toString();
+").
+
 :- pragma foreign_proc("Erlang",
     string.append_list(Strs::in) = (Str::uo),
     [will_not_call_mercury, promise_pure, thread_safe, will_not_modify_trail,
@@ -1638,6 +1673,15 @@ string.append_list(Lists, string.append_list(Lists)).
     Str = list_to_binary(Strs)
 ").

+string.append_list(Strs::in) = (Str::uo) :-
+    (
+        Strs = [X | Xs],
+        Str = X ++ append_list(Xs)
+    ;
+        Strs = [],
+        Str = ""
+    ).
+
     % We implement string.join_list in C as this minimises the amount of
     % garbage created.
 :- pragma foreign_proc("C",
@@ -1689,15 +1733,6 @@ string.append_list(Lists, string.append_list(Lists)).
     Str[len] = '\\0';
 }").

-string.append_list(Strs::in) = (Str::uo) :-
-    (
-        Strs = [X | Xs],
-        Str = X ++ append_list(Xs)
-    ;
-        Strs = [],
-        Str = ""
-    ).
-
 string.join_list(_, []) = "".
 string.join_list(Sep, [H | T]) = H ++ string.join_list_2(Sep, T).

diff --git a/library/time.m b/library/time.m
index 42e499a..a3ba3e8 100644
--- a/library/time.m
+++ b/library/time.m
@@ -1008,6 +1008,8 @@ time.ctime(Time) = asctime(localtime(Time)).
     "ML_construct_time_t").
 :- pragma foreign_export("IL", construct_time_t(in) = out,
     "ML_construct_time_t").
+:- pragma foreign_export("Java", construct_time_t(in) = out,
+    "ML_construct_time_t").

 construct_time_t(T) = time_t(T).
--------------------------------------------------------------------------
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