translate( string, tr1, tr2 )
public
This is entirely Mike Stok’s beast
Show source
def Functions::translate( string, tr1, tr2 )
from = string(tr1)
to = string(tr2)
map = Hash.new
0.upto(from.length - 1) { |pos|
from_char = from[pos]
unless map.has_key? from_char
map[from_char] =
if pos < to.length
to[pos]
else
nil
end
end
}
string(string).unpack('U*').collect { |c|
if map.has_key? c then map[c] else c end
}.compact.pack('U*')
end