module LiveAST::Reader

Constants

MAGIC_COMMENT
UTF8_BOM

Public Class Methods

read(file) click to toggle source
# File lib/live_ast/reader.rb, line 9
def self.read(file)
  contents = File.read(file, encoding: "BINARY")

  utf8 = contents.sub!(UTF8_BOM, "") ? "UTF-8" : nil

  # magic comment overrides BOM
  encoding = contents[MAGIC_COMMENT, 1] || utf8 || "US-ASCII"

  contents.force_encoding(strip_special(encoding))
end
strip_special(encoding) click to toggle source
# File lib/live_ast/reader.rb, line 20
def self.strip_special(encoding)
  if /\Autf8-mac\Z/i.match?(encoding)
    "UTF8-MAC"
  else
    encoding.sub(/-(unix|dos|mac)\Z/i, "")
  end
end