@@ -161,6 +161,16 @@ strio_substr(struct StringIO *ptr, long pos, long len, rb_encoding *enc)
161161 return enc_subseq (str , pos , len , enc );
162162}
163163
164+ static VALUE
165+ strio_readbuf (struct StringIO * ptr , VALUE str )
166+ {
167+ if (!NIL_P (str )) {
168+ StringValue (str );
169+ rb_str_modify (str );
170+ }
171+ return str ;
172+ }
173+
164174#define StringIO (obj ) get_strio(obj)
165175#define StringIOForRead (obj ) get_strio_for_read(obj)
166176
@@ -1684,11 +1694,7 @@ strio_read(int argc, VALUE *argv, VALUE self)
16841694
16851695 switch (argc ) {
16861696 case 2 :
1687- str = argv [1 ];
1688- if (!NIL_P (str )) {
1689- StringValue (str );
1690- rb_str_modify (str );
1691- }
1697+ str = strio_readbuf (ptr , argv [1 ]);
16921698 /* fall through */
16931699 case 1 :
16941700 if (!NIL_P (argv [0 ])) {
@@ -1753,6 +1759,8 @@ static VALUE
17531759strio_pread (int argc , VALUE * argv , VALUE self )
17541760{
17551761 VALUE rb_len , rb_offset , rb_buf ;
1762+ struct StringIO * ptr = readable (self );
1763+
17561764 rb_scan_args (argc , argv , "21" , & rb_len , & rb_offset , & rb_buf );
17571765 long len = NUM2LONG (rb_len );
17581766 long offset = NUM2LONG (rb_offset );
@@ -1761,19 +1769,19 @@ strio_pread(int argc, VALUE *argv, VALUE self)
17611769 rb_raise (rb_eArgError , "negative string size (or size too big): %" PRIsVALUE , rb_len );
17621770 }
17631771
1772+ if (offset < 0 ) {
1773+ rb_syserr_fail_str (EINVAL , rb_sprintf ("pread: Invalid offset argument: %" PRIsVALUE , rb_offset ));
1774+ }
1775+
1776+ rb_buf = strio_readbuf (ptr , rb_buf );
1777+
17641778 if (len == 0 ) {
17651779 if (NIL_P (rb_buf )) {
17661780 return rb_str_new ("" , 0 );
17671781 }
17681782 return rb_buf ;
17691783 }
17701784
1771- if (offset < 0 ) {
1772- rb_syserr_fail_str (EINVAL , rb_sprintf ("pread: Invalid offset argument: %" PRIsVALUE , rb_offset ));
1773- }
1774-
1775- struct StringIO * ptr = readable (self );
1776-
17771785 if (outside_p (ptr , offset )) {
17781786 rb_eof_error ();
17791787 }
0 commit comments