1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

* io.c (clear_readconv): extracted from rb_io_fptr_finalize.

(mode_enc): call clear_readconv.
  (io_set_encoding): ditto.
  (argf_next_argv): ditto.
  (io_encoding_set): ditto.


git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@18690 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
akr 2008-08-18 08:24:49 +00:00
parent f7bdac01c2
commit 89b4f06a59
3 changed files with 77 additions and 8 deletions

View file

@ -1,3 +1,11 @@
Mon Aug 18 17:23:38 2008 Tanaka Akira <akr@fsij.org>
* io.c (clear_readconv): extracted from rb_io_fptr_finalize.
(mode_enc): call clear_readconv.
(io_set_encoding): ditto.
(argf_next_argv): ditto.
(io_encoding_set): ditto.
Mon Aug 18 16:54:06 2008 Tanaka Akira <akr@fsij.org>
* io.c (mode_enc): modify enc and enc2 consistently.

28
io.c
View file

@ -2894,6 +2894,19 @@ rb_io_fptr_cleanup(rb_io_t *fptr, int noraise)
}
}
static void
clear_readconv(rb_io_t *fptr)
{
if (fptr->readconv) {
rb_econv_close(fptr->readconv);
fptr->readconv = NULL;
}
if (fptr->crbuf) {
free(fptr->crbuf);
fptr->crbuf = NULL;
}
}
int
rb_io_fptr_finalize(rb_io_t *fptr)
{
@ -2913,14 +2926,7 @@ rb_io_fptr_finalize(rb_io_t *fptr)
free(fptr->wbuf);
fptr->wbuf = 0;
}
if (fptr->readconv) {
rb_econv_close(fptr->readconv);
fptr->readconv = NULL;
}
if (fptr->crbuf) {
free(fptr->crbuf);
fptr->crbuf = NULL;
}
clear_readconv(fptr);
free(fptr);
return 1;
}
@ -3529,6 +3535,7 @@ mode_enc(rb_io_t *fptr, const char *estr)
fptr->enc = 0;
fptr->enc2 = 0;
clear_readconv(fptr);
p0 = strrchr(estr, ':');
if (!p0) p1 = estr;
@ -4258,6 +4265,7 @@ io_set_encoding(VALUE io, VALUE opt)
GetOpenFile(io, fptr);
fptr->enc = 0;
fptr->enc2 = 0;
clear_readconv(fptr);
if (!NIL_P(encoding)) {
rb_warn("Ignoring encoding parameter '%s': external_encoding is used",
RSTRING_PTR(encoding));
@ -5604,6 +5612,7 @@ argf_next_argv(VALUE argf)
GetOpenFile(current_file, fptr);
fptr->enc = argf_enc;
fptr->enc2 = argf_enc2;
clear_readconv(fptr);
}
}
else {
@ -6331,11 +6340,13 @@ io_encoding_set(rb_io_t *fptr, int argc, VALUE v1, VALUE v2)
if (argc == 2) {
fptr->enc2 = rb_to_encoding(v1);
fptr->enc = rb_to_encoding(v2);
clear_readconv(fptr);
}
else if (argc == 1) {
if (NIL_P(v1)) {
fptr->enc = 0;
fptr->enc2 = 0;
clear_readconv(fptr);
}
else {
VALUE tmp = rb_check_string_type(v1);
@ -6345,6 +6356,7 @@ io_encoding_set(rb_io_t *fptr, int argc, VALUE v1, VALUE v2)
else {
fptr->enc = rb_to_encoding(v1);
fptr->enc2 = 0;
clear_readconv(fptr);
}
}
}

View file

@ -552,5 +552,54 @@ EOT
}
}
end
def test_set_encoding
with_pipe("utf-8:euc-jp") {|r, w|
s = "\u3042".force_encoding("ascii-8bit")
s << "\x82\xa0".force_encoding("ascii-8bit")
w << s
w.close
assert_equal("\xa4\xa2".force_encoding("euc-jp"), r.getc)
r.set_encoding("shift_jis:euc-jp")
assert_equal("\xa4\xa2".force_encoding("euc-jp"), r.getc)
}
end
def test_set_encoding2
with_pipe("utf-8:euc-jp") {|r, w|
s = "\u3042".force_encoding("ascii-8bit")
s << "\x82\xa0".force_encoding("ascii-8bit")
w << s
w.close
assert_equal("\xa4\xa2".force_encoding("euc-jp"), r.getc)
r.set_encoding("shift_jis", "euc-jp")
assert_equal("\xa4\xa2".force_encoding("euc-jp"), r.getc)
}
end
def test_set_encoding_nil
with_pipe("utf-8:euc-jp") {|r, w|
s = "\u3042".force_encoding("ascii-8bit")
s << "\x82\xa0".force_encoding("ascii-8bit")
w << s
w.close
assert_equal("\xa4\xa2".force_encoding("euc-jp"), r.getc)
r.set_encoding(nil)
assert_equal("\x82\xa0".force_encoding(Encoding.default_external), r.read)
}
end
def test_set_encoding_enc
with_pipe("utf-8:euc-jp") {|r, w|
s = "\u3042".force_encoding("ascii-8bit")
s << "\x82\xa0".force_encoding("ascii-8bit")
w << s
w.close
assert_equal("\xa4\xa2".force_encoding("euc-jp"), r.getc)
r.set_encoding(Encoding::Shift_JIS)
assert_equal("\x82\xa0".force_encoding(Encoding::Shift_JIS), r.getc)
}
end
end