ruby-changes:7196
From: akr <ko1@a...>
Date: Wed, 20 Aug 2008 05:21:10 +0900 (JST)
Subject: [ruby-changes:7196] Ruby:r18715 (trunk): * io.c (parse_mode_enc): extracted from mode_enc.
akr 2008-08-20 05:20:31 +0900 (Wed, 20 Aug 2008) New Revision: 18715 http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=18715 Log: * io.c (parse_mode_enc): extracted from mode_enc. (io_extract_encoding_option): extracted from io_set_encoding. Modified files: trunk/ChangeLog trunk/io.c Index: ChangeLog =================================================================== --- ChangeLog (revision 18714) +++ ChangeLog (revision 18715) @@ -1,3 +1,8 @@ +Wed Aug 20 05:19:40 2008 Tanaka Akira <akr@f...> + + * io.c (parse_mode_enc): extracted from mode_enc. + (io_extract_encoding_option): extracted from io_set_encoding. + Wed Aug 20 04:17:26 2008 Tanaka Akira <akr@f...> * io.c (rb_io_flags_modenum): make it static. Index: io.c =================================================================== --- io.c (revision 18714) +++ io.c (revision 18715) @@ -3686,7 +3686,7 @@ } static void -mode_enc(rb_io_t *fptr, const char *estr) +parse_mode_enc(const char *estr, rb_encoding **enc_p, rb_encoding **enc2_p) { const char *p0, *p1; char *enc2name; @@ -3694,22 +3694,21 @@ /* parse estr as "enc" or "enc2:enc" */ - fptr->enc = 0; - fptr->enc2 = 0; - clear_codeconv(fptr); + *enc_p = 0; + *enc2_p = 0; p0 = strrchr(estr, ':'); if (!p0) p1 = estr; else p1 = p0 + 1; idx = rb_enc_find_index(p1); if (idx >= 0) { - fptr->enc = rb_enc_from_index(idx); + *enc_p = rb_enc_from_index(idx); } else { rb_warn("Unsupported encoding %s ignored", p1); } - if (fptr->enc && p0) { + if (*enc_p && p0) { int n = p0 - estr; if (n > ENCODING_MAXNAMELEN) { idx2 = -1; @@ -3729,11 +3728,19 @@ n, estr, p1); } else { - fptr->enc2 = rb_enc_from_index(idx2); + *enc2_p = rb_enc_from_index(idx2); } } } +static void +mode_enc(rb_io_t *fptr, const char *estr) +{ + clear_codeconv(fptr); + + parse_mode_enc(estr, &fptr->enc, &fptr->enc2); +} + void rb_io_mode_enc(rb_io_t *fptr, const char *mode) { @@ -3743,6 +3750,54 @@ } } +static int +io_extract_encoding_option(VALUE opt, rb_encoding **enc_p, rb_encoding **enc2_p) +{ + VALUE encoding=Qnil, extenc=Qnil, intenc=Qnil; + int extracted = 0; + if (!NIL_P(opt)) { + VALUE v; + v = rb_hash_aref(opt, sym_encoding); + if (!NIL_P(v)) encoding = v; + v = rb_hash_aref(opt, sym_extenc); + if (!NIL_P(v)) extenc = v; + v = rb_hash_aref(opt, sym_intenc); + if (!NIL_P(v)) intenc = v; + } + if (!NIL_P(extenc)) { + rb_encoding *extencoding = rb_to_encoding(extenc); + extracted = 1; + *enc_p = 0; + *enc2_p = 0; + if (!NIL_P(encoding)) { + rb_warn("Ignoring encoding parameter '%s': external_encoding is used", + RSTRING_PTR(encoding)); + } + if (!NIL_P(intenc)) { + rb_encoding *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))); + } + else { + *enc2_p = intencoding; + } + } + *enc_p = extencoding; + } + else { + if (!NIL_P(intenc)) { + rb_raise(rb_eArgError, "External encoding must be specified when internal encoding is given"); + } + if (!NIL_P(encoding)) { + extracted = 1; + parse_mode_enc(StringValueCStr(encoding), enc_p, enc2_p); + } + } + return extracted; +} + struct sysopen_struct { char *fname; int flag; @@ -4386,48 +4441,14 @@ io_set_encoding(VALUE io, VALUE opt) { rb_io_t *fptr; - VALUE encoding=Qnil, extenc=Qnil, intenc=Qnil; - if (!NIL_P(opt)) { - VALUE v; - v = rb_hash_aref(opt, sym_encoding); - if (!NIL_P(v)) encoding = v; - v = rb_hash_aref(opt, sym_extenc); - if (!NIL_P(v)) extenc = v; - v = rb_hash_aref(opt, sym_intenc); - if (!NIL_P(v)) intenc = v; - } - if (!NIL_P(extenc)) { - rb_encoding *extencoding = rb_to_encoding(extenc); - GetOpenFile(io, fptr); - fptr->enc = 0; - fptr->enc2 = 0; + rb_encoding *enc, *enc2; + + if (io_extract_encoding_option(opt, &enc, &enc2)) { + GetOpenFile(io, fptr); + fptr->enc = enc; + fptr->enc2 = enc2; clear_codeconv(fptr); - if (!NIL_P(encoding)) { - rb_warn("Ignoring encoding parameter '%s': external_encoding is used", - RSTRING_PTR(encoding)); - } - if (!NIL_P(intenc)) { - rb_encoding *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))); - } - else { - fptr->enc2 = intencoding; - } - } - fptr->enc = extencoding; } - else { - if (!NIL_P(intenc)) { - rb_raise(rb_eArgError, "External encoding must be specified when internal encoding is given"); - } - if (!NIL_P(encoding)) { - GetOpenFile(io, fptr); - mode_enc(fptr, StringValueCStr(encoding)); - } - } } static VALUE -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/