From: MoonWolf Date: 2010-09-09T14:48:30+09:00 Subject: [ruby-dev:42212] putsの不具合 This is a multi-part message in MIME format. --------------040205060909000608050301 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit putsで最後の文字ではなく最後のバイトが'\n'かチェックしているため、一部の エンコーディングで不具合がでます。 test.txtの(9)の後の改行が出力されません。 puts_test.rb: #!/usr/bin/ruby # -*- coding: UTF-8 -*- open("test.txt","w:UTF-8") {|f| f.puts "(1)test" f.puts "(2)test".encode("UTF-16BE") f.puts "(3)日本語" f.puts "(4)日本語".encode("UTF-16BE") f.puts "(5)\u0109" f.puts "(6)\u010a" f.puts "(7)\u010b" f.puts "(8)\u0109".encode("UTF-16BE") f.puts "(9)\u010a".encode("UTF-16BE") f.puts "(10)\u010b".encode("UTF-16BE") } test.txt: (1)test (2)test (3)日本語 (4)日本語 (5)ĉ (6)Ċ (7)ċ (8)ĉ (9)Ċ(10)ċ 文字単位でチェックすると遅いですし、添付のパッチのようにいまの複雑な仕様 をシンプルなものに変えちゃうのもありかと思います。 -- MoonWolf --------------040205060909000608050301 Content-Type: text/x-patch; name="puts.patch" Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename="puts.patch" Index: io.c =================================================================== --- io.c (revision 29201) +++ io.c (working copy) @@ -6153,10 +6153,7 @@ line = rb_obj_as_string(argv[i]); string: rb_io_write(out, line); - if (RSTRING_LEN(line) == 0 || - RSTRING_PTR(line)[RSTRING_LEN(line)-1] != '\n') { - rb_io_write(out, rb_default_rs); - } + rb_io_write(out, rb_default_rs); } return Qnil; --------------040205060909000608050301--