% svn diff --diff-cmd diff -x '-u -p'
Index: io.c
===================================================================
--- io.c (revision 36217)
+++ io.c (working copy)
@@ -140,6 +140,7 @@ static VALUE argf;
static ID id_write, id_read, id_getc, id_flush, id_readpartial, id_set_encoding;
static VALUE sym_mode, sym_perm, sym_extenc, sym_intenc, sym_encoding, sym_open_args;
static VALUE sym_textmode, sym_binmode, sym_autoclose;
+static VALUE sym_set, sym_cur, sym_end;
struct argf {
VALUE filename, current_file;
@@ -1443,6 +1444,18 @@ rb_io_seek(VALUE io, VALUE offset, int w
return INT2FIX(0);
}
+static int
+interpret_seek_whence(VALUE vwhence)
+{
+ if (vwhence == sym_set)
+ return SEEK_SET;
+ if (vwhence == sym_cur)
+ return SEEK_CUR;
+ if (vwhence == sym_end)
+ return SEEK_END;
+ return NUM2INT(vwhence);
+}
+
/*
* call-seq:
* ios.seek(amount, whence=IO::SEEK_SET) -> 0
@@ -1450,12 +1463,12 @@ rb_io_seek(VALUE io, VALUE offset, int w
* Seeks to a given offset anInteger in the stream according to
* the value of whence:
*
- * IO::SEEK_CUR | Seeks to _amount_ plus current position
- * --------------+----------------------------------------------------
- * IO::SEEK_END | Seeks to _amount_ plus end of stream (you probably
- * | want a negative value for _amount_)
- * --------------+----------------------------------------------------
- * IO::SEEK_SET | Seeks to the absolute location given by _amount_
+ * :cur or IO::SEEK_CUR | Seeks to _amount_ plus current position
+ * ----------------------+--------------------------------------------------
+ * :end or IO::SEEK_END | Seeks to _amount_ plus end of stream (you
+ * | probably want a negative value for _amount_)
+ * ----------------------+--------------------------------------------------
+ * :set or IO::SEEK_SET | Seeks to the absolute location given by _amount_
*
* Example:
*
@@ -1471,7 +1484,7 @@ rb_io_seek_m(int argc, VALUE *argv, VALU
int whence = SEEK_SET;
if (rb_scan_args(argc, argv, "11", &offset, &ptrname) == 2) {
- whence = NUM2INT(ptrname);
+ whence = interpret_seek_whence(ptrname);
}
return rb_io_seek(io, offset, whence);
@@ -4233,7 +4246,7 @@ rb_io_sysseek(int argc, VALUE *argv, VAL
off_t pos;
if (rb_scan_args(argc, argv, "11", &offset, &ptrname) == 2) {
- whence = NUM2INT(ptrname);
+ whence = interpret_seek_whence(ptrname);
}
pos = NUM2OFFT(offset);
GetOpenFile(io, fptr);
@@ -11623,4 +11636,7 @@ Init_IO(void)
sym_willneed = ID2SYM(rb_intern("willneed"));
sym_dontneed = ID2SYM(rb_intern("dontneed"));
sym_noreuse = ID2SYM(rb_intern("noreuse"));
+ sym_set = ID2SYM(rb_intern("set"));
+ sym_cur = ID2SYM(rb_intern("cur"));
+ sym_end = ID2SYM(rb_intern("end"));
}
Index: test/ruby/test_io.rb
===================================================================
--- test/ruby/test_io.rb (revision 36217)
+++ test/ruby/test_io.rb (working copy)
@@ -1477,6 +1477,26 @@ class TestIO < Test::Unit::TestCase
}
end
+ def test_seek_symwhence
+ make_tempfile {|t|
+ open(t.path) { |f|
+ f.seek(9, :set)
+ assert_equal("az\n", f.read)
+ }
+
+ open(t.path) { |f|
+ f.seek(-4, :end)
+ assert_equal("baz\n", f.read)
+ }
+
+ open(t.path) { |f|
+ assert_equal("foo\n", f.gets)
+ f.seek(2, :cur)
+ assert_equal("r\nbaz\n", f.read)
+ }
+ }
+ end
+
def test_sysseek
t = make_tempfile