From 461bd62fec54d1026ef2496fbc9b9fd676f4d9d9 Mon Sep 17 00:00:00 2001 From: akr Date: Fri, 21 Aug 2009 15:51:35 +0000 Subject: [PATCH] * enumerator.c (ary2sv): add dup argument. (enumerator_next): call ary2sv with dup=0. (enumerator_peek): call ary2sv with dup=1 to return duplicated array. (enumerator_peek_values_m): new function to return duplicated array. (Init_Enumerator): use enumerator_peek_values_m as Enumerator#peek_value. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@24619 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- ChangeLog | 9 +++++++++ enumerator.c | 30 +++++++++++++++++++----------- test/ruby/test_enumerator.rb | 22 ++++++++++++++++++++++ 3 files changed, 50 insertions(+), 11 deletions(-) diff --git a/ChangeLog b/ChangeLog index b91eb5113f..a4b513e9f0 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,12 @@ +Sat Aug 22 00:48:08 2009 Tanaka Akira + + * enumerator.c (ary2sv): add dup argument. + (enumerator_next): call ary2sv with dup=0. + (enumerator_peek): call ary2sv with dup=1 to return duplicated array. + (enumerator_peek_values_m): new function to return duplicated array. + (Init_Enumerator): use enumerator_peek_values_m as + Enumerator#peek_value. + Sat Aug 22 00:03:19 2009 Yusuke Endoh * thread.c (rb_check_deadlock): decrease number of sleepers before diff --git a/enumerator.c b/enumerator.c index 2f9a5589e7..9d5c39009a 100644 --- a/enumerator.c +++ b/enumerator.c @@ -680,7 +680,7 @@ enumerator_next_values(VALUE obj) } static VALUE -ary2sv(VALUE args) +ary2sv(VALUE args, int dup) { if (TYPE(args) != T_ARRAY) return args; @@ -693,6 +693,8 @@ ary2sv(VALUE args) return RARRAY_PTR(args)[0]; default: + if (dup) + return rb_ary_dup(args); return args; } } @@ -715,7 +717,18 @@ static VALUE enumerator_next(VALUE obj) { VALUE vs = enumerator_next_values(obj); - return ary2sv(vs); + return ary2sv(vs, 0); +} + +static VALUE +enumerator_peek_values(VALUE obj) +{ + struct enumerator *e = enumerator_ptr(obj); + + if (e->lookahead == Qundef) { + e->lookahead = get_next_values(obj, e); + } + return e->lookahead; } /* @@ -744,14 +757,9 @@ enumerator_next(VALUE obj) */ static VALUE -enumerator_peek_values(VALUE obj) +enumerator_peek_values_m(VALUE obj) { - struct enumerator *e = enumerator_ptr(obj); - - if (e->lookahead == Qundef) { - e->lookahead = get_next_values(obj, e); - } - return e->lookahead; + return rb_ary_dup(enumerator_peek_values(obj)); } /* @@ -768,7 +776,7 @@ static VALUE enumerator_peek(VALUE obj) { VALUE vs = enumerator_peek_values(obj); - return ary2sv(vs); + return ary2sv(vs, 1); } /* @@ -1153,7 +1161,7 @@ Init_Enumerator(void) rb_define_method(rb_cEnumerator, "with_index", enumerator_with_index, -1); rb_define_method(rb_cEnumerator, "with_object", enumerator_with_object, 1); rb_define_method(rb_cEnumerator, "next_values", enumerator_next_values, 0); - rb_define_method(rb_cEnumerator, "peek_values", enumerator_peek_values, 0); + rb_define_method(rb_cEnumerator, "peek_values", enumerator_peek_values_m, 0); rb_define_method(rb_cEnumerator, "next", enumerator_next, 0); rb_define_method(rb_cEnumerator, "peek", enumerator_peek, 0); rb_define_method(rb_cEnumerator, "feed", enumerator_feed, 1); diff --git a/test/ruby/test_enumerator.rb b/test/ruby/test_enumerator.rb index e70a783a00..fe8a573b2b 100644 --- a/test/ruby/test_enumerator.rb +++ b/test/ruby/test_enumerator.rb @@ -141,6 +141,28 @@ class TestEnumerator < Test::Unit::TestCase assert_raise(StopIteration) { e.peek } end + def test_peek_modify + o = Object.new + def o.each + yield 1,2 + end + e = o.to_enum + a = e.peek + a << 3 + assert_equal([1,2], e.peek) + end + + def test_peek_values_modify + o = Object.new + def o.each + yield 1,2 + end + e = o.to_enum + a = e.peek_values + a << 3 + assert_equal([1,2], e.peek) + end + def test_next_after_stopiteration a = [1] e = a.each