diff options
author | nobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2009-09-29 15:37:28 +0000 |
---|---|---|
committer | nobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2009-09-29 15:37:28 +0000 |
commit | 58a4e147ef57667e1d63045c60c68e75b774765e (patch) | |
tree | 48159ef0af1071b686d01d0d017b1dae25897835 | |
parent | 3f3e44962cedb1e5c607f62004789d6876ab0ff0 (diff) | |
download | ruby-58a4e147ef57667e1d63045c60c68e75b774765e.tar.gz ruby-58a4e147ef57667e1d63045c60c68e75b774765e.tar.xz ruby-58a4e147ef57667e1d63045c60c68e75b774765e.zip |
* enumerator.c (enumerator_block_call): extracted.
git-svn-id: http://svn.ruby-lang.org/repos/ruby/trunk@25164 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
-rw-r--r-- | ChangeLog | 4 | ||||
-rw-r--r-- | enumerator.c | 58 |
2 files changed, 26 insertions, 36 deletions
@@ -1,3 +1,7 @@ +Wed Sep 30 00:37:27 2009 Nobuyoshi Nakada <nobu@ruby-lang.org> + + * enumerator.c (enumerator_block_call): extracted. + Wed Sep 30 00:00:25 2009 NARUSE, Yui <naruse@ruby-lang.org> * string.c (rb_str_inspect): escape as \x{XXXX} when the encoding is diff --git a/enumerator.c b/enumerator.c index e6af7d810..7b2ef3491 100644 --- a/enumerator.c +++ b/enumerator.c @@ -400,7 +400,8 @@ enumerator_initialize(int argc, VALUE *argv, VALUE obj) rb_raise(rb_eArgError, "wrong number of argument (0 for 1+)"); recv = generator_init(generator_allocate(rb_cGenerator), rb_block_proc()); - } else { + } + else { recv = *argv++; if (--argc) { meth = *argv++; @@ -445,6 +446,21 @@ rb_enumeratorize(VALUE obj, VALUE meth, int argc, VALUE *argv) return enumerator_init(enumerator_allocate(rb_cEnumerator), obj, meth, argc, argv); } +static VALUE +enumerator_block_call(VALUE obj, rb_block_call_func *func, VALUE arg) +{ + int argc = 0; + VALUE *argv = 0; + const struct enumerator *e = enumerator_ptr(obj); + ID meth = e->meth; + + if (e->args) { + argc = RARRAY_LENINT(e->args); + argv = RARRAY_PTR(e->args); + } + return rb_block_call(e->obj, meth, argc, argv, func, arg); +} + /* * call-seq: * enum.each {...} @@ -456,24 +472,15 @@ rb_enumeratorize(VALUE obj, VALUE meth, int argc, VALUE *argv) static VALUE enumerator_each(VALUE obj) { - struct enumerator *e; - int argc = 0; - VALUE *argv = 0; - if (!rb_block_given_p()) return obj; - e = enumerator_ptr(obj); - if (e->args) { - argc = RARRAY_LENINT(e->args); - argv = RARRAY_PTR(e->args); - } - return rb_block_call(e->obj, e->meth, argc, argv, - enumerator_each_i, (VALUE)e); + return enumerator_block_call(obj, enumerator_each_i, obj); } static VALUE -enumerator_with_index_i(VALUE val, VALUE *memo, int argc, VALUE *argv) +enumerator_with_index_i(VALUE val, VALUE m, int argc, VALUE *argv) { VALUE idx; + VALUE *memo = (VALUE *)m; idx = INT2FIX(*memo); ++*memo; @@ -496,23 +503,12 @@ enumerator_with_index_i(VALUE val, VALUE *memo, int argc, VALUE *argv) static VALUE enumerator_with_index(int argc, VALUE *argv, VALUE obj) { - struct enumerator *e; VALUE memo; rb_scan_args(argc, argv, "01", &memo); RETURN_ENUMERATOR(obj, argc, argv); memo = NIL_P(memo) ? 0 : (VALUE)NUM2LONG(memo); - e = enumerator_ptr(obj); - if (e->args) { - argc = RARRAY_LENINT(e->args); - argv = RARRAY_PTR(e->args); - } - else { - argc = 0; - argv = NULL; - } - return rb_block_call(e->obj, e->meth, argc, argv, - enumerator_with_index_i, (VALUE)&memo); + return enumerator_block_call(obj, enumerator_with_index_i, (VALUE)&memo); } /* @@ -553,18 +549,8 @@ enumerator_with_object_i(VALUE val, VALUE memo, int argc, VALUE *argv) static VALUE enumerator_with_object(VALUE obj, VALUE memo) { - struct enumerator *e; - int argc = 0; - VALUE *argv = 0; - RETURN_ENUMERATOR(obj, 1, &memo); - e = enumerator_ptr(obj); - if (e->args) { - argc = RARRAY_LENINT(e->args); - argv = RARRAY_PTR(e->args); - } - rb_block_call(e->obj, e->meth, argc, argv, - enumerator_with_object_i, memo); + enumerator_block_call(obj, enumerator_with_object_i, memo); return memo; } |