Sets the encoding of string based on the magic comment
# File lib/rdoc/encoding.rb, line 73
def self.set_encoding string
first_line = string[/\A(?:#!.*\n)?.*\n/]
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