From fc9677332a1f2ce41912b57886d4733d61d192f1 Mon Sep 17 00:00:00 2001 From: nagachika Date: Wed, 28 May 2014 14:38:54 +0000 Subject: [PATCH] merge revision(s) r45646: [Backport #9765] * ext/stringio/stringio.c (strio_putc): fix for non-ascii encoding, like as IO#putc. [ruby-dev:48114] [Bug #9765] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/branches/ruby_2_1@46213 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- ChangeLog | 5 +++++ ext/stringio/stringio.c | 16 ++++++++-------- test/stringio/test_stringio.rb | 16 ++++++++++++++++ version.h | 2 +- 4 files changed, 30 insertions(+), 9 deletions(-) diff --git a/ChangeLog b/ChangeLog index ff85c30fb9..e484264525 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,8 @@ +Wed May 28 23:37:32 2014 Nobuyoshi Nakada + + * ext/stringio/stringio.c (strio_putc): fix for non-ascii + encoding, like as IO#putc. [ruby-dev:48114] [Bug #9765] + Wed May 28 01:05:06 2014 Nobuyoshi Nakada * lib/fileutils.rb (FileUtils#copy_entry): update rdoc about diff --git a/ext/stringio/stringio.c b/ext/stringio/stringio.c index 3fef619de6..fb8e7ce3ea 100644 --- a/ext/stringio/stringio.c +++ b/ext/stringio/stringio.c @@ -1233,17 +1233,17 @@ static VALUE strio_putc(VALUE self, VALUE ch) { struct StringIO *ptr = writable(self); - int c = NUM2CHR(ch); - long olen; + VALUE str; check_modifiable(ptr); - olen = RSTRING_LEN(ptr->string); - if (ptr->flags & FMODE_APPEND) { - ptr->pos = olen; + if (RB_TYPE_P(ch, T_STRING)) { + str = rb_str_substr(ch, 0, 1); } - strio_extend(ptr, ptr->pos, 1); - RSTRING_PTR(ptr->string)[ptr->pos++] = c; - OBJ_INFECT(ptr->string, self); + else { + char c = NUM2CHR(ch); + str = rb_str_new(&c, 1); + } + strio_write(self, str); return ch; } diff --git a/test/stringio/test_stringio.rb b/test/stringio/test_stringio.rb index f29322b393..c7db91aae1 100644 --- a/test/stringio/test_stringio.rb +++ b/test/stringio/test_stringio.rb @@ -419,6 +419,22 @@ class TestStringIO < Test::Unit::TestCase assert_equal("foo123", s) end + def test_putc_nonascii + s = "" + f = StringIO.new(s, "w") + f.putc("\u{3042}") + f.putc(0x3044) + f.close + assert_equal("\u{3042}D", s) + + s = "foo" + f = StringIO.new(s, "a") + f.putc("\u{3042}") + f.putc(0x3044) + f.close + assert_equal("foo\u{3042}D", s) + end + def test_read f = StringIO.new("\u3042\u3044") assert_raise(ArgumentError) { f.read(-1) } diff --git a/version.h b/version.h index b41bcef92a..784d02f3bf 100644 --- a/version.h +++ b/version.h @@ -1,6 +1,6 @@ #define RUBY_VERSION "2.1.2" #define RUBY_RELEASE_DATE "2014-05-28" -#define RUBY_PATCHLEVEL 112 +#define RUBY_PATCHLEVEL 113 #define RUBY_RELEASE_YEAR 2014 #define RUBY_RELEASE_MONTH 5