module SimpleCov::SourceFile::SourceLoader

def call(filename)

def call(filename)
  lines = []
  # The default encoding is UTF-8
  File.open(filename, "rb:UTF-8") do |file|
    current_line = file.gets
    if shebang?(current_line)
      lines << current_line
      current_line = file.gets
    end
    read_lines(file, lines, current_line)
  end
end

def ensure_remove_undefs(file_lines)

properly, so it has to be done here.
these options on `file.set_encoding` doesn't seem to work
nice to have and work around a JRuby incompatibility. Setting
invalid/undef replace are technically not really necessary but
def ensure_remove_undefs(file_lines)
  file_lines.each do |line|
    # simplecov:disable — defensive: only fires for non-UTF-8 source files
    line.encode!("UTF-8", invalid: :replace, undef: :replace) unless line.encoding == Encoding::UTF_8
    # simplecov:enable
  end
end

def read_lines(file, lines, current_line)

def read_lines(file, lines, current_line)
  return lines unless current_line
  set_encoding_based_on_magic_comment(file, current_line)
  lines.concat([current_line], ensure_remove_undefs(file.readlines))
end

def set_encoding_based_on_magic_comment(file, line)

shebang.
Encoding magic comment must be placed at first line except for
def set_encoding_based_on_magic_comment(file, line)
  if (match = RUBY_FILE_ENCODING_MAGIC_COMMENT_REGEX.match(line))
    file.set_encoding(match[1], "UTF-8")
  end
end

def shebang?(line)

def shebang?(line)
  SHEBANG_REGEX.match?(line)
end