[前][次][番号順一覧][スレッド一覧]

ruby-changes:7171

From: akr <ko1@a...>
Date: Mon, 18 Aug 2008 17:25:08 +0900 (JST)
Subject: [ruby-changes:7171] Ruby:r18690 (trunk): * io.c (clear_readconv): extracted from rb_io_fptr_finalize.

akr	2008-08-18 17:24:49 +0900 (Mon, 18 Aug 2008)

  New Revision: 18690

  http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=18690

  Log:
    * 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.

  Modified files:
    trunk/ChangeLog
    trunk/io.c
    trunk/test/ruby/test_io_m17n.rb

Index: ChangeLog
===================================================================
--- ChangeLog	(revision 18689)
+++ ChangeLog	(revision 18690)
@@ -1,3 +1,11 @@
+Mon Aug 18 17:23:38 2008  Tanaka Akira  <akr@f...>
+
+	* 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@f...>
 
 	* io.c (mode_enc): modify enc and enc2 consistently.
Index: io.c
===================================================================
--- io.c	(revision 18689)
+++ io.c	(revision 18690)
@@ -2894,6 +2894,19 @@
     }
 }
 
+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 @@
         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 @@
 
     fptr->enc = 0;
     fptr->enc2 = 0;
+    clear_readconv(fptr);
 
     p0 = strrchr(estr, ':');
     if (!p0) p1 = estr;
@@ -4258,6 +4265,7 @@
 	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 @@
 		GetOpenFile(current_file, fptr);
 		fptr->enc = argf_enc;
 		fptr->enc2 = argf_enc2;
+                clear_readconv(fptr);
 	    }
 	}
 	else {
@@ -6331,11 +6340,13 @@
     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 @@
 	    else {
 		fptr->enc = rb_to_encoding(v1);
 		fptr->enc2 = 0;
+                clear_readconv(fptr);
 	    }
 	}
     }
Index: test/ruby/test_io_m17n.rb
===================================================================
--- test/ruby/test_io_m17n.rb	(revision 18689)
+++ test/ruby/test_io_m17n.rb	(revision 18690)
@@ -552,5 +552,54 @@
       }
     }
   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
 

--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]