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

ruby-changes:7143

From: akr <ko1@a...>
Date: Sun, 17 Aug 2008 00:04:59 +0900 (JST)
Subject: [ruby-changes:7143] Ruby:r18662 (trunk): * include/ruby/encoding.h (rb_econv_check_error): declared.

akr	2008-08-17 00:04:39 +0900 (Sun, 17 Aug 2008)

  New Revision: 18662

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

  Log:
    * include/ruby/encoding.h (rb_econv_check_error): declared.
    
    * transcode.c (make_econv_exception): new function.
      (transcode_loop): use make_econv_exception.
      (rb_econv_check_error): defined.

  Modified files:
    trunk/ChangeLog
    trunk/include/ruby/encoding.h
    trunk/transcode.c

Index: include/ruby/encoding.h
===================================================================
--- include/ruby/encoding.h	(revision 18661)
+++ include/ruby/encoding.h	(revision 18662)
@@ -254,7 +254,6 @@
     unsigned char **destination_buffer_ptr, unsigned char *destination_buffer_end,
     int flags);
 
-
 /* result: 0:success -1:failure */
 int rb_econv_insert_output(rb_econv_t *ec,
     const unsigned char *str, size_t len, const char *str_encoding);
@@ -262,6 +261,9 @@
 /* encoding that rb_econv_insert_output doesn't need conversion */
 const char *rb_econv_encoding_to_insert_output(rb_econv_t *ec);
 
+/* raise an error if the last rb_econv_convert is error */
+void rb_econv_check_error(rb_econv_t *ec);
+
 void rb_econv_close(rb_econv_t *ec);
 
 /* flags for rb_econv_open */
Index: ChangeLog
===================================================================
--- ChangeLog	(revision 18661)
+++ ChangeLog	(revision 18662)
@@ -1,3 +1,11 @@
+Sun Aug 17 00:02:07 2008  Tanaka Akira  <akr@f...>
+
+	* include/ruby/encoding.h (rb_econv_check_error): declared.
+
+	* transcode.c (make_econv_exception): new function.
+	  (transcode_loop): use make_econv_exception.
+	  (rb_econv_check_error): defined.
+
 Sat Aug 16 15:23:16 2008  Tanaka Akira  <akr@f...>
 
 	* include/ruby/encoding.h (rb_econv_elem_t): fields removed: from and
Index: transcode.c
===================================================================
--- transcode.c	(revision 18661)
+++ transcode.c	(revision 18662)
@@ -1202,6 +1202,32 @@
     xfree(ec);
 }
 
+static VALUE
+make_econv_exception(rb_econv_t *ec)
+{
+    VALUE mesg;
+    if (ec->last_error.result == econv_invalid_byte_sequence) {
+        VALUE bytes = rb_str_new((const char *)ec->last_error.error_bytes_start,
+                                 ec->last_error.error_bytes_len);
+        bytes = rb_str_dump(bytes);
+        mesg = rb_sprintf("invalid byte sequence: %s on %s",
+                StringValueCStr(bytes),
+                ec->last_error.source_encoding);
+        return rb_exc_new3(rb_eInvalidByteSequence, mesg);
+    }
+    if (ec->last_error.result == econv_undefined_conversion) {
+        VALUE bytes = rb_str_new((const char *)ec->last_error.error_bytes_start,
+                                 ec->last_error.error_bytes_len);
+        bytes = rb_str_dump(bytes);
+        mesg = rb_sprintf("conversion undefined: %s from %s to %s",
+                StringValueCStr(bytes),
+                ec->last_error.source_encoding,
+                ec->last_error.destination_encoding);
+        return rb_exc_new3(rb_eConversionUndefined, mesg);
+    }
+    return Qnil;
+}
+
 static void
 more_output_buffer(
         VALUE destination,
@@ -1256,6 +1282,7 @@
     rb_econv_result_t ret;
     unsigned char *out_start = *out_pos;
     int max_output;
+    VALUE exc;
 
     ec = rb_econv_open(from_encoding, to_encoding, 0);
     if (!ec)
@@ -1276,8 +1303,9 @@
 	    if (output_replacement_character(ec) == 0)
                 goto resume;
 	}
+        exc = make_econv_exception(ec);
         rb_econv_close(ec);
-	rb_raise(rb_eInvalidByteSequence, "invalid byte sequence");
+	rb_exc_raise(exc);
     }
     if (ret == econv_undefined_conversion) {
 	/* valid character in from encoding
@@ -1290,8 +1318,9 @@
 	    if (output_replacement_character(ec) == 0)
                 goto resume;
 	}
+        exc = make_econv_exception(ec);
         rb_econv_close(ec);
-        rb_raise(rb_eConversionUndefined, "conversion undefined for byte sequence (maybe invalid byte sequence)");
+	rb_exc_raise(exc);
     }
     if (ret == econv_destination_buffer_full) {
         more_output_buffer(destination, resize_destination, max_output, &out_start, out_pos, &out_stop);
@@ -1318,6 +1347,7 @@
     unsigned char *out_start = *out_pos;
     const unsigned char *ptr;
     int max_output;
+    VALUE exc;
 
     ec = rb_econv_open(from_encoding, to_encoding, 0);
     if (!ec)
@@ -1357,8 +1387,9 @@
                 if (output_replacement_character(ec) == 0)
                     break;
             }
+            exc = make_econv_exception(ec);
             rb_econv_close(ec);
-            rb_raise(rb_eInvalidByteSequence, "invalid byte sequence");
+            rb_exc_raise(exc);
             break;
 
           case econv_undefined_conversion:
@@ -1372,8 +1403,9 @@
                 if (output_replacement_character(ec) == 0)
                     break;
             }
+            exc = make_econv_exception(ec);
             rb_econv_close(ec);
-            rb_raise(rb_eConversionUndefined, "conversion undefined for byte sequence (maybe invalid byte sequence)");
+            rb_exc_raise(exc);
             break;
 
           case econv_destination_buffer_full:
@@ -2036,6 +2068,17 @@
 }
 
 void
+rb_econv_check_error(rb_econv_t *ec)
+{
+    VALUE exc;
+
+    exc = make_econv_exception(ec);
+    if (NIL_P(exc))
+        return;
+    rb_exc_raise(exc);
+}
+
+void
 Init_transcode(void)
 {
     rb_eConversionUndefined = rb_define_class_under(rb_cEncoding, "ConversionUndefined", rb_eStandardError);

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

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