[m-rev.] for review: improve string__replace and string__replace_all

Ralph Becket rafe at cs.mu.OZ.AU
Mon Dec 6 11:40:20 AEDT 2004


Peter Ross, Sunday,  5 December 2004:
> On Sun, Jun 27, 2004 at 01:48:57PM +1000, Ralph Becket wrote:
> > Peter Ross, Friday, 25 June 2004:
> > > Hi,
> > > 
> > > For anyone to review.
> > > 
> > > One question what should the result of 
> > >     string__replace("aaa bbbb ccccc aaa", "", "**", Result) 
> > > be?
> > 
> > I think the result should be
> > "**a**a**a** **b**b**b**b** **c**c**c**c**c** **a**a**a**"
> > 
> 
> ===================================================================
> 
> 
> library/string.m:
> 	Make string__replace_all with the pattern "" be consistent
> 	with the behaviour of string__replace.
> 
> Index: library/string.m
> ===================================================================
> RCS file: /home/mercury1/repository/mercury/library/string.m,v
> retrieving revision 1.221
> diff -u -r1.221 string.m
> --- library/string.m	10 Nov 2004 07:12:38 -0000	1.221
> +++ library/string.m	5 Dec 2004 16:26:38 -0000
> @@ -609,7 +609,9 @@
>  
>  string__replace_all(Str, Pat, Subst, Result) :-
>  	( Pat = "" ->
> -		copy(Str, Result)
> +		F = (func(C, L) = [char_to_string(C) ++ Subst | L]),
> +		Foldl = string__foldl(F, Str, []),
> +		Result = append_list([Subst | list__reverse(Foldl)])
>  	;
>  		PatLength = string__length(Pat),
>  		ReversedChunks = replace_all(Str, Pat, Subst, PatLength, 0, []),

Just as a minor efficiency hack, I'd move the char_to_string(C) computation 
out of the lambda - loop invariant hoisting isn't turned on until -O5 or
6 IIRC and even then it might not hoist it.

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