1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

* lib/irb/init.rb (IRB.opt_parse): (M17N) adds -U and -E as command

line options. [ruby-dev:37161]. Fixes #711.
  improved long optinos.

* lib/irb/init.rb (IRB.set_encoding): new subroutine for IRB.opt_parse

* lib/irb/input-method.rb (IRB::StdioInputMethod): (M17N) regards
  scripts that user types as encoded in the external_encoding.

* lib/irb/input-method.rb (IRB::ReadlineInputMethod): ditto.

* lib/irb/input-method.rb (IRB::FileInputMethod): (M17N) respects
  magic comment.

* lib/irb/help.rb (IRB.print_usage): (M17N) respects magic comment
  in the resource file.

* lib/irb/lc/help-message: adds -U and -E.

* lib/irb/lc/ja/help-message: ditto. re-encoded from ISO-2022-JP into UTF-8.

* lib/irb/lc/ja/encoding_aliases.rb: new file. provides Japanese specific
  character encoding name table for backward compatibility.

* lib/irb/lc/ja/error.rb: re-eoncoded from ISO-2022-JP into UTF-8.
  magic comment.

* lib/irb/locale.rb: integrated with Ruby 1.9's M17N support.

* lib/irb/magic-file.rb: new file. utility to handle files with magic
  comment and opens in the correct encoding.

