From 1072cb65c996e672b23878839fac27c47d722f73 Mon Sep 17 00:00:00 2001 From: nobu Date: Tue, 13 May 2008 14:32:46 +0000 Subject: * enum.c (enum_yield): use rb_yield_values2. * enum.c (DEFINE_ENUMFUNCS): macro to define enumerator and yielding functions. * enum.c (enum_all_func, enum_any_func, enum_one_func, enum_none_func): reduced duplicate code. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@16404 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- ChangeLog | 10 ++++++++ enum.c | 80 +++++++++++++++++++++------------------------------------------ 2 files changed, 36 insertions(+), 54 deletions(-) diff --git a/ChangeLog b/ChangeLog index fd4c27cfeb..f114e95fa3 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,13 @@ +Tue May 13 23:32:44 2008 Nobuyoshi Nakada + + * enum.c (enum_yield): use rb_yield_values2. + + * enum.c (DEFINE_ENUMFUNCS): macro to define enumerator and yielding + functions. + + * enum.c (enum_all_func, enum_any_func, enum_one_func, + enum_none_func): reduced duplicate code. + Tue May 13 15:09:38 2008 Akinori MUSHA * enumerator.c: Update rdoc. diff --git a/enum.c b/enum.c index a485a72c23..a7821e8b6d 100644 --- a/enum.c +++ b/enum.c @@ -28,11 +28,7 @@ enum_values_pack(int argc, VALUE *argv) i = enum_values_pack(argc, argv); \ } while (0) -static VALUE -enum_yield(int argc, VALUE *argv) -{ - return rb_yield(enum_values_pack(argc, argv)); -} +#define enum_yield rb_yield_values2 static VALUE grep_i(VALUE i, VALUE *arg, int argc, VALUE *argv) @@ -791,26 +787,31 @@ enum_sort_by(VALUE obj) return ary; } -static VALUE -all_i(VALUE i, VALUE *memo, int argc, VALUE *argv) -{ - if (!RTEST(enum_values_pack(argc, argv))) { - *memo = Qfalse; - rb_iter_break(); - } - return Qnil; +#define DEFINE_ENUMFUNCS(name) \ +static VALUE \ +name##_i(VALUE i, VALUE *memo, int argc, VALUE *argv) \ +{ \ + return enum_##name##_func(enum_values_pack(argc, argv), memo); \ +} \ +\ +static VALUE \ +name##_iter_i(VALUE i, VALUE *memo, int argc, VALUE *argv) \ +{ \ + return enum_##name##_func(enum_yield(argc, argv), memo); \ } - + static VALUE -all_iter_i(VALUE i, VALUE *memo, int argc, VALUE *argv) +enum_all_func(VALUE result, VALUE *memo) { - if (!RTEST(enum_yield(argc, argv))) { + if (!RTEST(result)) { *memo = Qfalse; rb_iter_break(); } return Qnil; } +DEFINE_ENUMFUNCS(all) + /* * call-seq: * enum.all? [{|obj| block } ] => true or false @@ -838,24 +839,16 @@ enum_all(VALUE obj) } static VALUE -any_i(VALUE i, VALUE *memo, int argc, VALUE *argv) +enum_any_func(VALUE result, VALUE *memo) { - if (RTEST(enum_values_pack(argc, argv))) { + if (RTEST(result)) { *memo = Qtrue; rb_iter_break(); } return Qnil; } -static VALUE -any_iter_i(VALUE i, VALUE *memo, int argc, VALUE *argv) -{ - if (RTEST(enum_yield(argc, argv))) { - *memo = Qtrue; - rb_iter_break(); - } - return Qnil; -} +DEFINE_ENUMFUNCS(any) /* * call-seq: @@ -885,9 +878,9 @@ enum_any(VALUE obj) } static VALUE -one_i(VALUE i, VALUE *memo, int argc, VALUE *argv) +enum_one_func(VALUE result, VALUE *memo) { - if (RTEST(enum_values_pack(argc, argv))) { + if (RTEST(result)) { if (*memo == Qundef) { *memo = Qtrue; } @@ -899,20 +892,7 @@ one_i(VALUE i, VALUE *memo, int argc, VALUE *argv) return Qnil; } -static VALUE -one_iter_i(VALUE i, VALUE *memo, int argc, VALUE *argv) -{ - if (RTEST(enum_yield(argc, argv))) { - if (*memo == Qundef) { - *memo = Qtrue; - } - else if (*memo == Qtrue) { - *memo = Qfalse; - rb_iter_break(); - } - } - return Qnil; -} +DEFINE_ENUMFUNCS(one) /* * call-seq: @@ -943,24 +923,16 @@ enum_one(VALUE obj) } static VALUE -none_i(VALUE i, VALUE *memo, int argc, VALUE *argv) +enum_none_func(VALUE result, VALUE *memo) { - if (RTEST(enum_values_pack(argc, argv))) { + if (RTEST(result)) { *memo = Qfalse; rb_iter_break(); } return Qnil; } -static VALUE -none_iter_i(VALUE i, VALUE *memo, int argc, VALUE *argv) -{ - if (RTEST(enum_yield(argc, argv))) { - *memo = Qfalse; - rb_iter_break(); - } - return Qnil; -} +DEFINE_ENUMFUNCS(none) /* * call-seq: -- cgit v1.2.3