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

ruby-changes:7474

From: akr <ko1@a...>
Date: Sun, 31 Aug 2008 18:10:26 +0900 (JST)
Subject: [ruby-changes:7474] Ruby:r18993 (trunk): * transcode.c (transcode.c): set source_encoding and

akr	2008-08-31 18:08:31 +0900 (Sun, 31 Aug 2008)

  New Revision: 18993

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

  Log:
    * transcode.c (transcode.c): set source_encoding and
      destination_encoding as encoding object.
      (ecerr_source_encoding): new method.
      (ecerr_destination_encoding): ditto.

  Modified files:
    trunk/ChangeLog
    trunk/test/ruby/test_econv.rb
    trunk/transcode.c

Index: ChangeLog
===================================================================
--- ChangeLog	(revision 18992)
+++ ChangeLog	(revision 18993)
@@ -1,3 +1,10 @@
+Sun Aug 31 18:06:49 2008  Tanaka Akira  <akr@f...>
+
+	* transcode.c (transcode.c): set source_encoding and
+	  destination_encoding as encoding object.
+	  (ecerr_source_encoding): new method.
+	  (ecerr_destination_encoding): ditto.
+
 Sun Aug 31 17:58:59 2008  Nobuyoshi Nakada  <nobu@r...>
 
 	* lib/mkmf.rb (Logging.log_open): opens in binary mode to get rid of
Index: test/ruby/test_econv.rb
===================================================================
--- test/ruby/test_econv.rb	(revision 18992)
+++ test/ruby/test_econv.rb	(revision 18993)
@@ -457,6 +457,8 @@
     }
     assert_equal("EUC-JP", err.source_encoding_name)
     assert_equal("UTF-8", err.destination_encoding_name)
+    assert_equal(Encoding::EUC_JP, err.source_encoding)
+    assert_equal(Encoding::UTF_8, err.destination_encoding)
     assert_equal("\xA4".force_encoding("ASCII-8BIT"), err.error_bytes)
     assert_equal("d", err.readagain_bytes)
     assert_equal(false, err.incomplete_input?)
@@ -468,6 +470,8 @@
     }
     assert_equal("EUC-JP", err.source_encoding_name)
     assert_equal("UTF-8", err.destination_encoding_name)
+    assert_equal(Encoding::EUC_JP, err.source_encoding)
+    assert_equal(Encoding::UTF_8, err.destination_encoding)
     assert_equal("\xA4".force_encoding("ASCII-8BIT"), err.error_bytes)
     assert_equal(nil, err.readagain_bytes)
     assert_equal(true, err.incomplete_input?)
@@ -479,6 +483,8 @@
     }
     assert_equal("UTF-8", err.source_encoding_name)
     assert_equal("ISO-8859-1", err.destination_encoding_name)
+    assert_equal(Encoding::UTF_8, err.source_encoding)
+    assert_equal(Encoding::ISO_8859_1, err.destination_encoding)
     assert_equal("\u{3042}", err.error_char)
   end
 
Index: transcode.c
===================================================================
--- transcode.c	(revision 18992)
+++ transcode.c	(revision 18993)
@@ -1647,6 +1647,7 @@
         size_t readagain_len = ec->last_error.readagain_len;
         VALUE bytes2 = Qnil;
         VALUE dumped2;
