diff --git a/ChangeLog b/ChangeLog index 9ead4833be..b5b580646c 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,10 @@ +Mon Aug 18 22:30:07 2008 Tanaka Akira + + * io.c (make_writeconv): if enc and enc2 is set, convert + string.encoding to enc2. + + * include/ruby/io.h: comment changed. + Mon Aug 18 21:02:08 2008 Tanaka Akira * include/ruby/io.h (rb_io_t): new fields: writeconv, diff --git a/include/ruby/io.h b/include/ruby/io.h index 32830aaaea..9d5407e1f3 100644 --- a/include/ruby/io.h +++ b/include/ruby/io.h @@ -53,7 +53,7 @@ typedef struct rb_io_t { * enc enc2 read action write action * NULL NULL force_encoding(default_external) write the byte sequence of str * e1 NULL force_encoding(e1) convert str.encoding to e1 - * e1 e2 convert from e2 to e1 convert from e1 to e2 + * e1 e2 convert from e2 to e1 convert str.encoding to e2 */ rb_encoding *enc; rb_encoding *enc2; diff --git a/io.c b/io.c index aae3e433cb..9eb01c1864 100644 --- a/io.c +++ b/io.c @@ -694,20 +694,17 @@ make_writeconv(rb_io_t *fptr) { if (!fptr->writeconv_initialized) { const char *senc, *denc; - fptr->writeconv_stateless = Qnil; - if (fptr->enc2) { - senc = fptr->enc->name; - denc = fptr->enc2->name; + rb_encoding *enc; + + enc = fptr->enc2 ? fptr->enc2 : fptr->enc; + senc = rb_econv_stateless_encoding(enc->name); + if (senc) { + denc = enc->name; + fptr->writeconv_stateless = rb_str_new2(senc); } else { - senc = rb_econv_stateless_encoding(fptr->enc->name); - if (senc) { - denc = fptr->enc->name; - fptr->writeconv_stateless = rb_str_new2(senc); - } - else { - denc = NULL; - } + denc = NULL; + fptr->writeconv_stateless = Qnil; } if (senc) { fptr->writeconv = rb_econv_open(senc, denc, 0); @@ -730,22 +727,19 @@ io_fwrite(VALUE str, rb_io_t *fptr) /* * If an external encoding was specified and it differs from * the strings encoding then we must transcode before writing. - * We must also transcode if two encodings were specified */ if (fptr->enc) { make_writeconv(fptr); - if (fptr->enc2) { + if (fptr->writeconv) { + str = rb_str_transcode(str, fptr->writeconv_stateless); str = rb_econv_string(fptr->writeconv, str, 0, RSTRING_LEN(str), Qnil, ECONV_PARTIAL_INPUT); - } - else { - if (fptr->writeconv) { - str = rb_str_transcode(str, fptr->writeconv_stateless); - str = rb_econv_string(fptr->writeconv, str, 0, RSTRING_LEN(str), Qnil, ECONV_PARTIAL_INPUT); - } - else { + } + else { + if (fptr->enc2) + str = rb_str_transcode(str, rb_enc_from_encoding(fptr->enc2)); + else str = rb_str_transcode(str, rb_enc_from_encoding(fptr->enc)); - } - } + } } len = RSTRING_LEN(str);