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

ruby-changes:5087

From: knu <ko1@a...>
Date: Sun, 25 May 2008 05:08:08 +0900 (JST)
Subject: [ruby-changes:5087] Ruby:r16582 (ruby_1_8): * array.c (rb_ary_slice_bang): Be consistent with Array#slice()

knu	2008-05-25 05:07:54 +0900 (Sun, 25 May 2008)

  New Revision: 16582

  Modified files:
    branches/ruby_1_8/ChangeLog
    branches/ruby_1_8/array.c
    branches/ruby_1_8/enumerator.c

  Log:
    * array.c (rb_ary_slice_bang): Be consistent with Array#slice()
      and String#slice!().  Just return nil when a negative length or
      out of boundary index is given instead of raising an exception
      via internal functions.
      (rb_ary_slice_bang): should not use rb_ary_subseq() which shares
      internal pointer.  splice modifies the receiver right after
      subseq.  [ruby-dev:34005]
      (rb_ary_slice_bang): should adjust length before making
      sub-array.
    
    * enumerator.c (Init_Enumerator): Override
      Enumerable::Enumerator#each_with_index with #with_index.


  http://svn.ruby-lang.org/cgi-bin/viewvc.cgi/branches/ruby_1_8/ChangeLog?r1=16582&r2=16581&diff_format=u
  http://svn.ruby-lang.org/cgi-bin/viewvc.cgi/branches/ruby_1_8/enumerator.c?r1=16582&r2=16581&diff_format=u
  http://svn.ruby-lang.org/cgi-bin/viewvc.cgi/branches/ruby_1_8/array.c?r1=16582&r2=16581&diff_format=u

Index: ruby_1_8/array.c
===================================================================
--- ruby_1_8/array.c	(revision 16581)
+++ ruby_1_8/array.c	(revision 16582)
@@ -2078,17 +2078,24 @@
     VALUE ary;
 {
     VALUE arg1, arg2;
-    long pos, len;
+    long pos, len, orig_len;
 
     if (rb_scan_args(argc, argv, "11", &arg1, &arg2) == 2) {
 	pos = NUM2LONG(arg1);
 	len = NUM2LONG(arg2);
       delete_pos_len:
+	if (len < 0) return Qnil;
+	orig_len = RARRAY_LEN(ary);
 	if (pos < 0) {
-	    pos = RARRAY(ary)->len + pos;
+	    pos += orig_len;
 	    if (pos < 0) return Qnil;
 	}
-	arg2 = rb_ary_subseq(ary, pos, len);
+	else if (orig_len <= pos) return Qnil;
+	if (orig_len < pos + len) {
+	    len = orig_len - pos;
+	}
+	arg2 = rb_ary_new4(len, RARRAY_PTR(ary)+pos);
+	RBASIC(arg2)->klass = rb_obj_class(ary);
 	rb_ary_splice(ary, pos, len, Qnil);	/* Qnil/rb_ary_new2(0) */
 	return arg2;
     }
Index: ruby_1_8/ChangeLog
===================================================================
--- ruby_1_8/ChangeLog	(revision 16581)
+++ ruby_1_8/ChangeLog	(revision 16582)
@@ -1,3 +1,18 @@
+Sun May 25 05:07:19 2008  Akinori MUSHA  <knu@i...>
+
+	* array.c (rb_ary_slice_bang): Be consistent with Array#slice()
+	  and String#slice!().  Just return nil when a negative length or
+	  out of boundary index is given instead of raising an exception
+	  via internal functions.
+	  (rb_ary_slice_bang): should not use rb_ary_subseq() which shares
+	  internal pointer.  splice modifies the receiver right after
+	  subseq.  [ruby-dev:34005]
+	  (rb_ary_slice_bang): should adjust length before making
+	  sub-array.
+
+	* enumerator.c (Init_Enumerator): Override
+	  Enumerable::Enumerator#each_with_index with #with_index.
+
 Sun May 25 03:13:09 2008  Akinori MUSHA  <knu@i...>
 
 	* eval.c (Init_Thread): Initialize recursive_key.
Index: ruby_1_8/enumerator.c
===================================================================
--- ruby_1_8/enumerator.c	(revision 16581)
+++ ruby_1_8/enumerator.c	(revision 16582)
@@ -422,6 +422,7 @@
     rb_define_method(rb_cEnumerator, "initialize", enumerator_initialize, -1);
     rb_define_method(rb_cEnumerator, "initialize_copy", enumerator_init_copy, 1);
     rb_define_method(rb_cEnumerator, "each", enumerator_each, 0);
+    rb_define_method(rb_cEnumerator, "each_with_index", enumerator_with_index, 0);
     rb_define_method(rb_cEnumerator, "with_index", enumerator_with_index, 0);
     rb_define_method(rb_cEnumerator, "next", enumerator_next, 0);
     rb_define_method(rb_cEnumerator, "rewind", enumerator_rewind, 0);

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

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