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

ruby-changes:7461

From: akr <ko1@a...>
Date: Sun, 31 Aug 2008 14:28:11 +0900 (JST)
Subject: [ruby-changes:7461] Ruby:r18980 (trunk): * transcode.c (econv_last_error): new method.

akr	2008-08-31 14:27:52 +0900 (Sun, 31 Aug 2008)

  New Revision: 18980

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

  Log:
    * transcode.c (econv_last_error): new method.

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

Index: ChangeLog
===================================================================
--- ChangeLog	(revision 18979)
+++ ChangeLog	(revision 18980)
@@ -1,3 +1,7 @@
+Sun Aug 31 14:27:27 2008  Tanaka Akira  <akr@f...>
+
+	* transcode.c (econv_last_error): new method.
+
 Sun Aug 31 14:17:34 2008  Tanaka Akira  <akr@f...>
 
 	* transcode.c (econv_primitive_convert): make two arguments,
Index: test/ruby/test_econv.rb
===================================================================
--- test/ruby/test_econv.rb	(revision 18979)
+++ test/ruby/test_econv.rb	(revision 18980)
@@ -598,4 +598,19 @@
     ec.convert("\xEF")
     assert_raise(Encoding::InvalidByteSequence) { ec.finish }
   end
+
+  def test_last_error1
+    ec = Encoding::Converter.new("sjis", "euc-jp")
+    assert_equal(nil, ec.last_error)
+    assert_equal(:incomplete_input, ec.primitive_convert(src="fo\x81", dst="", nil, nil))
+    assert_kind_of(Encoding::InvalidByteSequence, ec.last_error)
+  end
+
+  def test_last_error2
+    ec = Encoding::Converter.new("sjis", "euc-jp")
+    assert_equal("fo", ec.convert(src="fo\x81"))
+    assert_raise(Encoding::InvalidByteSequence) { ec.finish }
+    assert_kind_of(Encoding::InvalidByteSequence, ec.last_error)
+  end
+
 end
Index: transcode.c
===================================================================
--- transcode.c	(revision 18979)
+++ transcode.c	(revision 18980)
@@ -2756,6 +2756,39 @@
     return str;
 }
 
+/*
+ * call-seq:
+ *   last_error -> exception or nil
+ *
+ * returns an exception object for the last conversion.
+ * it returns nil if the last conversion is not an error. 
+ *
+ * "error" means that
+ * Encoding::InvalidByteSequence and Encoding::ConversionUndefined for
+ * Encoding::Converter#convert and
+ * :invalid_byte_sequence, :incomplete_input and :undefined_conversion for
+ * Encoding::Converter#primitive_convert.
+ *
+ * ec = Encoding::Converter.new("utf-8", "iso-8859-1")
+ * p ec.primitive_convert(src="\xf1abcd", dst="")       #=> :invalid_byte_sequence
+ * p ec.last_error      #=> #<Encoding::InvalidByteSequence: "\xF1" followed by "a" on UTF-8>
+ * p ec.primitive_convert(src, dst, nil, 1)             #=> :destination_buffer_full
+ * p ec.last_error      #=> nil
+ *
+ */
+
+static VALUE
+econv_last_error(VALUE self)
+{
+    rb_econv_t *ec = check_econv(self);
+    VALUE exc;
+
+    exc = make_econv_exception(ec);
+    if (NIL_P(exc))
+        return Qnil;
+    return exc;
+}
+
 void
 rb_econv_check_error(rb_econv_t *ec)
 {
@@ -2842,6 +2875,7 @@
     rb_define_method(rb_cEncodingConverter, "primitive_errinfo", econv_primitive_errinfo, 0);
     rb_define_method(rb_cEncodingConverter, "insert_output", econv_insert_output, 1);
     rb_define_method(rb_cEncodingConverter, "putback", econv_putback, -1);
+    rb_define_method(rb_cEncodingConverter, "last_error", econv_last_error, 0);
     rb_define_const(rb_cEncodingConverter, "INVALID_MASK", INT2FIX(ECONV_INVALID_MASK));
     rb_define_const(rb_cEncodingConverter, "INVALID_IGNORE", INT2FIX(ECONV_INVALID_IGNORE));
     rb_define_const(rb_cEncodingConverter, "INVALID_REPLACE", INT2FIX(ECONV_INVALID_REPLACE));

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

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