summaryrefslogtreecommitdiff
path: root/numeric.c
diff options
context:
space:
mode:
authornagachika <nagachika@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2014-07-02 16:27:21 +0000
committernagachika <nagachika@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2014-07-02 16:27:21 +0000
commit5ab69337816b10557e5a1658a5fa980c790a4b48 (patch)
tree0bc3d510dea742f149473015c547d8957184bc6d /numeric.c
parent428a637f0f7feffd33fed9a984844c6b0d167758 (diff)
merge revision(s) r45858,r45859,r45860,r45861,r46638: [Backport #9811]
numeric.c: indent * numeric.c (ruby_num_interval_step_size): adjust indent. * numeric.c (num_step_scan_args): check keyword arguments and fail if they conflict with positional arguments. [ruby-dev:48177] [Bug #9811] * numeric.c (num_step_scan_args): table argument of rb_get_kwargs() is array of IDs, not Symbols. [ruby-dev:48353] [Bug #9811] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/branches/ruby_2_1@46661 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'numeric.c')
-rw-r--r--numeric.c82
1 files changed, 49 insertions, 33 deletions
diff --git a/numeric.c b/numeric.c
index bd538798b2..4b13198797 100644
--- a/numeric.c
+++ b/numeric.c
@@ -119,7 +119,7 @@ VALUE rb_cFixnum;
VALUE rb_eZeroDivError;
VALUE rb_eFloatDomainError;
-static VALUE sym_to, sym_by;
+static ID id_to, id_by;
void
rb_num_zerodiv(void)
@@ -1841,8 +1841,8 @@ ruby_num_interval_step_size(VALUE from, VALUE to, VALUE step, int excl)
VALUE result;
ID cmp = '>';
switch (rb_cmpint(rb_num_coerce_cmp(step, INT2FIX(0), id_cmp), step, INT2FIX(0))) {
- case 0: return DBL2NUM(INFINITY);
- case -1: cmp = '<'; break;
+ case 0: return DBL2NUM(INFINITY);
+ case -1: cmp = '<'; break;
}
if (RTEST(rb_funcall(from, cmp, 1, to))) return INT2FIX(0);
result = rb_funcall(rb_funcall(to, '-', 1, from), id_div, 1, step);
@@ -1853,39 +1853,55 @@ ruby_num_interval_step_size(VALUE from, VALUE to, VALUE step, int excl)
}
}
-#define NUM_STEP_SCAN_ARGS(argc, argv, to, step, hash, desc) do { \
- argc = rb_scan_args(argc, argv, "02:", &to, &step, &hash); \
- if (!NIL_P(hash)) { \
- step = rb_hash_aref(hash, sym_by); \
- to = rb_hash_aref(hash, sym_to); \
- } \
- else { \
- /* compatibility */ \
- if (argc > 1 && NIL_P(step)) { \
- rb_raise(rb_eTypeError, "step must be numeric"); \
- } \
- if (rb_equal(step, INT2FIX(0))) { \
- rb_raise(rb_eArgError, "step can't be 0"); \
- } \
- } \
- if (NIL_P(step)) { \
- step = INT2FIX(1); \
- } \
- desc = !positive_int_p(step); \
- if (NIL_P(to)) { \
- to = desc ? DBL2NUM(-INFINITY) : DBL2NUM(INFINITY); \
- } \
-} while (0)
+static int
+num_step_scan_args(int argc, const VALUE *argv, VALUE *to, VALUE *step)
+{
+ VALUE hash;
+ int desc;
+
+ argc = rb_scan_args(argc, argv, "02:", to, step, &hash);
+ if (!NIL_P(hash)) {
+ ID keys[2];
+ VALUE values[2];
+ keys[0] = id_to;
+ keys[1] = id_by;
+ rb_get_kwargs(hash, keys, 0, 2, values);
+ if (values[0] != Qundef) {
+ if (argc > 0) rb_raise(rb_eArgError, "to is given twice");
+ *to = values[0];
+ }
+ if (values[1] != Qundef) {
+ if (argc > 1) rb_raise(rb_eArgError, "step is given twice");
+ *step = values[1];
+ }
+ }
+ else {
+ /* compatibility */
+ if (argc > 1 && NIL_P(*step)) {
+ rb_raise(rb_eTypeError, "step must be numeric");
+ }
+ if (rb_equal(*step, INT2FIX(0))) {
+ rb_raise(rb_eArgError, "step can't be 0");
+ }
+ }
+ if (NIL_P(*step)) {
+ *step = INT2FIX(1);
+ }
+ desc = !positive_int_p(*step);
+ if (NIL_P(*to)) {
+ *to = desc ? DBL2NUM(-INFINITY) : DBL2NUM(INFINITY);
+ }
+ return desc;
+}
static VALUE
num_step_size(VALUE from, VALUE args, VALUE eobj)
{
- VALUE to, step, hash;
- int desc;
+ VALUE to, step;
int argc = args ? RARRAY_LENINT(args) : 0;
VALUE *argv = args ? RARRAY_PTR(args) : 0;
- NUM_STEP_SCAN_ARGS(argc, argv, to, step, hash, desc);
+ num_step_scan_args(argc, argv, &to, &step);
return ruby_num_interval_step_size(from, to, step, FALSE);
}
@@ -1946,12 +1962,12 @@ num_step_size(VALUE from, VALUE args, VALUE eobj)
static VALUE
num_step(int argc, VALUE *argv, VALUE from)
{
- VALUE to, step, hash;
+ VALUE to, step;
int desc, inf;
RETURN_SIZED_ENUMERATOR(from, argc, argv, num_step_size);
- NUM_STEP_SCAN_ARGS(argc, argv, to, step, hash, desc);
+ desc = num_step_scan_args(argc, argv, &to, &step);
if (RTEST(rb_num_coerce_cmp(step, INT2FIX(0), id_eq))) {
inf = 1;
}
@@ -4085,8 +4101,8 @@ Init_Numeric(void)
rb_define_method(rb_cFloat, "infinite?", flo_is_infinite_p, 0);
rb_define_method(rb_cFloat, "finite?", flo_is_finite_p, 0);
- sym_to = ID2SYM(rb_intern("to"));
- sym_by = ID2SYM(rb_intern("by"));
+ id_to = rb_intern("to");
+ id_by = rb_intern("by");
}
#undef rb_float_value