[m-rev.] for review: Modifications to string library implementation.

Fergus Henderson fjh at cs.mu.OZ.AU
Thu Dec 18 18:28:11 AEDT 2003


On 18-Dec-2003, James Goddard <goddardjames at yahoo.com> wrote:
> 
> Modified string library implementation.

A better summary would be "Got string__format working for Java".

> +% char_list_remove_suffix/3 : We use this instead of the more general
> +% list__remove_suffix so that string__format will not depend on unification.

Some more comments here would be helpful. Why don't we want string__format
to depend on unification?  (Your log message was more informative, but a
future maintainer might not think to look at the CVS logs.)

> +:- pred char_list_remove_suffix(list(char), list(char), list(char)).
> +:- mode char_list_remove_suffix(in, in, out) is semidet.
> +
> +char_list_remove_suffix(List, Suffix, Prefix) :-
> +	list__reverse(List, RevList),
> +	list__reverse(Suffix, RevSuffix),
> +	char_list_remove_suffix_reversed(RevList, RevSuffix, RevPrefix),
> +	list__reverse(RevPrefix, Prefix).
> +
> +:- pred char_list_remove_suffix_reversed(list(char), list(char), list(char)).
> +:- mode char_list_remove_suffix_reversed(in, in, out) is semidet.
> +
> +char_list_remove_suffix_reversed(ReversedList, [], ReversedList).
> +
> +char_list_remove_suffix_reversed([Char|RList], [Char|RSuffix], RPrefix) :-
> +	char_list_remove_suffix_reversed(RList, RSuffix, RPrefix).

This has those three calls to list__reverse, which will allocate a lot
of heap cells.  The following code, which is adapted from the code for
list__remove_suffix, does the same number of traversals, but allocates
less heap cells, so it is probably more efficient.

	char_list_remove_suffix(List, Suffix, Prefix) :-
		list__length(List, ListLength),
		list__length(Suffix, SuffixLength),
		PrefixLength = ListLength - SuffixLength,
		list__split_list(PrefixLength, List, Prefix, Rest),
		char_list_equal(Suffix, Rest).

	:- pred char_list_equal(list(char)::in, list(char)::in) is semidet.
	char_list_equal([], []).
	char_list_equal([X|Xs], [X|Ys]) :-
		char_list_equal(Xs, Ys).

Also, this change should be accompanied by a change to the test suite to
enable for grade java at least one test case that uses string__format.

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