From: Robert Klemme Date: 2007-08-03T22:07:07+09:00 Subject: Re: Fast implementation of XML escape 2007/8/3, Bob Hutchison : > Hi Robert, > > Thanks for the response. > > > On 3-Aug-07, at 8:47 AM, Robert Klemme wrote: > > > > You are on the right track. There is just one thing to improve: get > > rid of "case": > > > > class Converter > > MAP = { > > "&" => "&", > > # ... > > } > > > > def self.convert(s) > > s.gsub(/[&<>'"]/) do |m| > > MAP[m] || "ERROR" > > end > > end > > end > > > My version3 was: > > @@convert = { > '&' => '&', > '<' => '<', > '>' => '>', > "'" => ''', > '"' => '"' > } > > def version3(input) > # all kinds of other processing of input simulated by the input.dup > result = input.dup > > result.gsub!(/[&<>'"]/) do | match | > @@convert[match] > end > > return result > end > > which is pretty much what you suggested, but it takes 29.71 seconds > (rather than 8.74 seconds). The overhead of the block form is significant. It probably pays off when you have longer strings. That depends. Kind regards robert