ruby-changes:11775
From: nobu <ko1@a...>
Date: Thu, 14 May 2009 16:08:11 +0900 (JST)
Subject: [ruby-changes:11775] Ruby:r23424 (trunk): * ext/stringio/stringio.c (strio_ungetbyte): encoding should not
nobu 2009-05-14 16:07:55 +0900 (Thu, 14 May 2009) New Revision: 23424 http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=23424 Log: * ext/stringio/stringio.c (strio_ungetbyte): encoding should not be effective. Modified files: trunk/ChangeLog trunk/ext/stringio/stringio.c trunk/test/ruby/test_io.rb trunk/test/stringio/test_stringio.rb Index: ChangeLog =================================================================== --- ChangeLog (revision 23423) +++ ChangeLog (revision 23424) @@ -1,3 +1,8 @@ +Thu May 14 16:07:48 2009 Nobuyoshi Nakada <nobu@r...> + + * ext/stringio/stringio.c (strio_ungetbyte): encoding should no + be effective. + Thu May 14 10:17:45 2009 Nobuyoshi Nakada <nobu@r...> * sample/test.rb (valid_syntax?): defaults to us-ascii. Index: ext/stringio/stringio.c =================================================================== --- ext/stringio/stringio.c (revision 23423) +++ ext/stringio/stringio.c (revision 23424) @@ -744,8 +744,37 @@ static VALUE strio_ungetbyte(VALUE self, VALUE c) { - NUM2INT(c); - return strio_ungetc(self, c); + struct StringIO *ptr = readable(StringIO(self)); + char buf[1], *cp = buf; + long pos = ptr->pos, cl = 1; + VALUE str = ptr->string; + + if (NIL_P(c)) return Qnil; + if (FIXNUM_P(c)) { + buf[0] = (char)FIX2INT(c); + } + else { + SafeStringValue(c); + cp = RSTRING_PTR(c); + cl = RSTRING_LEN(c); + if (cl == 0) return Qnil; + } + rb_str_modify(str); + if (cl > pos) { + char *s; + long rest = RSTRING_LEN(str) - pos; + rb_str_resize(str, rest + cl); + s = RSTRING_PTR(str); + memmove(s + cl, s + pos, rest); + pos = 0; + } + else { + pos -= cl; + } + memcpy(RSTRING_PTR(str) + pos, cp, cl); + ptr->pos = pos; + RB_GC_GUARD(c); + return Qnil; } /* Index: test/ruby/test_io.rb =================================================================== --- test/ruby/test_io.rb (revision 23423) +++ test/ruby/test_io.rb (revision 23424) @@ -118,6 +118,22 @@ r.close end + def test_ungetbyte + t = make_tempfile + t.open + t.ungetbyte(0x41) + assert_equal(0x41, t.getbyte) + t.rewind + t.ungetbyte("qux") + assert_equal("quxfoo\n", t.gets) + t.set_encoding("utf-8") + t.ungetbyte(0x89) + t.ungetbyte(0x8e) + t.ungetbyte("\xe7") + t.ungetbyte("\xe7\xb4\x85") + assert_equal("\u7d05\u7389bar\n", t.gets) + end + def test_each_byte r, w = IO.pipe w << "abc def" Index: test/stringio/test_stringio.rb =================================================================== --- test/stringio/test_stringio.rb (revision 23423) +++ test/stringio/test_stringio.rb (revision 23424) @@ -289,6 +289,21 @@ f.close unless f.closed? end + def test_ungetbyte + s = "foo\nbar\n" + t = StringIO.new(s, "r") + t.ungetbyte(0x41) + assert_equal(0x41, t.getbyte) + t.ungetbyte("qux") + assert_equal("quxfoo\n", t.gets) + t.set_encoding("utf-8") + t.ungetbyte(0x89) + t.ungetbyte(0x8e) + t.ungetbyte("\xe7") + t.ungetbyte("\xe7\xb4\x85") + assert_equal("\u7d05\u7389bar\n", t.gets) + end + def test_ungetc s = "1234" f = StringIO.new(s, "r") -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/