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

ruby-changes:7039

From: akr <ko1@a...>
Date: Wed, 13 Aug 2008 14:49:16 +0900 (JST)
Subject: [ruby-changes:7039] Ruby:r18557 (trunk): * enc/trans/newline.trans (rb_crlf_newline): new transcoder.

akr	2008-08-13 14:48:57 +0900 (Wed, 13 Aug 2008)

  New Revision: 18557

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

  Log:
    * enc/trans/newline.trans (rb_crlf_newline): new transcoder.
      (rb_cr_newline): new transcoder.
    
    * transcode.c (trans_open_i): one more exra room for input newline
      converter.
      (rb_trans_open): crlf newline and cr newline implemented.
      (Init_transcode): Encoding::Converter::CRLF_NEWLINE and
      Encoding::Converter::LF_NEWLINE defined.

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

Index: ChangeLog
===================================================================
--- ChangeLog	(revision 18556)
+++ ChangeLog	(revision 18557)
@@ -1,3 +1,14 @@
+Wed Aug 13 14:45:37 2008  Tanaka Akira  <akr@f...>
+
+	* enc/trans/newline.trans (rb_crlf_newline): new transcoder.
+	  (rb_cr_newline): new transcoder.
+
+	* transcode.c (trans_open_i): one more exra room for input newline
+	  converter.
+	  (rb_trans_open): crlf newline and cr newline implemented.
+	  (Init_transcode): Encoding::Converter::CRLF_NEWLINE and
+	  Encoding::Converter::LF_NEWLINE defined.
+
 Wed Aug 13 14:22:16 2008  Tanaka Akira  <akr@f...>
 
 	* enc/trans/newline.trans: new file.
@@ -8,7 +19,7 @@
 	  (CRLF_NEWLINE): defined.
 	  (CR_NEWLINE): defined.
 	  (rb_trans_open_by_transcoder_entries): initialize last_tc.
-	  (trans_open_i): allocate one more room for newline converter.
+	  (trans_open_i): allocate one more room for output newline converter.
 	  (rb_trans_open): universal newline implemented.
 	  (more_output_buffer): take max_output argument instead ts.
 	  (output_replacement_character): take tc argument instead of ts.
Index: enc/trans/newline.trans
===================================================================
--- enc/trans/newline.trans	(revision 18556)
+++ enc/trans/newline.trans	(revision 18557)
@@ -47,10 +47,45 @@
     NULL, NULL, NULL, fun_so_universal_newline
 };
 
+<%
+  map_crlf = {}
+  map_crlf["{00-09,0b-ff}"] = :nomap
+  map_crlf["0a"] = "0d0a"
+%>
 
+<%= transcode_generate_node(ActionMap.parse(map_crlf), "crlf_newline") %>
+
+static const rb_transcoder
+rb_crlf_newline = {
+    "", "crlf_newline", &crlf_newline,
+    1, /* input_unit_length */
+    1, /* max_input */
+    2, /* max_output */
+    NULL, NULL, NULL, NULL
+};
+
+<%
+  map_crlf = {}
+  map_crlf["{00-09,0b-ff}"] = :nomap
+  map_crlf["0a"] = "0d"
+%>
+
+<%= transcode_generate_node(ActionMap.parse(map_crlf), "cr_newline") %>
+
+static const rb_transcoder
+rb_cr_newline = {
+    "", "cr_newline", &cr_newline,
+    1, /* input_unit_length */
+    1, /* max_input */
+    1, /* max_output */
+    NULL, NULL, NULL, NULL
+};
+
 void
 Init_newline(void)
 {
     rb_register_transcoder(&rb_universal_newline);
+    rb_register_transcoder(&rb_crlf_newline);
+    rb_register_transcoder(&rb_cr_newline);
 }
 
Index: test/ruby/test_econv.rb
===================================================================
--- test/ruby/test_econv.rb	(revision 18556)
+++ test/ruby/test_econv.rb	(revision 18557)
@@ -64,4 +64,16 @@
     ret = ec.primitive_convert(src="\nvwx", dst="", 50, Encoding::Converter::PARTIAL_INPUT)
     assert_equal([:ibuf_empty, "", "vwx"], [ret, src, dst])
   end
+
+  def test_crlf_newline
+    ec = Encoding::Converter.new("UTF-8", "EUC-JP", Encoding::Converter::CRLF_NEWLINE)
+    ret = ec.primitive_convert(src="abc\ndef", dst="", 50, 0)
+    assert_equal([:finished, "", "abc\r\ndef"], [ret, src, dst])
+  end
+
+  def test_cr_newline
+    ec = Encoding::Converter.new("UTF-8", "EUC-JP", Encoding::Converter::CR_NEWLINE)
+    ret = ec.primitive_convert(src="abc\ndef", dst="", 50, 0)
+    assert_equal([:finished, "", "abc\rdef"], [ret, src, dst])
+  end
 end
Index: transcode.c
===================================================================
--- transcode.c	(revision 18556)
+++ transcode.c	(revision 18557)
@@ -683,7 +683,7 @@
     transcoder_entry_t **entries;
 
     if (!*entries_ptr) {
-        entries = ALLOC_N(transcoder_entry_t *, depth+1+1);
+        entries = ALLOC_N(transcoder_entry_t *, depth+1+2);
         *entries_ptr = entries;
     }
     else {
@@ -704,6 +704,16 @@
     if (num_trans < 0 || !entries)
         return NULL;
 
+    if (flags & (CRLF_NEWLINE|CR_NEWLINE)) {
+        char *name = (flags & CRLF_NEWLINE) ? "crlf_newline" : "cr_newline";
+        transcoder_entry_t *e = get_transcoder_entry("", name);
+        if (!e)
+            return NULL;
+        MEMMOVE(entries+1, entries, transcoder_entry_t *, num_trans);
+        entries[0] = e;
+        num_trans++;
+    }
+
     if (flags & UNIVERSAL_NEWLINE) {
         transcoder_entry_t *e = get_transcoder_entry("universal_newline", "");
         if (!e)
@@ -1392,4 +1402,6 @@
     rb_define_method(rb_cEncodingConverter, "max_output", econv_max_output, 0);
     rb_define_const(rb_cEncodingConverter, "PARTIAL_INPUT", INT2FIX(PARTIAL_INPUT));
     rb_define_const(rb_cEncodingConverter, "UNIVERSAL_NEWLINE", INT2FIX(UNIVERSAL_NEWLINE));
+    rb_define_const(rb_cEncodingConverter, "CRLF_NEWLINE", INT2FIX(CRLF_NEWLINE));
+    rb_define_const(rb_cEncodingConverter, "CR_NEWLINE", INT2FIX(CR_NEWLINE));
 }

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

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