ruby-changes:8355
From: matz <ko1@a...>
Date: Wed, 22 Oct 2008 07:11:06 +0900 (JST)
Subject: [ruby-changes:8355] Ruby:r19883 (trunk): * ext/zlib/zlib.c (rb_gzreader_ungetc): should be able to unget
matz 2008-10-22 07:10:49 +0900 (Wed, 22 Oct 2008) New Revision: 19883 http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=19883 Log: * ext/zlib/zlib.c (rb_gzreader_ungetc): should be able to unget Fixnum. * ext/stringio/stringio.c (strio_ungetc): should convert unget string. * ext/stringio/stringio.c (strio_ungetbyte): new method. Modified files: trunk/ChangeLog trunk/ext/stringio/stringio.c trunk/ext/zlib/zlib.c Index: ChangeLog =================================================================== --- ChangeLog (revision 19882) +++ ChangeLog (revision 19883) @@ -1,3 +1,13 @@ +Wed Oct 22 07:09:19 2008 Yukihiro Matsumoto <matz@r...> + + * ext/zlib/zlib.c (rb_gzreader_ungetc): should be able to unget + Fixnum. + + * ext/stringio/stringio.c (strio_ungetc): should convert unget + string. + + * ext/stringio/stringio.c (strio_ungetbyte): new method. + Wed Oct 22 05:46:25 2008 Koichi Sasada <ko1@a...> * include/ruby/vm.h: write a comment. Index: ext/zlib/zlib.c =================================================================== --- ext/zlib/zlib.c (revision 19882) +++ ext/zlib/zlib.c (revision 19883) @@ -3075,8 +3075,11 @@ static VALUE rb_gzreader_ungetc(VALUE obj, VALUE s) { - struct gzfile *gz = get_gzfile(obj); + struct gzfile *gz; + if (FIXNUM_P(s)) + return rb_gzreader_ungetbyte(obj, s); + gz = get_gzfile(obj); StringValue(s); if (gz->enc2 && gz->enc2 != rb_ascii8bit_encoding()) { s = rb_str_conv_enc(s, rb_enc_get(s), gz->enc2); Index: ext/stringio/stringio.c =================================================================== --- ext/stringio/stringio.c (revision 19882) +++ ext/stringio/stringio.c (revision 19883) @@ -701,7 +701,7 @@ struct StringIO *ptr = readable(StringIO(self)); long lpos, clen; char *p, *pend; - rb_encoding *enc; + rb_encoding *enc, *enc2; if (NIL_P(c)) return Qnil; if (FIXNUM_P(c)) { @@ -714,7 +714,11 @@ } else { SafeStringValue(c); - enc = rb_enc_check(ptr->string, c); + enc = rb_enc_get(ptr->string); + enc2 = rb_enc_get(c); + if (enc != enc2 && enc != rb_ascii8bit_encoding()) { + c = rb_str_conv_enc(c, enc2, enc); + } } /* get logical position */ lpos = 0; p = RSTRING_PTR(ptr->string); pend = p + ptr->pos - 1; @@ -732,6 +736,19 @@ /* * call-seq: + * strio.ungetbyte(fixnum) -> nil + * + * See IO#ungetbyte + */ +static VALUE +strio_ungetbyte(VALUE self, VALUE c) +{ + NUM2INT(c); + return strio_ungetc(self, c); +} + +/* + * call-seq: * strio.readchar -> fixnum * * See IO#readchar. @@ -1318,6 +1335,7 @@ rb_define_method(StringIO, "chars", strio_each_char, 0); rb_define_method(StringIO, "getc", strio_getc, 0); rb_define_method(StringIO, "ungetc", strio_ungetc, 1); + rb_define_method(StringIO, "ungetbyte", strio_ungetbyte, 1); rb_define_method(StringIO, "readchar", strio_readchar, 0); rb_define_method(StringIO, "getbyte", strio_getbyte, 0); rb_define_method(StringIO, "readbyte", strio_readbyte, 0); -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/