[m-rev.] diff: implement concat_string_list for .NET
Fergus Henderson
fjh at cs.mu.OZ.AU
Sun Feb 16 13:02:22 AEDT 2003
On 14-Feb-2003, Fergus Henderson <fjh at cs.mu.OZ.AU> wrote:
> On 14-Feb-2003, Zoltan Somogyi <zs at cs.mu.OZ.AU> wrote:
> > On 13-Feb-2003, Fergus Henderson <fjh at cs.mu.OZ.AU> wrote:
>
> > > +% This version is only used if there is no matching foreign_proc version.
> > > +% XXX why is this implemented in C anyway? Why not just use Mercury?
> > > +concat_string_list(StringsList, _Len, String) :-
> > > + String = string__append_list(StringsList).
> >
> > This Mercury implementation therefore has a very subtle bug.
>
> Well, actually that code will work fine on .NET and Java,
> since on those platforms, strings can contain embedded NUL characters.
Sorry, I was wrong about that. I didn't realize that `concat_string_list'
doesn't just concatenate the strings, it also includes their terminating
nulls. So the diff that I posted earlier won't work. But I have a new
version that will -- see below. I've also modified the code so that
it checks if the Mercury string representation can support embedded
nulls, and calls sorry/1 if it can't.
I'm going to go ahead and commit this.
----------
Estimated hours taken: 2
Branches: main
compiler/stack_layout.m:
Implement concat_strings in Mercury.
compiler/layout.m:
compiler/layout_out.m:
compiler/stack_layout.m:
Use a separate type `string_with_0s' rather than `string'
for strings which may contain null characters.
Workspace: /home/fjh/ws/hermes
Index: compiler/layout.m
===================================================================
RCS file: /home/mercury1/repository/mercury/compiler/layout.m,v
retrieving revision 1.10
diff -u -d -r1.10 layout.m
--- compiler/layout.m 15 Nov 2002 04:50:22 -0000 1.10
+++ compiler/layout.m 16 Feb 2003 01:54:44 -0000
@@ -39,6 +39,9 @@
:- import_module libs__trace_params.
:- import_module bool, std_util, list, assoc_list.
+ % This type is for strings which may contain embedded null characters.
+:- type string_with_0s ---> string_with_0s(string).
+
:- type layout_data
---> label_layout_data( % defines MR_Label_Layout
label :: label,
@@ -56,7 +59,7 @@
; module_layout_data( % defines MR_Module_Layout
module_name :: module_name,
string_table_size :: int,
- string_table :: string,
+ string_table :: string_with_0s,
proc_layout_names :: list(layout_name),
file_layouts :: list(file_layout_data),
trace_level :: trace_level,
Index: compiler/layout_out.m
===================================================================
RCS file: /home/mercury1/repository/mercury/compiler/layout_out.m,v
retrieving revision 1.17
diff -u -d -r1.17 layout_out.m
--- compiler/layout_out.m 15 Nov 2002 04:50:22 -0000 1.17
+++ compiler/layout_out.m 16 Feb 2003 01:54:44 -0000
@@ -931,7 +931,7 @@
%-----------------------------------------------------------------------------%
:- pred output_module_layout_data_defn(module_name::in, int::in,
- string::in, list(layout_name)::in, list(file_layout_data)::in,
+ string_with_0s::in, list(layout_name)::in, list(file_layout_data)::in,
trace_level::in, int::in, decl_set::in, decl_set::out,
io__state::di, io__state::uo) is det.
@@ -1006,7 +1006,7 @@
% empty string.
:- pred output_module_string_table(module_name::in,
- int::in, string::in, decl_set::in, decl_set::out,
+ int::in, string_with_0s::in, decl_set::in, decl_set::out,
io__state::di, io__state::uo) is det.
output_module_string_table(ModuleName, StringTableSize, StringTable,
@@ -1020,15 +1020,16 @@
{ decl_set_insert(DeclSet0, data_addr(layout_addr(TableName)),
DeclSet) }.
-:- pred output_module_string_table_chars(int::in, int::in, string::in,
+:- pred output_module_string_table_chars(int::in, int::in, string_with_0s::in,
io__state::di, io__state::uo) is det.
-output_module_string_table_chars(CurIndex, MaxIndex, String) -->
+output_module_string_table_chars(CurIndex, MaxIndex, StringWithNulls) -->
( { CurIndex mod 16 = 0 } ->
io__write_string("\n\t")
;
[]
),
+ { StringWithNulls = string_with_0s(String) },
{ string__unsafe_index(String, CurIndex, Char) },
io__write_char(''''),
c_util__output_quoted_char(Char),
@@ -1036,7 +1037,7 @@
( { CurIndex < MaxIndex } ->
io__write_string(", "),
output_module_string_table_chars(CurIndex + 1, MaxIndex,
- String)
+ StringWithNulls)
;
[]
).
Index: compiler/stack_layout.m
===================================================================
RCS file: /home/mercury1/repository/mercury/compiler/stack_layout.m,v
retrieving revision 1.73
diff -u -d -r1.73 stack_layout.m
--- compiler/stack_layout.m 15 Nov 2002 04:50:28 -0000 1.73
+++ compiler/stack_layout.m 16 Feb 2003 01:54:44 -0000
@@ -58,7 +58,7 @@
:- import_module backend_libs__rtti, ll_backend__layout.
:- import_module ll_backend__ll_pseudo_type_info, (parse_tree__inst).
:- import_module ll_backend__code_util.
-:- import_module assoc_list, bool, string, int, require.
+:- import_module assoc_list, bool, char, string, int, require.
:- import_module map, term, set, varset.
%---------------------------------------------------------------------------%
@@ -151,8 +151,11 @@
%---------------------------------------------------------------------------%
+ % concat_string_list appends a list of strings together,
+ % appending a null character after each string.
+ % The resulting string will contain embedded null characters.
:- pred stack_layout__concat_string_list(list(string)::in, int::in,
- string::out) is det.
+ string_with_0s::out) is det.
:- pragma c_header_code("
#include ""mercury_tags.h"" /* for MR_list_*() */
@@ -192,6 +195,25 @@
MR_fatal_error(msg);
}
}").
+
+% This version is only used if there is no matching foreign_proc version.
+% Note that this version only works if the Mercury implementation's
+% string representation allows strings to contain embedded null
+% characters. So we check that.
+concat_string_list(StringsList, _Len, string_with_0s(StringWithNulls)) :-
+ (
+ char__to_int(NullChar, 0),
+ NullCharString = string__char_to_string(NullChar),
+ string__length(NullCharString, 1)
+ ->
+ StringsWithNullsList = list__map(func(S) = S ++ NullCharString,
+ StringsList),
+ StringWithNulls = string__append_list(StringsWithNullsList)
+ ;
+ % the Mercury implementation's string representation
+ % doesn't support strings containing null characters
+ private_builtin.sorry("stack_layout.concat_string_list")
+ ).
%---------------------------------------------------------------------------%
--
Fergus Henderson <fjh at cs.mu.oz.au> | "I have always known that the pursuit
The University of Melbourne | of excellence is a lethal habit"
WWW: <http://www.cs.mu.oz.au/~fjh> | -- the last words of T. S. Garp.
--------------------------------------------------------------------------
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