わたなべです.

もうちょっとだけ jcode.rb を高速化しました.
#2 倍というのは嘘になってしまった.

--- lib/jcode.rb.orig Tue Jul 27 13:40:43 1999 +++ lib/jcode.rb Thu Aug 12 18:52:18 1999 @@ -78,16 +78,8 @@ - HashCache = {} - - def expand_ch_hash from, to = "" - key = from.intern.to_s + ":" + to.intern.to_s - return HashCache[key] if HashCache.key? key - afrom = _expand_ch(from) + def expand_ch_hash from, to h = {} - if to.length != 0 - ato = _expand_ch(to) - afrom.each_with_index do |x,i| h[x] = ato[i] || ato[-1] end - else - afrom.each do |x| h[x] = true end - end - HashCache[key] = h + afrom = _expand_ch(from) + ato = _expand_ch(to) + afrom.each_with_index do |x,i| h[x] = ato[i] || ato[-1] end + h end @@ -98,2 +90,7 @@ + HashCache = {} + TrPatternCache = {} + DeletePatternCache = {} + SqueezePatternCache = {} + public @@ -103,3 +100,3 @@ - pattern = /[#{bsquote(from)}]/ + pattern = TrPatternCache[from] ||= /[#{bsquote(from)}]/ if from[0] == ?^ @@ -108,3 +105,3 @@ else - h = expand_ch_hash(from, to) + h = HashCache[from + "::" + to] ||= expand_ch_hash(from, to) self.gsub!(pattern) do |c| h[c] end @@ -118,4 +115,3 @@ def delete!(del) - pattern = /[#{bsquote(del)}]+/ - self.gsub!(pattern, '') + self.gsub!(DeletePatternCache[del] ||= /[#{bsquote(del)}]+/, '') end @@ -129,3 +125,3 @@ if del - /([#{bsquote(del)}])\1+/ + SqueezePatternCache[del] ||= /([#{bsquote(del)}])\1+/ else @@ -143,3 +139,3 @@ - pattern = /([#{bsquote(from)}])\1+/ + pattern = SqueezePatternCache[from] ||= /([#{bsquote(from)}])\1+"/ if from[0] == ?^ @@ -148,3 +144,3 @@ else - h = expand_ch_hash(from, to) + h = HashCache[from + "::" + to] ||= expand_ch_hash(from, to) self.gsub!(pattern) do h[$1] end @@ -158,3 +154,3 @@ def chop! - self.gsub!(/(?:.|\n)\z/, '') + self.gsub!(/(?:.|\r?\n)\z/, '') end