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

ruby-changes:7393

From: akr <ko1@a...>
Date: Fri, 29 Aug 2008 02:50:32 +0900 (JST)
Subject: [ruby-changes:7393] Ruby:r18912 (trunk): * transcode.c (econv_convert): new method.

akr	2008-08-29 02:46:18 +0900 (Fri, 29 Aug 2008)

  New Revision: 18912

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

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

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

Index: ChangeLog
===================================================================
--- ChangeLog	(revision 18911)
+++ ChangeLog	(revision 18912)
@@ -1,3 +1,7 @@
+Fri Aug 29 02:45:29 2008  Tanaka Akira  <akr@f...>
+
+	* transcode.c (econv_convert): new method.
+
 Fri Aug 29 02:38:14 2008  Tanaka Akira  <akr@f...>
 
 	* transcode.c (econv_primitive_convert): accept nil as
Index: test/ruby/test_econv.rb
===================================================================
--- test/ruby/test_econv.rb	(revision 18911)
+++ test/ruby/test_econv.rb	(revision 18912)
@@ -559,4 +559,13 @@
     assert_equal(:finished, ret)
     assert_equal(["xyzabc", ""], [dst, src])
   end
+
+  def test_convert
+    ec = Encoding::Converter.new("utf-8", "euc-jp")
+    assert_raise(Encoding::InvalidByteSequence) { ec.convert("a\x80") }
+    assert_raise(Encoding::ConversionUndefined) { ec.convert("\ufffd") }
+    ret = ec.primitive_convert(nil, "", nil, nil)
+    assert_equal(:finished, ret)
+    assert_raise(ArgumentError) { ec.convert("a") }
+  end
 end
Index: transcode.c
===================================================================
--- transcode.c	(revision 18911)
+++ transcode.c	(revision 18912)
@@ -2493,6 +2493,59 @@
 
 /*
  * call-seq:
+ *   convert(source_string) -> destination_string
+ *
+ * convert source_string and return destination_string.
+ *
+ * source_string is assumed as a part of source.
+ * i.e.  Encoding::Converter::PARTIAL_INPUT is used internally.
+ *
+ * If a conversion error occur,
+ * Encoding::ConversionUndefined or
+ * Encoding::InvalidByteSequence is raised.
+ *
+ */
+static VALUE
+econv_convert(VALUE self, VALUE source_string)
+{
+    VALUE ret, dst;
+    VALUE av[5];
+    int ac;
+    rb_econv_t *ec = check_econv(self);
+
+    StringValue(source_string);
+
+    dst = rb_str_new(NULL, 0);
+
+    av[0] = rb_str_dup(source_string);
+    av[1] = dst;
+    av[2] = Qnil;
+    av[3] = Qnil;
+    av[4] = INT2NUM(ECONV_PARTIAL_INPUT);
+    ac = 5;
+
+    ret = econv_primitive_convert(ac, av, self);
+
+    if (ret == sym_invalid_byte_sequence ||
+        ret == sym_undefined_conversion ||
+        ret == sym_incomplete_input) {
+        VALUE exc = make_econv_exception(ec);
+        rb_exc_raise(exc);
+    }
+
+    if (ret == sym_finished) {
+        rb_raise(rb_eArgError, "converter already finished");
+    }
+
+    if (ret != sym_source_buffer_empty) {
+        rb_bug("unexpected result of econv_primitive_convert");
+    }
+
+    return dst;
+}
+
+/*
+ * call-seq:
  *   primitive_errinfo -> array
  *
  * primitive_errinfo returns a precious information of last error result
@@ -2721,6 +2774,7 @@
     rb_define_method(rb_cEncodingConverter, "source_encoding", econv_source_encoding, 0);
     rb_define_method(rb_cEncodingConverter, "destination_encoding", econv_destination_encoding, 0);
     rb_define_method(rb_cEncodingConverter, "primitive_convert", econv_primitive_convert, -1);
+    rb_define_method(rb_cEncodingConverter, "convert", econv_convert, 1);
     rb_define_method(rb_cEncodingConverter, "primitive_errinfo", econv_primitive_errinfo, 0);
     rb_define_method(rb_cEncodingConverter, "primitive_insert_output", econv_primitive_insert_output, 1);
     rb_define_method(rb_cEncodingConverter, "primitive_putback", econv_primitive_putback, 1);

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

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