# File rexml/functions.rb, line 213 def Functions::translate( string, tr1, tr2 ) from = string(tr1) to = string(tr2) # the map is our translation table. # # if a character occurs more than once in the # from string then we ignore the second & # subsequent mappings # # if a charactcer maps to nil then we delete it # in the output. This happens if the from # string is longer than the to string # # there's nothing about - or ^ being special in # http://www.w3.org/TR/xpath#function-translate # so we don't build ranges or negated classes 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