summaryrefslogtreecommitdiff
path: root/io.c
diff options
context:
space:
mode:
authornaruse <naruse@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2019-01-17 21:36:17 (GMT)
committernaruse <naruse@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2019-01-17 21:36:17 (GMT)
commit4cb618e7aacf84cf7c241ee0513bbb37e1068c9d (patch)
tree9022b6c440e8dcaf11d9f0eb04125ef84fb58677 /io.c
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 'io.c')
-rw-r--r--io.c28
1 files changed, 11 insertions, 17 deletions
diff --git a/io.c b/io.c
index 24dacc5..7de405d 100644
--- a/io.c
+++ b/io.c
@@ -4258,23 +4258,17 @@ rb_io_ungetbyte(VALUE io, VALUE b)
GetOpenFile(io, fptr);
rb_io_check_byte_readable(fptr);
- if (NIL_P(b)) return Qnil;
- if (FIXNUM_P(b)) {
- int i = FIX2INT(b);
- if (0 <= i && i <= UCHAR_MAX) {
- unsigned char cc = i & 0xFF;
- b = rb_str_new((const char *)&cc, 1);
- }
- else {
- rb_raise(rb_eRangeError,
- "integer %d too big to convert into `unsigned char'", i);
- }
- }
- else if (RB_TYPE_P(b, T_BIGNUM)) {
- rb_raise(rb_eRangeError, "bignum too big to convert into `unsigned char'");
- }
- else {
- SafeStringValue(b);
+ switch (TYPE(b)) {
+ case T_NIL:
+ return Qnil;
+ case T_FIXNUM:
+ case T_BIGNUM: ;
+ VALUE v = rb_int_modulo(b, INT2FIX(256));
+ unsigned char c = NUM2INT(v) & 0xFF;
+ b = rb_str_new((const char *)&c, 1);
+ break;
+ default:
+ SafeStringValue(b);
}
io_ungetbyte(b, fptr);
return Qnil;