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

ruby-changes:33565

From: nobu <ko1@a...>
Date: Mon, 21 Apr 2014 14:11:57 +0900 (JST)
Subject: [ruby-changes:33565] nobu:r45646 (trunk): stringio.c: non-ascii encoding

nobu	2014-04-21 14:11:50 +0900 (Mon, 21 Apr 2014)

  New Revision: 45646

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

  Log:
    stringio.c: non-ascii encoding
    
    * ext/stringio/stringio.c (strio_putc): fix for non-ascii
      encoding, like as IO#putc.  [ruby-dev:48114] [Bug #9765]

  Modified files:
    trunk/ChangeLog
    trunk/ext/stringio/stringio.c
    trunk/test/stringio/test_stringio.rb
Index: ChangeLog
===================================================================
--- ChangeLog	(revision 45645)
+++ ChangeLog	(revision 45646)
@@ -1,3 +1,8 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1
+Mon Apr 21 14:11:48 2014  Nobuyoshi Nakada  <nobu@r...>
+
+	* ext/stringio/stringio.c (strio_putc): fix for non-ascii
+	  encoding, like as IO#putc.  [ruby-dev:48114] [Bug #9765]
+
 Sun Apr 20 12:57:15 2014  Masaya Tarui  <tarui@r...>
 
 	* st.c (st_foreach_check): change start point of search at check
Index: ext/stringio/stringio.c
===================================================================
--- ext/stringio/stringio.c	(revision 45645)
+++ ext/stringio/stringio.c	(revision 45646)
@@ -1233,17 +1233,17 @@ static VALUE https://github.com/ruby/ruby/blob/trunk/ext/stringio/stringio.c#L1233
 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;
 }
 
Index: test/stringio/test_stringio.rb
===================================================================
--- test/stringio/test_stringio.rb	(revision 45645)
+++ test/stringio/test_stringio.rb	(revision 45646)
@@ -419,6 +419,22 @@ class TestStringIO < Test::Unit::TestCas https://github.com/ruby/ruby/blob/trunk/test/stringio/test_stringio.rb#L419
     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) }

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

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