diff options
author | matz <matz@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2008-10-21 22:10:49 +0000 |
---|---|---|
committer | matz <matz@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2008-10-21 22:10:49 +0000 |
commit | 81525af4c9a8e512db6bd8b73c6fbe5ea39c865a (patch) | |
tree | 1dae2e33418150469470a784a251fd160dc69fa3 /ext/stringio | |
parent | 34d1e9bb2504d7fa0b98ad99a32c549cd0d92853 (diff) | |
download | ruby-81525af4c9a8e512db6bd8b73c6fbe5ea39c865a.tar.gz |
* 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.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@19883 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'ext/stringio')
-rw-r--r-- | ext/stringio/stringio.c | 22 |
1 files changed, 20 insertions, 2 deletions
diff --git a/ext/stringio/stringio.c b/ext/stringio/stringio.c index b907047137..191c1024c4 100644 --- a/ext/stringio/stringio.c +++ b/ext/stringio/stringio.c @@ -701,7 +701,7 @@ strio_ungetc(VALUE self, VALUE c) 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 @@ strio_ungetc(VALUE self, VALUE c) } 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 @@ strio_ungetc(VALUE self, VALUE c) /* * 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 @@ Init_stringio() 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); |