Sets the encoding of string based on the magic comment
# File lib/rdoc/encoding.rb, line 78
def self.set_encoding string
string =~ /\A(?:#!.*\n)?(.*\n)/
first_line = $1
name = case first_line
when /^<\?xml[^?]*encoding=(["'])(.*?)\11// then $2
when /\b(?:en)?coding[=:]\s*([^\s;]+)/ then $1
else return
end
string.sub! first_line, ''
return unless Object.const_defined? :Encoding
enc = Encoding.find name
string.force_encoding enc if enc
end