[ruby/irb] Retire magic-file.rb

(https://github.com/ruby/irb/pull/574)

`MagicFile` was introduced around v0.9.6, which was like 14~15 years ago.
It was needed because back then we needed to read a file's magic comment
to determine the encoding of it, and read it with that encoding.

Commit: 3ee79e89

But now we expect files to be encoded in UTF-8 and don't specify encoding
through magic comments anymore, `MagicFile` can be retired.
This commit is contained in:
Stan Lo 2023-04-28 18:20:16 +01:00 committed by git
parent d4dc149c3c
commit e9930b51b2
3 changed files with 1 additions and 42 deletions

View File

@ -4,8 +4,6 @@
# by Keiju ISHITSUKA(keiju@ishitsuka.com)
#
require_relative 'magic-file'
module IRB
# Outputs the irb help message, see IRB@Command+line+options.
def IRB.print_usage

View File

@ -5,7 +5,6 @@
#
require_relative 'src_encoding'
require_relative 'magic-file'
require_relative 'completion'
require 'io/console'
require 'reline'
@ -132,7 +131,7 @@ module IRB
# Creates a new input method object
def initialize(file)
super
@io = file.is_a?(IO) ? file : IRB::MagicFile.open(file)
@io = file.is_a?(IO) ? file : File.open(file)
@external_encoding = @io.external_encoding
end
# The file name of this input method, usually given during initialization.

View File

@ -1,38 +0,0 @@
# frozen_string_literal: false
module IRB
class << (MagicFile = Object.new)
# see parser_magic_comment in parse.y
ENCODING_SPEC_RE = %r"coding\s*[=:]\s*([[:alnum:]\-_]+)"
def open(path)
io = File.open(path, 'rb')
line = io.gets
line = io.gets if line[0,2] == "#!"
encoding = detect_encoding(line)
internal_encoding = encoding
encoding ||= IRB.default_src_encoding
io.rewind
io.set_encoding(encoding, internal_encoding)
if block_given?
begin
return (yield io)
ensure
io.close
end
else
return io
end
end
private
def detect_encoding(line)
return unless line[0] == ?#
line = line[1..-1]
line = $1 if line[/-\*-\s*(.*?)\s*-*-$/]
return nil unless ENCODING_SPEC_RE =~ line
encoding = $1
return encoding.sub(/-(?:mac|dos|unix)/i, '')
end
end
end