RE: Replacing characters (Matt: bug)

new topic     » goto parent     » topic index » view thread      » older message » newer message

Andy Serpa wrote:
> 
> Henri Goffin wrote:
> >> global function replace_in_string(sequence s, object o, object n)
> > 
> 
> The "so" sequence is not necessary.  You can compare the sequence 
> directly to the atom "o":
> 
> 	s += (s = o) * (n - o)
> 
> 

Of course this, while devoid of cleverness, is at least 10x faster (and 
does swaps too):

function replace_in_string(sequence s, object o, object n)
integer ni

	if atom(o) and atom(n) then
		for i = 1 to length(s) do
			if s[i] = o then
				s[i] = n
			end if
		end for
		return s
	
	elsif sequence(o) and sequence(n) and length(o) = length(n) then

		for i = 1 to length(s) do
			ni = find(s[i],o)
			if ni then
				s[i] = n[ni]
			end if
		end for
		return s
	else
		return -1
	end if
	
end function

new topic     » goto parent     » topic index » view thread      » older message » newer message

Search



Quick Links

User menu

Not signed in.

Misc Menu