+        int idx;
         if (ec->last_error.result == econv_incomplete_input) {
             mesg = rb_sprintf("incomplete %s on %s",
                     StringValueCStr(dumped),
@@ -1667,11 +1668,19 @@
         }
 
         exc = rb_exc_new3(rb_eInvalidByteSequence, mesg);
-        rb_ivar_set(exc, rb_intern("source_encoding_name"), rb_str_new2(ec->last_error.source_encoding));
-        rb_ivar_set(exc, rb_intern("destination_encoding_name"), rb_str_new2(ec->last_error.destination_encoding));
         rb_ivar_set(exc, rb_intern("error_bytes"), bytes);
         rb_ivar_set(exc, rb_intern("readagain_bytes"), bytes2);
         rb_ivar_set(exc, rb_intern("incomplete_input"), ec->last_error.result == econv_incomplete_input ? Qtrue : Qfalse);
+
+      set_encs:
+        rb_ivar_set(exc, rb_intern("source_encoding_name"), rb_str_new2(ec->last_error.source_encoding));
+        rb_ivar_set(exc, rb_intern("destination_encoding_name"), rb_str_new2(ec->last_error.destination_encoding));
+        idx = rb_enc_find_index(ec->last_error.source_encoding);
+        if (0 <= idx)
+            rb_ivar_set(exc, rb_intern("source_encoding"), rb_enc_from_encoding(rb_enc_from_index(idx)));
+        idx = rb_enc_find_index(ec->last_error.destination_encoding);
+        if (0 <= idx)
+            rb_ivar_set(exc, rb_intern("destination_encoding"), rb_enc_from_encoding(rb_enc_from_index(idx)));
         return exc;
     }
     if (ec->last_error.result == econv_undefined_conversion) {
@@ -1686,13 +1695,10 @@
                 ec->last_error.destination_encoding);
         exc = rb_exc_new3(rb_eConversionUndefined, mesg);
         idx = rb_enc_find_index(ec->last_error.source_encoding);
-        rb_ivar_set(exc, rb_intern("source_encoding_name"), rb_str_new2(ec->last_error.source_encoding));
-        rb_ivar_set(exc, rb_intern("destination_encoding_name"), rb_str_new2(ec->last_error.destination_encoding));
-        idx = rb_enc_find_index(ec->last_error.source_encoding);
         if (0 <= idx)
             rb_enc_associate_index(bytes, idx);
         rb_ivar_set(exc, rb_intern("error_char"), bytes);
-        return exc;
+        goto set_encs;
     }
     return Qnil;
 }
@@ -2855,12 +2861,24 @@
 }
 
 static VALUE
+ecerr_source_encoding(VALUE self)
+{
+    return rb_attr_get(self, rb_intern("source_encoding"));
+}
+
+static VALUE
 ecerr_destination_encoding_name(VALUE self)
 {
     return rb_attr_get(self, rb_intern("destination_encoding_name"));
 }
 
 static VALUE
+ecerr_destination_encoding(VALUE self)
+{
+    return rb_attr_get(self, rb_intern("destination_encoding"));
+}
+
+static VALUE
 ecerr_error_char(VALUE self)
 {
     return rb_attr_get(self, rb_intern("error_char"));
@@ -2938,10 +2956,14 @@
 
     rb_define_method(rb_eConversionUndefined, "source_encoding_name", ecerr_source_encoding_name, 0);
     rb_define_method(rb_eConversionUndefined, "destination_encoding_name", ecerr_destination_encoding_name, 0);
+    rb_define_method(rb_eConversionUndefined, "source_encoding", ecerr_source_encoding, 0);
+    rb_define_method(rb_eConversionUndefined, "destination_encoding", ecerr_destination_encoding, 0);
     rb_define_method(rb_eConversionUndefined, "error_char", ecerr_error_char, 0);
 
     rb_define_method(rb_eInvalidByteSequence, "source_encoding_name", ecerr_source_encoding_name, 0);
     rb_define_method(rb_eInvalidByteSequence, "destination_encoding_name", ecerr_destination_encoding_name, 0);
+    rb_define_method(rb_eInvalidByteSequence, "source_encoding", ecerr_source_encoding, 0);
+    rb_define_method(rb_eInvalidByteSequence, "destination_encoding", ecerr_destination_encoding, 0);
     rb_define_method(rb_eInvalidByteSequence, "error_bytes", ecerr_error_bytes, 0);
     rb_define_method(rb_eInvalidByteSequence, "readagain_bytes", ecerr_readagain_bytes, 0);
     rb_define_method(rb_eInvalidByteSequence, "incomplete_input?", ecerr_incomplete_input, 0);

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

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