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

Fergus Henderson fjh at cs.mu.OZ.AU
Thu Jun 20 07:36:48 AEST 2002


On 13-Jun-2002, Peter Ross <pro at missioncriticalit.com> wrote:
> +:- pragma foreign_proc("MC++", string__to_char_list(Str::in, CharList::uo),
> +		[will_not_call_mercury, promise_pure, thread_safe], "{
> +        MR_Integer length, i; 
> +        MR_Word tmp;
> +        MR_Word prev;
> +
> +        length = Str->get_Length();
> +      
> +        MR_list_nil(prev);
> +
> +        for (i = length - 1; i >= 0; i--) {
> +		MR_list_cons(tmp, __box(Str->get_Chars(i)), prev);
> +		prev = tmp;
> +        }
> +        CharList = tmp;
> +}").

This code is broken.  It will return an uninitialized variable
when length == 0.

> +:- pragma foreign_proc("MC++", string__to_char_list(Str::uo, CharList::in),
> +		[will_not_call_mercury, promise_pure, thread_safe], "{
> +        System::Text::StringBuilder *tmp;
> +	MR_Char c;
> +       
> +        tmp = new System::Text::StringBuilder();
> +        while (1) {
> +            if (MR_list_is_cons(CharList)) {
> +		c = System::Convert::ToChar(MR_list_head(CharList));
> +                tmp->Append(c);
> +                CharList = MR_list_tail(CharList);
> +            } else {
> +                break;
> +            }
> +        }

That loop should be written as

	while (MR_list_is_cons(CharList)) {
		c = System::Convert::ToChar(MR_list_head(CharList));
                tmp->Append(c);
                CharList = MR_list_tail(CharList);
	}

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