aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorakr <akr@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2013-04-03 12:25:09 +0000
committerakr <akr@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2013-04-03 12:25:09 +0000
commitb81ea661dbc3c31073de782a8b0b9f3f86e3de5e (patch)
tree4290400ce4aa2b7b0f74904ae584061ae199f647
parent2767139e056cb9b8585634f8eb9d174589f85905 (diff)
downloadruby-b81ea661dbc3c31073de782a8b0b9f3f86e3de5e.tar.gz
* io.c (rb_io_seek_m): Accept :CUR, :END, :SET as "whence" argument.
(interpret_seek_whence): New function. [ruby-dev:45818] [Feature #6643] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@40084 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
-rw-r--r--ChangeLog6
-rw-r--r--NEWS4
-rw-r--r--io.c32
-rw-r--r--test/ruby/test_io.rb20
4 files changed, 54 insertions, 8 deletions
diff --git a/ChangeLog b/ChangeLog
index ab260a6ef4..2c387194d5 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,9 @@
+Wed Apr 3 21:23:29 2013 Tanaka Akira <akr@fsij.org>
+
+ * io.c (rb_io_seek_m): Accept :CUR, :END, :SET as "whence" argument.
+ (interpret_seek_whence): New function.
+ [ruby-dev:45818] [Feature #6643]
+
Wed Apr 3 20:52:49 2013 Tanaka Akira <akr@fsij.org>
* process.c: Describe the behavior which Ruby invokes a commandline
diff --git a/NEWS b/NEWS
index 58c15536c3..b1a9804fb5 100644
--- a/NEWS
+++ b/NEWS
@@ -18,6 +18,10 @@ with all sufficient information, see the ChangeLog file.
* added environment variable:
* RUBY_HEAP_SLOTS_GROWTH_FACTOR: growth rate of the heap.
+* IO
+ * extended methods:
+ * IO#seek accepts symbols (:CUR, :END, :SET) for 2nd argument.
+
* Mutex
* misc
* Mutex#owned? is no longer experimental.
diff --git a/io.c b/io.c
index 2cd1325b03..ce6dc70af7 100644
--- a/io.c
+++ b/io.c
@@ -148,6 +148,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;
@@ -1533,6 +1534,18 @@ rb_io_seek(VALUE io, VALUE offset, int whence)
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
@@ -1540,12 +1553,12 @@ rb_io_seek(VALUE io, VALUE offset, int whence)
* Seeks to a given offset <i>anInteger</i> in the stream according to
* the value of <i>whence</i>:
*
- * 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:
*
@@ -1561,7 +1574,7 @@ rb_io_seek_m(int argc, VALUE *argv, VALUE io)
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);
@@ -4418,7 +4431,7 @@ rb_io_sysseek(int argc, VALUE *argv, VALUE io)
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);
@@ -11904,4 +11917,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"));
}
diff --git a/test/ruby/test_io.rb b/test/ruby/test_io.rb
index 8ccb5de03b..b968bb6fcd 100644
--- a/test/ruby/test_io.rb
+++ b/test/ruby/test_io.rb
@@ -1514,6 +1514,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
make_tempfile {|t|
open(t.path) do |f|