* io.c (rb_io_extract_encoding_option): "internal_encoding: nil"

to specify no-transcoding.  and other corner case fixed.
  [ruby-dev:37496]

* hash.c (rb_hash_lookup2): new function to look-up hash with
  default value.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@20870 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
matz 2008-12-19 08:22:45 +00:00
Родитель e5bfdb8391
Коммит 63219d8a24
4 изменённых файлов: 62 добавлений и 41 удалений

Просмотреть файл

@ -1,3 +1,12 @@
Fri Dec 19 17:01:35 2008 Yukihiro Matsumoto <matz@ruby-lang.org>
* io.c (rb_io_extract_encoding_option): "internal_encoding: nil"
to specify no-transcoding. and other corner case fixed.
[ruby-dev:37496]
* hash.c (rb_hash_lookup2): new function to look-up hash with
default value.
Fri Dec 19 15:51:48 2008 Nobuyoshi Nakada <nobu@ruby-lang.org> Fri Dec 19 15:51:48 2008 Nobuyoshi Nakada <nobu@ruby-lang.org>
* ruby.c (process_options): get rid of warning on DOSISH. * ruby.c (process_options): get rid of warning on DOSISH.

10
hash.c
Просмотреть файл

@ -463,16 +463,22 @@ rb_hash_aref(VALUE hash, VALUE key)
} }
VALUE VALUE
rb_hash_lookup(VALUE hash, VALUE key) rb_hash_lookup2(VALUE hash, VALUE key, VALUE def)
{ {
VALUE val; VALUE val;
if (!RHASH(hash)->ntbl || !st_lookup(RHASH(hash)->ntbl, key, &val)) { if (!RHASH(hash)->ntbl || !st_lookup(RHASH(hash)->ntbl, key, &val)) {
return Qnil; /* without Hash#default */ return def; /* without Hash#default */
} }
return val; return val;
} }
VALUE
rb_hash_lookup(VALUE hash, VALUE key)
{
return rb_hash_lookup2(hash, key, Qnil);
}
/* /*
* call-seq: * call-seq:
* hsh.fetch(key [, default] ) => obj * hsh.fetch(key [, default] ) => obj

Просмотреть файл

@ -359,6 +359,7 @@ VALUE rb_hash_dup(VALUE);
VALUE rb_hash_freeze(VALUE); VALUE rb_hash_freeze(VALUE);
VALUE rb_hash_aref(VALUE, VALUE); VALUE rb_hash_aref(VALUE, VALUE);
VALUE rb_hash_lookup(VALUE, VALUE); VALUE rb_hash_lookup(VALUE, VALUE);
VALUE rb_hash_lookup2(VALUE, VALUE, VALUE);
VALUE rb_hash_fetch(VALUE, VALUE); VALUE rb_hash_fetch(VALUE, VALUE);
VALUE rb_hash_aset(VALUE, VALUE, VALUE); VALUE rb_hash_aset(VALUE, VALUE, VALUE);
VALUE rb_hash_delete_if(VALUE); VALUE rb_hash_delete_if(VALUE);

83
io.c
Просмотреть файл

@ -3917,54 +3917,59 @@ rb_io_mode_enc(rb_io_t *fptr, const char *modestr)
int int
rb_io_extract_encoding_option(VALUE opt, rb_encoding **enc_p, rb_encoding **enc2_p) rb_io_extract_encoding_option(VALUE opt, rb_encoding **enc_p, rb_encoding **enc2_p)
{ {
VALUE encoding=Qnil, extenc=Qnil, intenc=Qnil; VALUE encoding=Qnil, extenc=Qundef, intenc=Qundef, tmp;
int extracted = 0; int extracted = 0;
rb_encoding *extencoding = NULL;
rb_encoding *intencoding = NULL;
if (!NIL_P(opt)) { if (!NIL_P(opt)) {
VALUE v; VALUE v;
v = rb_hash_aref(opt, sym_encoding); v = rb_hash_lookup2(opt, sym_encoding, Qnil);
if (!NIL_P(v)) encoding = v; if (v != Qnil) encoding = v;
v = rb_hash_aref(opt, sym_extenc); v = rb_hash_lookup2(opt, sym_extenc, Qundef);
if (!NIL_P(v)) extenc = v; if (v != Qnil) extenc = v;
v = rb_hash_aref(opt, sym_intenc); v = rb_hash_lookup2(opt, sym_intenc, Qundef);
if (!NIL_P(v)) intenc = v; if (v != Qundef) intenc = v;
} }
if (!NIL_P(extenc)) { if ((extenc != Qundef || intenc != Qundef) && !NIL_P(encoding)) {
rb_encoding *extencoding = rb_to_encoding(extenc); rb_warn("Ignoring encoding parameter '%s': %s_encoding is used",
rb_encoding *intencoding = NULL; StringValueCStr(encoding),
extracted = 1; extenc == Qundef ? "internal" : "external");
if (!NIL_P(encoding)) { encoding = Qnil;
rb_warn("Ignoring encoding parameter '%s': external_encoding is used", }
RSTRING_PTR(encoding)); if (extenc != Qundef && !NIL_P(extenc)) {
extencoding = rb_to_encoding(extenc);
}
if (intenc != Qundef) {
if (NIL_P(intenc)) {
/* internal_encoding: nil => no transcoding */
intencoding = (rb_encoding *)Qnil;
} }
if (!NIL_P(intenc)) { else if (!NIL_P(tmp = rb_check_string_type(intenc))) {
if (!NIL_P(encoding = rb_check_string_type(intenc))) { char *p = StringValueCStr(tmp);
char *p = StringValueCStr(encoding);
if (*p == '-' && *(p+1) == '\0') { if (*p == '-' && *(p+1) == '\0') {
/* Special case - "-" => no transcoding */ /* Special case - "-" => no transcoding */
intencoding = (rb_encoding *)Qnil;
}
else
intencoding = rb_to_encoding(intenc);
}
else
intencoding = rb_to_encoding(intenc);
if (extencoding == intencoding) {
rb_warn("Ignoring internal encoding '%s': it is identical to external encoding '%s'",
RSTRING_PTR(rb_inspect(intenc)),
RSTRING_PTR(rb_inspect(extenc)));
intencoding = (rb_encoding *)Qnil; intencoding = (rb_encoding *)Qnil;
} }
else {
intencoding = rb_to_encoding(intenc);
}
}
else {
intencoding = rb_to_encoding(intenc);
}
if (extencoding == intencoding) {
intencoding = (rb_encoding *)Qnil;
} }
rb_io_ext_int_to_encs(extencoding, intencoding, enc_p, enc2_p);
} }
else { if (!NIL_P(encoding)) {
if (!NIL_P(intenc)) { extracted = 1;
rb_raise(rb_eArgError, "External encoding must be specified when internal encoding is given"); parse_mode_enc(StringValueCStr(encoding), enc_p, enc2_p);
} }
if (!NIL_P(encoding)) { else if (extenc != Qundef || intenc != Qundef) {
extracted = 1; extracted = 1;
parse_mode_enc(StringValueCStr(encoding), enc_p, enc2_p); rb_io_ext_int_to_encs(extencoding, intencoding, enc_p, enc2_p);
}
} }
return extracted; return extracted;
} }