Hi,

At Mon, 27 Oct 2008 21:07:16 +0900,
Nobuyoshi Nakada wrote in [ruby-core:19546]:
> Anyway, I think I understand the needs to specify source
> encoding without magic comments.  Is the option for that
> purpose an acceptable solution?

Here is the patch to add options:

  --encoding=external:internal:source
  --external-encoding=enc
  --internal-encoding=enc
  --source-encoding=enc


Index: ruby.c =================================================================== --- ruby.c (revision 20075) +++ ruby.c (working copy) @@ -623,5 +623,5 @@ dump_option(const char *str, int len, vo static void -set_internal_encoding_once(struct cmdline_options *opt, const char *e, int elen) +set_option_encoding_once(const char *type, VALUE *name, const char *e, int elen) { VALUE ename; @@ -630,27 +630,16 @@ set_internal_encoding_once(struct cmdlin ename = rb_str_new(e, elen); - if (opt->intern.enc.name && - rb_funcall(ename, rb_intern("casecmp"), 1, opt->intern.enc.name) != INT2FIX(0)) { + if (*name && + rb_funcall(ename, rb_intern("casecmp"), 1, *name) != INT2FIX(0)) { rb_raise(rb_eRuntimeError, - "default_intenal already set to %s", RSTRING_PTR(opt->intern.enc.name)); + "%s already set to %s", type, RSTRING_PTR(*name)); } - opt->intern.enc.name = ename; + *name = ename; } -static void -set_external_encoding_once(struct cmdline_options *opt, const char *e, int elen) -{ - VALUE ename; - - if (!elen) elen = strlen(e); - ename = rb_str_new(e, elen); - - if (opt->ext.enc.name && - rb_funcall(ename, rb_intern("casecmp"), 1, opt->ext.enc.name) != INT2FIX(0)) { - rb_raise(rb_eRuntimeError, - "default_external already set to %s", RSTRING_PTR(opt->ext.enc.name)); - } - opt->ext.enc.name = ename; -} +#define set_internal_encoding_once(opt, e, elen) \ + set_option_encoding_once("default_intenal", &opt->intern.enc.name, e, elen) +#define set_external_encoding_once(opt, e, elen) \ + set_option_encoding_once("default_extenal", &opt->ext.enc.name, e, elen) static int @@ -956,13 +945,29 @@ proc_options(int argc, char **argv, stru char *p; encoding: - p = strchr(s, ':'); - if (p) { - if (p > s) - set_external_encoding_once(opt, s, p-s); - if (*++p) - set_internal_encoding_once(opt, p, 0); - } - else - set_external_encoding_once(opt, s, 0); + do { +# define set_encoding_part(type) \ + if (!(p = strchr(s, ':'))) { \ + set_##type##_encoding_once(opt, s, 0); \ + break; \ + } \ + else if (p > s) { \ + set_##type##_encoding_once(opt, s, p-s); \ + } + set_encoding_part(external); + if (!*(s = ++p)) break; + set_encoding_part(internal); + if (!*(s = ++p)) break; + set_encoding_part(source); +# undef set_encoding_part + } while (0); + } + else if (is_option_with_arg("internal-encoding", Qfalse, Qtrue)) { + set_internal_encoding_once(opt, s, 0); + } + else if (is_option_with_arg("external-encoding", Qfalse, Qtrue)) { + set_external_encoding_once(opt, s, 0); + } + else if (is_option_with_arg("source-encoding", Qfalse, Qtrue)) { + set_source_encoding_once(opt, s, 0); } else if (strcmp("version", s) == 0) {
-- Nobu Nakada