aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--ext/stringio/stringio.c43
-rw-r--r--test/stringio/test_stringio.rb19
2 files changed, 62 insertions, 0 deletions
diff --git a/ext/stringio/stringio.c b/ext/stringio/stringio.c
index 68306cac4b..f5213f2fc0 100644
--- a/ext/stringio/stringio.c
+++ b/ext/stringio/stringio.c
@@ -1584,6 +1584,48 @@ strio_read(int argc, VALUE *argv, VALUE self)
}
/*
+ * call-seq:
+ * pread(maxlen, offset) -> string
+ * pread(maxlen, offset, out_string) -> string
+ *
+ * See IO#pread.
+ */
+static VALUE
+strio_pread(int argc, VALUE *argv, VALUE self)
+{
+ VALUE rb_len, rb_offset, rb_buf;
+ rb_scan_args(argc, argv, "21", &rb_len, &rb_offset, &rb_buf);
+ long len = NUM2LONG(rb_len);
+ long offset = NUM2LONG(rb_offset);
+
+ if (len < 0) {
+ rb_raise(rb_eArgError, "negative string size (or size too big): %" PRIsVALUE, rb_len);
+ }
+
+ if (offset < 0) {
+ rb_syserr_fail_str(EINVAL, rb_sprintf("pread: Invalid offset argument: %" PRIsVALUE, rb_offset));
+ }
+
+ struct StringIO *ptr = readable(self);
+
+ if (offset >= RSTRING_LEN(ptr->string)) {
+ rb_eof_error();
+ }
+
+ if (NIL_P(rb_buf)) {
+ return strio_substr(ptr, offset, len, rb_ascii8bit_encoding());
+ }
+
+ long rest = RSTRING_LEN(ptr->string) - offset;
+ if (len > rest) len = rest;
+ rb_str_resize(rb_buf, len);
+ rb_enc_associate(rb_buf, rb_ascii8bit_encoding());
+ MEMCPY(RSTRING_PTR(rb_buf), RSTRING_PTR(ptr->string) + offset, char, len);
+ return rb_buf;
+}
+
+
+/*
* call-seq:
* strio.sysread(integer[, outbuf]) -> string
* strio.readpartial(integer[, outbuf]) -> string
@@ -1843,6 +1885,7 @@ Init_stringio(void)
rb_define_method(StringIO, "gets", strio_gets, -1);
rb_define_method(StringIO, "readlines", strio_readlines, -1);
rb_define_method(StringIO, "read", strio_read, -1);
+ rb_define_method(StringIO, "pread", strio_pread, -1);
rb_define_method(StringIO, "write", strio_write_m, -1);
rb_define_method(StringIO, "putc", strio_putc, 1);
diff --git a/test/stringio/test_stringio.rb b/test/stringio/test_stringio.rb
index fd9974caeb..246f107d05 100644
--- a/test/stringio/test_stringio.rb
+++ b/test/stringio/test_stringio.rb
@@ -729,6 +729,25 @@ class TestStringIO < Test::Unit::TestCase
assert_equal Encoding::ASCII_8BIT, f.sysread(3).encoding
end
+ def test_pread
+ f = StringIO.new("pread")
+ f.read
+
+ assert_equal "pre".b, f.pread(3, 0)
+ assert_equal "read".b, f.pread(4, 1)
+ assert_equal Encoding::ASCII_8BIT, f.pread(4, 1).encoding
+
+ buf = "".b
+ f.pread(3, 0, buf)
+ assert_equal "pre".b, buf
+ f.pread(4, 1, buf)
+ assert_equal "read".b, buf
+
+ assert_raise(EOFError) { f.pread(1, 5) }
+ assert_raise(ArgumentError) { f.pread(-1, 0) }
+ assert_raise(Errno::EINVAL) { f.pread(3, -1) }
+ end
+
def test_size
f = StringIO.new("1234")
assert_equal(4, f.size)