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

Peter Ross pro at missioncriticalit.com
Mon Dec 6 03:33:14 AEDT 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, []),

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