summaryrefslogtreecommitdiff
path: root/ext
diff options
context:
space:
mode:
authornaruse <naruse@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2019-01-17 21:36:17 +0000
committernaruse <naruse@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2019-01-17 21:36:17 +0000
commit4cb618e7aacf84cf7c241ee0513bbb37e1068c9d (patch)
tree9022b6c440e8dcaf11d9f0eb04125ef84fb58677 /ext
parentb3b786c580995b31dcc0259890cefdb1385998c6 (diff)
merge revision(s) 66760,66761,66824: [Backport #15460]
Follow behaviour of IO#ungetbyte see r65802 and [Bug #14359] * expand tabs. setbyte / ungetbyte allow out-of-range integers * string.c: String#setbyte to accept arbitrary integers [Bug #15460] * io.c: ditto for IO#ungetbyte * ext/strringio/stringio.c: ditto for StringIO#ungetbyte git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/branches/ruby_2_6@66845 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'ext')
-rw-r--r--ext/stringio/stringio.c34
1 files changed, 19 insertions, 15 deletions
diff --git a/ext/stringio/stringio.c b/ext/stringio/stringio.c
index eb23109087..47c2c50b95 100644
--- a/ext/stringio/stringio.c
+++ b/ext/stringio/stringio.c
@@ -803,24 +803,28 @@ static VALUE
strio_ungetbyte(VALUE self, VALUE c)
{
struct StringIO *ptr = readable(self);
- char buf[1], *cp = buf;
- long cl = 1;
check_modifiable(ptr);
- if (NIL_P(c)) return Qnil;
- if (FIXNUM_P(c)) {
- buf[0] = (char)FIX2INT(c);
- return strio_unget_bytes(ptr, buf, 1);
- }
- else {
- SafeStringValue(c);
- cp = RSTRING_PTR(c);
- cl = RSTRING_LEN(c);
- if (cl == 0) return Qnil;
- strio_unget_bytes(ptr, cp, cl);
- RB_GC_GUARD(c);
- return Qnil;
+ switch (TYPE(c)) {
+ case T_NIL:
+ return Qnil;
+ case T_FIXNUM:
+ case T_BIGNUM: ;
+ /* rb_int_modulo() not visible from exts */
+ VALUE v = rb_funcall(c, rb_intern("modulo"), 1, INT2FIX(256));
+ unsigned char cc = NUM2INT(v) & 0xFF;
+ c = rb_str_new((const char *)&cc, 1);
+ break;
+ default:
+ SafeStringValue(c);
}
+
+ const char *cp = RSTRING_PTR(c);
+ long cl = RSTRING_LEN(c);
+ if (cl == 0) return Qnil;
+ strio_unget_bytes(ptr, cp, cl);
+ RB_GC_GUARD(c);
+ return Qnil;
}
static VALUE