[前][次][番号順一覧][スレッド一覧]

ruby-changes:28032

From: akr <ko1@a...>
Date: Wed, 3 Apr 2013 21:25:20 +0900 (JST)
Subject: [ruby-changes:28032] akr:r40084 (trunk): * io.c (rb_io_seek_m): Accept :CUR, :END, :SET as "whence" argument.

akr	2013-04-03 21:25:09 +0900 (Wed, 03 Apr 2013)

  New Revision: 40084

  http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=40084

  Log:
    * io.c (rb_io_seek_m): Accept :CUR, :END, :SET as "whence" argument.
      (interpret_seek_whence): New function.
      [ruby-dev:45818] [Feature #6643]

  Modified files:
    trunk/ChangeLog
    trunk/NEWS
    trunk/io.c
    trunk/test/ruby/test_io.rb

Index: ChangeLog
===================================================================
--- ChangeLog	(revision 40083)
+++ ChangeLog	(revision 40084)
@@ -1,3 +1,9 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1
+Wed Apr  3 21:23:29 2013  Tanaka Akira  <akr@f...>
+
+	 * 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@f...>
 
 	* process.c: Describe the behavior which Ruby invokes a commandline
Index: io.c
===================================================================
--- io.c	(revision 40083)
+++ io.c	(revision 40084)
@@ -148,6 +148,7 @@ static VALUE argf; https://github.com/ruby/ruby/blob/trunk/io.c#L148
 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 w https://github.com/ruby/ruby/blob/trunk/io.c#L1534
     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 w https://github.com/ruby/ruby/blob/trunk/io.c#L1553
  *  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, VALU https://github.com/ruby/ruby/blob/trunk/io.c#L1574
     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, VAL https://github.com/ruby/ruby/blob/trunk/io.c#L4431
     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) https://github.com/ruby/ruby/blob/trunk/io.c#L11917
     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: NEWS
===================================================================
--- NEWS	(revision 40083)
+++ NEWS	(revision 40084)
@@ -18,6 +18,10 @@ with all sufficient information, see the https://github.com/ruby/ruby/blob/trunk/NEWS#L18
   * 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.
Index: test/ruby/test_io.rb
===================================================================
--- test/ruby/test_io.rb	(revision 40083)
+++ test/ruby/test_io.rb	(revision 40084)
@@ -1514,6 +1514,26 @@ class TestIO < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_io.rb#L1514
     }
   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|

--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]