diff options
-rw-r--r-- | ChangeLog | 13 | ||||
-rw-r--r-- | enc/trans/newline.trans | 35 | ||||
-rw-r--r-- | test/ruby/test_econv.rb | 12 | ||||
-rw-r--r-- | transcode.c | 14 |
4 files changed, 72 insertions, 2 deletions
@@ -1,3 +1,14 @@ +Wed Aug 13 14:45:37 2008 Tanaka Akira <akr@fsij.org> + + * 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@fsij.org> * enc/trans/newline.trans: new file. @@ -8,7 +19,7 @@ Wed Aug 13 14:22:16 2008 Tanaka Akira <akr@fsij.org> (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. diff --git a/enc/trans/newline.trans b/enc/trans/newline.trans index 3d33a1e323..55be6c1d54 100644 --- a/enc/trans/newline.trans +++ b/enc/trans/newline.trans @@ -47,10 +47,45 @@ rb_universal_newline = { 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); } diff --git a/test/ruby/test_econv.rb b/test/ruby/test_econv.rb index 8a09f1e9bb..3b32419859 100644 --- a/test/ruby/test_econv.rb +++ b/test/ruby/test_econv.rb @@ -64,4 +64,16 @@ class TestEncodingConverter < Test::Unit::TestCase 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 diff --git a/transcode.c b/transcode.c index 507e1b0b9e..62e11c7551 100644 --- a/transcode.c +++ b/transcode.c @@ -683,7 +683,7 @@ trans_open_i(const char *from, const char *to, int depth, void *arg) 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 @@ rb_trans_open(const char *from, const char *to, int flags) 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 @@ Init_transcode(void) 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)); } |