def parse_haml_magic_comment(str)
      scanner = StringScanner.new(str.dup.force_encoding(Encoding::ASCII_8BIT))
      bom = scanner.scan(/\xEF\xBB\xBF/n)
      return bom unless scanner.scan(/-\s*#\s*/n)
      if coding = try_parse_haml_emacs_magic_comment(scanner)
        return bom, coding
      end

      return bom unless scanner.scan(/.*?coding[=:]\s*([\w-]+)/in)
      return bom, scanner[1]
    end
def parse_haml_magic_comment(str)
      scanner = StringScanner.new(str.dup.force_encoding(Encoding::ASCII_8BIT))
      bom = scanner.scan(/\xEF\xBB\xBF/n)
      return bom unless scanner.scan(/-\s*#\s*/n)
      if coding = try_parse_haml_emacs_magic_comment(scanner)
        return bom, coding
      end

      return bom unless scanner.scan(/.*?coding[=:]\s*([\w-]+)/in)
      return bom, scanner[1]
    end
Parses a magic comment at the beginning of a Haml file.
 The parsing rules are basically the same as Ruby's.

 @return [(Boolean, String or nil)]
   Whether the document begins with a UTF-8 BOM,
   and the declared encoding of the document (or nil if none is declared)