Hi,

At Sat, 10 Nov 2007 10:51:50 +0900,
Yukihiro Matsumoto wrote [ruby-core:13356]:
> Compatibility problem.  I didn't think about that.  So at least -K
> should set the default source encoding for files without magic
> comments.

Then, separate it from -E option?

This patch lets -E/--encoding option have priority over -K option
for the primary encoding (a).  An alternative would be the last
one of -K and -E options is in effect for it (b).

(a)
  $ ruby -Ke -Eutf-8	# S=EUC-JP, P=UTF-8
  $ ruby -Eutf-8 -Ke	# same as above

(b)
  $ ruby -Ke -Eutf-8	# same as above
  $ ruby -Eutf-8 -Ke	# S=EUC-JP, P=EUC-JP

where S and P denote the script and primary encodings,
respectively.


Index: ruby.c =================================================================== --- ruby.c (revision 13858) +++ ruby.c (working copy) @@ -80,5 +80,5 @@ struct cmdline_options { char *script; VALUE e_script; - int enc_index; + int prim_enc_index, src_enc_index; }; @@ -751,5 +751,5 @@ proc_options(int argc, char **argv, stru } if (enc) { - opt->enc_index = rb_enc_find_index(rb_enc_name(enc)); + opt->src_enc_index = rb_enc_find_index(rb_enc_name(enc)); } s++; @@ -820,5 +820,5 @@ proc_options(int argc, char **argv, stru } encoding: - if ((opt->enc_index = rb_enc_find_index(s)) < 0) { + if ((opt->prim_enc_index = rb_enc_find_index(s)) < 0) { rb_raise(rb_eRuntimeError, "unknown encoding name - %s", s); } @@ -979,6 +979,6 @@ process_options(VALUE arg) parser = rb_parser_new(); if (opt->e_script) { - if (opt->enc_index >= 0) - rb_enc_associate_index(opt->e_script, opt->enc_index); + if (opt->src_enc_index >= 0) + rb_enc_associate_index(opt->e_script, opt->src_enc_index); require_libraries(); tree = rb_parser_compile_string(parser, opt->script, opt->e_script, 1); @@ -1010,6 +1010,9 @@ process_options(VALUE arg) } - if (opt->enc_index >= 0) { - enc = rb_enc_from_index(opt->enc_index); + if (opt->prim_enc_index >= 0) { + enc = rb_enc_from_index(opt->prim_enc_index); + } + else if (opt->src_enc_index >= 0) { + enc = rb_enc_from_index(opt->src_enc_index); } else { @@ -1136,5 +1139,6 @@ load_file(VALUE parser, const char *fnam require_libraries(); /* Why here? unnatural */ } - if (opt->enc_index >= 0) rb_enc_associate_index(f, opt->enc_index); + if (opt->src_enc_index >= 0) + rb_enc_associate_index(opt->e_script, opt->src_enc_index); tree = (NODE *)rb_parser_compile_file(parser, fname, f, line_start); if (script && rb_parser_end_seen_p(parser)) { @@ -1382,5 +1386,6 @@ ruby_process_options(int argc, char **ar opt.argc = argc; opt.argv = argv; - opt.enc_index = -1; + opt.prim_enc_index = -1; + opt.src_enc_index = -1; tree = (NODE *)rb_vm_call_cfunc(rb_vm_top_self(), process_options, (VALUE)&opt,
-- Nobu Nakada