* lib/irb/ruby-lex.rb (RubyLex#each_top_level_statement): recovers
  character encoding for a statement after it lexed so that irb can
  eval the statement in correct encoding.

* lib/irb/src_encoding.rb: new file. utility.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@20862 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
yugui 2008-12-18 13:09:26 +00:00
parent 52d481d8de
commit 5c1bd53c92
12 changed files with 271 additions and 107 deletions

View file

@ -1,3 +1,43 @@
Thu Dec 18 19:31:54 2008 Yuki Sonoda (Yugui) <yugui@yugui.jp>
* lib/irb/init.rb (IRB.opt_parse): (M17N) adds -U and -E as command
line options. [ruby-dev:37161]. Fixes #711.
improved long optinos.
* lib/irb/init.rb (IRB.set_encoding): new subroutine for IRB.opt_parse
* lib/irb/input-method.rb (IRB::StdioInputMethod): (M17N) regards
scripts that user types as encoded in the external_encoding.
* lib/irb/input-method.rb (IRB::ReadlineInputMethod): ditto.
* lib/irb/input-method.rb (IRB::FileInputMethod): (M17N) respects
magic comment.
* lib/irb/help.rb (IRB.print_usage): (M17N) respects magic comment
in the resource file.
* lib/irb/lc/help-message: adds -U and -E.
* lib/irb/lc/ja/help-message: ditto. re-encoded from ISO-2022-JP into UTF-8.
* lib/irb/lc/ja/encoding_aliases.rb: new file. provides Japanese specific
character encoding name table for backward compatibility.
* lib/irb/lc/ja/error.rb: re-eoncoded from ISO-2022-JP into UTF-8.
magic comment.
* lib/irb/locale.rb: integrated with Ruby 1.9's M17N support.
* lib/irb/magic-file.rb: new file. utility to handle files with magic
comment and opens in the correct encoding.
* lib/irb/ruby-lex.rb (RubyLex#each_top_level_statement): recovers
character encoding for a statement after it lexed so that irb can
eval the statement in correct encoding.
* lib/irb/src_encoding.rb: new file. utility.
Thu Dec 18 21:12:28 2008 Takeyuki FUJIOKA <xibbar@ruby-lang.org> Thu Dec 18 21:12:28 2008 Takeyuki FUJIOKA <xibbar@ruby-lang.org>
* lib/cgi/session.rb: fix bug for ignore session_id option. * lib/cgi/session.rb: fix bug for ignore session_id option.

View file

@ -9,24 +9,27 @@
# #
# #
require 'irb/magic-file'
module IRB module IRB
def IRB.print_usage def IRB.print_usage
lc = IRB.conf[:LC_MESSAGES] lc = IRB.conf[:LC_MESSAGES]
path = lc.find("irb/help-message") path = lc.find("irb/help-message")
space_line = false space_line = false
File.foreach(path) do IRB::MagicFile.open(path){|f|
|l| f.each_line do |l|
if /^\s*$/ =~ l if /^\s*$/ =~ l
lc.puts l unless space_line lc.puts l unless space_line
space_line = true space_line = true
next next
end
space_line = false
l.sub!(/#.*$/, "")
next if /^\s*$/ =~ l
lc.puts l
end end
space_line = false }
l.sub!(/#.*$/, "")
next if /^\s*$/ =~ l
lc.puts l
end
end end
end end

View file

@ -139,6 +139,11 @@ module IRB
when /^-I(.+)?/ when /^-I(.+)?/
opt = $1 || ARGV.shift opt = $1 || ARGV.shift
load_path.concat(opt.split(File::PATH_SEPARATOR)) if opt load_path.concat(opt.split(File::PATH_SEPARATOR)) if opt
when '-U'
set_encoding("UTF-8", "UTF-8")
when /^-E(.+)?/, /^--encoding(?:=(.+))?/
opt = $1 || ARGV.shift
set_encoding(*opt.split(':', 2))
when "--inspect" when "--inspect"
@CONF[:INSPECT_MODE] = true @CONF[:INSPECT_MODE] = true
when "--noinspect" when "--noinspect"
@ -155,8 +160,9 @@ module IRB
@CONF[:VERBOSE] = true @CONF[:VERBOSE] = true
when "--noverbose" when "--noverbose"
@CONF[:VERBOSE] = false @CONF[:VERBOSE] = false
when "--prompt-mode", "--prompt" when /^--prompt-mode(?:=(.+))?/, /^--prompt(?:=(.+))?/
prompt_mode = ARGV.shift.upcase.tr("-", "_").intern opt = $1 || ARGV.shift
prompt_mode = opt.upcase.tr("-", "_").intern
@CONF[:PROMPT_MODE] = prompt_mode @CONF[:PROMPT_MODE] = prompt_mode
when "--noprompt" when "--noprompt"
@CONF[:PROMPT_MODE] = :NULL @CONF[:PROMPT_MODE] = :NULL
@ -166,14 +172,14 @@ module IRB
@CONF[:PROMPT_MODE] = :SIMPLE @CONF[:PROMPT_MODE] = :SIMPLE
when "--tracer" when "--tracer"
@CONF[:USE_TRACER] = true @CONF[:USE_TRACER] = true
when "--back-trace-limit" when /^--back-trace-limit(?:=(.+))?/
@CONF[:BACK_TRACE_LIMIT] = ARGV.shift.to_i @CONF[:BACK_TRACE_LIMIT] = ($1 || ARGV.shift).to_i
when "--context-mode" when /^--context-mode(?:=(.+))?/
@CONF[:CONTEXT_MODE] = ARGV.shift.to_i @CONF[:CONTEXT_MODE] = ($1 || ARGV.shift).to_i
when "--single-irb" when "--single-irb"
@CONF[:SINGLE_IRB] = true @CONF[:SINGLE_IRB] = true
when "--irb_debug" when /^--irb_debug=(?:=(.+))?/
@CONF[:DEBUG_LEVEL] = ARGV.shift.to_i @CONF[:DEBUG_LEVEL] = ($1 || ARGV.shift).to_i
when "-v", "--version" when "-v", "--version"
print IRB.version, "\n" print IRB.version, "\n"
exit 0 exit 0
@ -181,6 +187,12 @@ module IRB
require "irb/help" require "irb/help"
IRB.print_usage IRB.print_usage
exit 0 exit 0
when "--"
if opt = ARGV.shfit
@CONF[:SCRIPT] = opt
$0 = opt
end
break
when /^-/ when /^-/
IRB.fail UnrecognizedSwitch, opt IRB.fail UnrecognizedSwitch, opt
else else
@ -195,6 +207,7 @@ module IRB
end end
end end
$LOAD_PATH.unshift(*load_path) $LOAD_PATH.unshift(*load_path)
end end
# running config # running config
@ -253,4 +266,21 @@ module IRB
end end
end end
DefaultEncodings = Struct.new(:external, :internal)
class << IRB
private
def set_encoding(extern, intern = nil)
verbose, $VERBOSE = $VERBOSE, nil
Encoding.default_external = extern unless extern.nil? || extern.empty?
Encoding.default_internal = intern unless intern.nil? || intern.empty?
@CONF[:ENCODINGS] = IRB::DefaultEncodings.new(extern, intern)
[$stdin, $stdout, $stderr].each do |io|
io.set_encoding(extern, intern)
end
@CONF[:LC_MESSAGES].instance_variable_set(:@encoding, extern)
ensure
$VERBOSE = verbose
end
end
end end

View file

@ -8,6 +8,9 @@
# #
# #
# #
require 'irb/src_encoding'
require 'irb/magic-file'
module IRB module IRB
# #
# InputMethod # InputMethod
@ -41,15 +44,19 @@ module IRB
super super
@line_no = 0 @line_no = 0
@line = [] @line = []
@stdin = IO.open(STDIN.to_i, :external_encoding => IRB.conf[:LC_MESSAGES].encoding, :internal_encoding => "-")
@stdout = IO.open(STDOUT.to_i, :external_encoding => IRB.conf[:LC_MESSAGES].encoding, :internal_encoding => "-")
end end
def gets def gets
print @prompt print @prompt
@line[@line_no += 1] = $stdin.gets line = @stdin.gets
p line.encoding
@line[@line_no += 1] = line
end end
def eof? def eof?
$stdin.eof? @stdin.eof?
end end
def readable_atfer_eof? def readable_atfer_eof?
@ -59,12 +66,16 @@ module IRB
def line(line_no) def line(line_no)
@line[line_no] @line[line_no]
end end
def encoding
@stdin.external_encoding
end
end end
class FileInputMethod < InputMethod class FileInputMethod < InputMethod
def initialize(file) def initialize(file)
super super
@io = open(file) @io = IRB::MagicFile.open(file)
end end
attr_reader :file_name attr_reader :file_name
@ -78,6 +89,10 @@ module IRB
# print @prompt, l # print @prompt, l
l l
end end
def encoding
@io.external_encoding
end
end end
begin begin
@ -90,11 +105,14 @@ module IRB
@line_no = 0 @line_no = 0
@line = [] @line = []
@eof = false @eof = false
@stdin = IO.open(STDIN.to_i, :external_encoding => IRB.conf[:LC_MESSAGES].encoding, :internal_encoding => "-")
@stdout = IO.open(STDOUT.to_i, :external_encoding => IRB.conf[:LC_MESSAGES].encoding, :internal_encoding => "-")
end end
def gets def gets
Readline.input = STDIN Readline.input = @stdin
Readline.output = STDOUT Readline.output = @stdout
if l = readline(@prompt, false) if l = readline(@prompt, false)
HISTORY.push(l) if !l.empty? HISTORY.push(l) if !l.empty?
@line[@line_no += 1] = l + "\n" @line[@line_no += 1] = l + "\n"
@ -115,6 +133,10 @@ module IRB
def line(line_no) def line(line_no)
@line[line_no] @line[line_no]
end end
def encoding
@stdin.external_encoding
end
end end
rescue LoadError rescue LoadError
end end

View file

@ -1,3 +1,4 @@
# -*- coding: US-ASCII -*-
# #
# irb/lc/help-message.rb - # irb/lc/help-message.rb -
# $Release Version: 0.9.5$ # $Release Version: 0.9.5$
@ -14,6 +15,8 @@ Usage: irb.rb [options] [programfile] [arguments]
-d Set $DEBUG to true (same as `ruby -d') -d Set $DEBUG to true (same as `ruby -d')
-r load-module Same as `ruby -r' -r load-module Same as `ruby -r'
-I path Specify $LOAD_PATH directory -I path Specify $LOAD_PATH directory
-U Same as `ruby -U`
-E enc Same as `ruby -E`
--inspect Use `inspect' for output (default except for bc mode) --inspect Use `inspect' for output (default except for bc mode)
--noinspect Don't use inspect for output --noinspect Don't use inspect for output
--readline Use Readline extension module --readline Use Readline extension module
@ -32,3 +35,4 @@ Usage: irb.rb [options] [programfile] [arguments]
value is 16. value is 16.
--irb_debug n Set internal debug level to n (not for popular use) --irb_debug n Set internal debug level to n (not for popular use)
-v, --version Print the version of irb -v, --version Print the version of irb
# vim:fileencoding=us-ascii

View file

@ -0,0 +1,8 @@
module IRB
class Locale
@@legacy_encoding_alias_map = {
'ujis' => Encoding::EUC_JP,
'euc' => Encoding::EUC_JP
}.freeze
end
end

View file

@ -1,4 +1,4 @@
# # -*- coding: utf-8 -*-
# irb/lc/ja/error.rb - # irb/lc/ja/error.rb -
# $Release Version: 0.9.5$ # $Release Version: 0.9.5$
# $Revision$ # $Revision$
@ -13,14 +13,15 @@ require "e2mmap"
module IRB module IRB
# exceptions # exceptions
extend Exception2MessageMapper extend Exception2MessageMapper
def_exception :UnrecognizedSwitch, '$B%9%$%C%A(B(%s)$B$,J,$j$^$;$s(B' def_exception :UnrecognizedSwitch, 'スイッチ(%s)が分りません'
def_exception :NotImplementedError, '`%s\'$B$NDj5A$,I,MW$G$9(B' def_exception :NotImplementedError, '`%s\'の定義が必要です'
def_exception :CantReturnToNormalMode, 'Normal$B%b!<%I$KLa$l$^$;$s(B.' def_exception :CantReturnToNormalMode, 'Normalモードに戻れません.'
def_exception :IllegalParameter, '$B%Q%i%a!<%?(B(%s)$B$,4V0c$C$F$$$^$9(B.' def_exception :IllegalParameter, 'パラメータ(%s)が間違っています.'
def_exception :IrbAlreadyDead, 'Irb$B$O4{$K;`$s$G$$$^$9(B.' def_exception :IrbAlreadyDead, 'Irbは既に死んでいます.'
def_exception :IrbSwitchedToCurrentThread, '$B%+%l%s%H%9%l%C%I$K@Z$jBX$o$j$^$7$?(B.' def_exception :IrbSwitchedToCurrentThread, 'カレントスレッドに切り替わりました.'
def_exception :NoSuchJob, '$B$=$N$h$&$J%8%g%V(B(%s)$B$O$"$j$^$;$s(B.' def_exception :NoSuchJob, 'そのようなジョブ(%s)はありません.'
def_exception :CantShiftToMultiIrbMode, 'multi-irb mode$B$K0\$l$^$;$s(B.' def_exception :CantShiftToMultiIrbMode, 'multi-irb modeに移れません.'
def_exception :CantChangeBinding, '$B%P%$%s%G%#%s%0(B(%s)$B$KJQ99$G$-$^$;$s(B.' def_exception :CantChangeBinding, 'バインディング(%s)に変更できません.'
def_exception :UndefinedPromptMode, '$B%W%m%s%W%H%b!<%I(B(%s)$B$ODj5A$5$l$F$$$^$;$s(B.' def_exception :UndefinedPromptMode, 'プロンプトモード(%s)は定義されていません.'
end end
# vim:fileencoding=utf-8

View file

@ -1,4 +1,4 @@
# # -*- coding: utf-8 -*-
# irb/lc/ja/help-message.rb - # irb/lc/ja/help-message.rb -
# $Release Version: 0.9.5$ # $Release Version: 0.9.5$
# $Revision$ # $Revision$
@ -9,27 +9,31 @@
# #
# #
Usage: irb.rb [options] [programfile] [arguments] Usage: irb.rb [options] [programfile] [arguments]
-f ~/.irbrc $B$rFI$_9~$^$J$$(B. -f ~/.irbrc を読み込まない.
-m bc$B%b!<%I(B($BJ,?t(B, $B9TNs$N7W;;$,$G$-$k(B) -m bcモード(分数, 行列の計算ができる)
-d $DEBUG $B$r(Btrue$B$K$9$k(B(ruby -d $B$HF1$8(B) -d $DEBUG をtrueにする(ruby -d と同じ)
-r load-module ruby -r $B$HF1$8(B. -r load-module ruby -r と同じ.
-I path $LOAD_PATH $B$K(B path $B$rDI2C$9$k(B. -I path $LOAD_PATH に path を追加する.
--inspect $B7k2L=PNO$K(Binspect$B$rMQ$$$k(B(bc$B%b!<%I0J30$O%G%U%)%k%H(B). -U ruby -U と同じ.
--noinspect $B7k2L=PNO$K(Binspect$B$rMQ$$$J$$(B. -E enc ruby -E と同じ.
--readline readline$B%i%$%V%i%j$rMxMQ$9$k(B. --inspect 結果出力にinspectを用いる(bcモード以外はデフォルト).
--noreadline readline$B%i%$%V%i%j$rMxMQ$7$J$$(B. --noinspect 結果出力にinspectを用いない.
--readline readlineライブラリを利用する.
--noreadline readlineライブラリを利用しない.
--prompt prompt-mode/--prompt-mode prompt-mode --prompt prompt-mode/--prompt-mode prompt-mode
$B%W%m%s%W%H%b!<%I$r@ZBX$($^$9(B. $B8=:_Dj5A$5$l$F$$$k%W(B プロンプトモードを切替えます. 現在定義されているプ
$B%m%s%W%H%b!<%I$O(B, default, simple, xmp, inf-ruby$B$,(B ロンプトモードは, default, simple, xmp, inf-rubyが
$BMQ0U$5$l$F$$$^$9(B. 用意されています.
--inf-ruby-mode emacs$B$N(Binf-ruby-mode$BMQ$N%W%m%s%W%HI=<($r9T$J$&(B. $BFC(B --inf-ruby-mode emacsのinf-ruby-mode用のプロンプト表示を行なう. 特
$B$K;XDj$,$J$$8B$j(B, readline$B%i%$%V%i%j$O;H$o$J$/$J$k(B. に指定がない限り, readlineライブラリは使わなくなる.
--simple-prompt $BHs>o$K%7%s%W%k$J%W%m%s%W%H$rMQ$$$k%b!<%I$G$9(B. --simple-prompt 非常にシンプルなプロンプトを用いるモードです.
--noprompt $B%W%m%s%W%HI=<($r9T$J$o$J$$(B. --noprompt プロンプト表示を行なわない.
--tracer $B%3%^%s%I<B9T;~$K%H%l!<%9$r9T$J$&(B. --tracer コマンド実行時にトレースを行なう.
--back-trace-limit n --back-trace-limit n
$B%P%C%/%H%l!<%9I=<($r%P%C%/%H%l!<%9$NF,$+$i(B n, $B8e$m(B バックトレース表示をバックトレースの頭から n, 後ろ
$B$+$i(Bn$B$@$19T$J$&(B. $B%G%U%)%k%H$O(B16 からnだけ行なう. デフォルトは16
--irb_debug n irb$B$N%G%P%C%0%G%P%C%0%l%Y%k$r(Bn$B$K@_Dj$9$k(B($BMxMQ$7$J(B --irb_debug n irbのデバッグデバッグレベルをnに設定する(利用しな
$B$$J}$,L5Fq$G$7$g$&(B). い方が無難でしょう).
-v, --version irb$B$N%P!<%8%g%s$rI=<($9$k(B -v, --version irbのバージョンを表示する
# vim:fileencoding=utf-8

View file

@ -8,43 +8,51 @@
# #
# #
# #
autoload :Kconv, "kconv"
module IRB module IRB
class Locale class Locale
@RCS_ID='-$Id$-' @RCS_ID='-$Id$-'
JPDefaultLocale = "ja" LOCALE_NAME_RE = %r[
(?<language>[[:alpha:]]{2})
(?:_
(?<territory>[[:alpha:]]{2,3})
(?:\.
(?<codeset>[^@]+)
)?
)?
(?:@
(?<modifier>.*)
)?
]x
LOCALE_DIR = "/lc/" LOCALE_DIR = "/lc/"
@@legacy_encoding_alias_map = {}.freeze
def initialize(locale = nil) def initialize(locale = nil)
@lang = locale || ENV["IRB_LANG"] || ENV["LC_MESSAGES"] || ENV["LC_ALL"] || ENV["LANG"] || "C" @locale = locale || ENV["IRB_LANG"] || ENV["LC_MESSAGES"] || ENV["LC_ALL"] || ENV["LANG"] || "C"
end if m = LOCALE_NAME_RE.match(@locale)
@lang, @territory, @encoding_name, @modifier = m[:language], m[:territory], m[:codeset], m[:modifier]
attr_reader :lang if @encoding_name
begin; load 'irb/encoding_aliases.rb' rescue LoadError; end
def lc2kconv(lang) if @encoding = @@legacy_encoding_alias_map[@encoding_name]
case lang warn "%s is obsolete. use %s" % ["#{@lang}_#{@territory}.#{@encoding_name}", "#{@lang}_#{@territory}.#{@encoding.name}"]
when "ja_JP.ujis", "ja_JP.euc", "ja_JP.eucJP", "ja_JP.EUC-JP" end
Kconv::EUC @encoding = Encoding.find(@encoding_name) rescue nil
when "ja_JP.sjis", "ja_JP.SJIS" end
Kconv::SJIS
when /ja_JP.utf-?8/i
Kconv::UTF8
end end
@encoding ||= (Encoding.find('locale') rescue Encoding::ASCII_8BIT)
end end
private :lc2kconv
attr_reader :lang, :territory, :encoding, :modifieer
def String(mes) def String(mes)
mes = super(mes) mes = super(mes)
case @lang if @encoding
when /^ja/ mes.encode(@encoding)
mes = Kconv::kconv(mes, lc2kconv(@lang))
else else
mes mes
end end
mes
end end
def format(*opts) def format(*opts)
@ -106,27 +114,20 @@ module IRB
dir = "" if dir == "." dir = "" if dir == "."
base = File.basename(file) base = File.basename(file)
if /^ja(_JP)?$/ =~ @lang if dir[0] == ?/ #/
back, @lang = @lang, "C" lc_path = search_file(dir, base)
return real_load(lc_path, priv) if lc_path
end end
begin
if dir[0] == ?/ #/ for path in $:
lc_path = search_file(dir, base) lc_path = search_file(path + "/" + dir, base)
return real_load(lc_path, priv) if lc_path return real_load(lc_path, priv) if lc_path
end
for path in $:
lc_path = search_file(path + "/" + dir, base)
return real_load(lc_path, priv) if lc_path
end
ensure
@lang = back if back
end end
raise LoadError, "No such file to load -- #{file}" raise LoadError, "No such file to load -- #{file}"
end end
def real_load(path, priv) def real_load(path, priv)
src = self.String(File.read(path)) src = MagicFile.open(path){|f| f.read}
if priv if priv
eval("self", TOPLEVEL_BINDING).extend(Module.new {eval(src, nil, path)}) eval("self", TOPLEVEL_BINDING).extend(Module.new {eval(src, nil, path)})
else else
@ -152,29 +153,39 @@ module IRB
end end
def search_file(path, file) def search_file(path, file)
if File.exist?(p1 = path + lc_path(file, "C")) each_sublocale do |lc|
if File.exist?(p2 = path + lc_path(file)) full_path = path + lc_path(file, lc)
return p2 return full_path if File.exist?(full_path)
else
end
return p1
else
end end
nil nil
end end
private :search_file private :search_file
def lc_path(file = "", lc = @lang) def lc_path(file = "", lc = @locale)
case lc if lc.nil?
when "C"
LOCALE_DIR + file LOCALE_DIR + file
when /^ja/
LOCALE_DIR + "ja/" + file
else else
LOCALE_DIR + @lang + "/" + file LOCALE_DIR + @lang + "/" + file
end end
end end
private :lc_path private :lc_path
def each_sublocale
if @lang
if @territory
if @encoding_name
yield "#{@lang}_#{@territory}.#{@encoding_name}@#{@modifier}" if @modifier
yield "#{@lang}_#{@territory}.#{@encoding_name}"
end
yield "#{@lang}_#{@territory}@#{@modifier}" if @modifier
yield "#{@lang}_#{@territory}"
end
yield "#{@lang}@#{@modifier}" if @modifier
yield "#{@lang}"
end
yield nil
end
private :each_sublocale
end end
end end

36
lib/irb/magic-file.rb Normal file
View file

@ -0,0 +1,36 @@
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)
encoding ||= default_src_encoding
io.rewind
io.set_encoding(encoding, nil)
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

View file

@ -240,6 +240,7 @@ class RubyLex
end end
end end
if @line != "\n" if @line != "\n"
@line.force_encoding(@io.encoding)
yield @line, @exp_line_no yield @line, @exp_line_no
end end
break unless l break unless l

4
lib/irb/src_encoding.rb Normal file
View file

@ -0,0 +1,4 @@
# DO NOT WRITE ANY MAGIC COMMENT HERE.
def default_src_encoding
return __ENCODING__
end