method
read
v1_9_1_378 -
Show latest stable
- Class:
StringIO
read(*args)public
See IO#read.
static VALUE
strio_read(int argc, VALUE *argv, VALUE self)
{
struct StringIO *ptr = readable(StringIO(self));
VALUE str = Qnil;
long len;
switch (argc) {
case 2:
str = argv[1];
StringValue(str);
rb_str_modify(str);
case 1:
if (!NIL_P(argv[0])) {
len = NUM2LONG(argv[0]);
if (len < 0) {
rb_raise(rb_eArgError, "negative length %ld given", len);
}
if (len > 0 && ptr->pos >= RSTRING_LEN(ptr->string)) {
if (!NIL_P(str)) rb_str_resize(str, 0);
return Qnil;
}
break;
}
/* fall through */
case 0:
len = RSTRING_LEN(ptr->string);
if (len <= ptr->pos) {
if (NIL_P(str)) {
str = rb_str_new(0, 0);
}
else {
rb_str_resize(str, 0);
}
return str;
}
else {
len -= ptr->pos;
}
break;
default:
rb_raise(rb_eArgError, "wrong number of arguments (%d for 0)", argc);
}
if (NIL_P(str)) {
str = strio_substr(ptr, ptr->pos, len);
if (argc > 0) rb_enc_associate(str, rb_ascii8bit_encoding());
}
else {
long rest = RSTRING_LEN(ptr->string) - ptr->pos;
if (len > rest) len = rest;
rb_str_resize(str, len);
MEMCPY(RSTRING_PTR(str), RSTRING_PTR(ptr->string) + ptr->pos, char, len);
}
if (NIL_P(str)) {
str = rb_str_new(0, 0);
len = 0;
}
else {
ptr->pos += len = RSTRING_LEN(str);
}
return str;
} Related methods
- Instance methods
- <<
- binmode
- bytes
- chars
- close
- close_read
- close_write
- closed?
- closed_read?
- closed_write?
- each
- each_byte
- each_char
- each_line
- eof
- eof?
- external_encoding
- fcntl
- fileno
- flush
- fsync
- getbyte
- getc
- gets
- initialize_copy
- internal_encoding
- isatty
- length
- lineno
- lineno=
- lines
- path
- pid
- pos
- pos=
- printf
- putc
- puts
- read
- readbyte
- readchar
- readline
- readlines
- readpartial
- reopen
- rewind
- seek
- set_encoding
- size
- string
- string=
- sync
- sync=
- sysread
- syswrite
- tell
- truncate
- tty?
- ungetbyte
- ungetc
- write
- Class methods
- new
- new
- open