From da32ce1a67677bd0f5df8fa31e0ab05c26c7d401 Mon Sep 17 00:00:00 2001 From: matz Date: Wed, 7 Dec 2005 08:41:59 +0000 Subject: [PATCH] * sprintf.c (rb_f_sprintf): [ruby-dev:27967] * range.c (range_include): use discrete membership for non Numeric values, for example, String. * numeric.c (num_scalar_p): new method. [ruby-dev:27936] * lib/complex.rb (Complex#scalar?): ditto. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@9654 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- ChangeLog | 13 +++++++++++++ lib/complex.rb | 4 ++++ numeric.c | 15 +++++++++++++++ range.c | 29 +++++++++++++++++++---------- sprintf.c | 12 ++++++------ 5 files changed, 57 insertions(+), 16 deletions(-) diff --git a/ChangeLog b/ChangeLog index 7ee9e04af6..c1c762b5b6 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,16 @@ +Wed Dec 7 17:10:27 2005 Kazuhiro NISHIYAMA + + * sprintf.c (rb_f_sprintf): [ruby-dev:27967] + +Wed Dec 7 16:39:18 2005 Yukihiro Matsumoto + + * range.c (range_include): use discrete membership for non Numeric + values, for example, String. + + * numeric.c (num_scalar_p): new method. [ruby-dev:27936] + + * lib/complex.rb (Complex#scalar?): ditto. + Wed Dec 7 15:31:35 2005 Yukihiro Matsumoto * sprintf.c (rb_str_format): integer overflow check added. diff --git a/lib/complex.rb b/lib/complex.rb index 110a28ee7e..8832f17f82 100644 --- a/lib/complex.rb +++ b/lib/complex.rb @@ -103,6 +103,10 @@ class Complex < Numeric undef step + def scalar? + false + end + def Complex.generic?(other) # :nodoc: other.kind_of?(Integer) or other.kind_of?(Float) or diff --git a/numeric.c b/numeric.c index 08685718a4..91e7dfc1e7 100644 --- a/numeric.c +++ b/numeric.c @@ -348,6 +348,20 @@ num_remainder(VALUE x, VALUE y) return z; } +/* + * call-seq: + * num.scalar? -> true or false + * + * Returns true if num is an Scalar + * (i.e. non Complex). + */ + +static VALUE +num_scalar_p(VALUE num) +{ + return Qtrue; +} + /* * call-seq: * num.integer? -> true or false @@ -2806,6 +2820,7 @@ Init_Numeric(void) rb_define_method(rb_cNumeric, "abs", num_abs, 0); rb_define_method(rb_cNumeric, "to_int", num_to_int, 0); + rb_define_method(rb_cNumeric, "scalar?", num_scalar_p, 0); rb_define_method(rb_cNumeric, "integer?", num_int_p, 0); rb_define_method(rb_cNumeric, "zero?", num_zero_p, 0); rb_define_method(rb_cNumeric, "nonzero?", num_nonzero_p, 0); diff --git a/range.c b/range.c index 12ea69daaa..d18b4efe01 100644 --- a/range.c +++ b/range.c @@ -618,19 +618,28 @@ range_inspect(VALUE range) static VALUE range_include(VALUE range, VALUE val) { - VALUE beg, end; + VALUE beg = rb_ivar_get(range, id_beg); + VALUE end = rb_ivar_get(range, id_end); + VALUE tmp; + int nv = FIXNUM_P(beg) || FIXNUM_P(end) || + rb_obj_is_kind_of(beg, rb_cNumeric) || + rb_obj_is_kind_of(end, rb_cNumeric); - beg = rb_ivar_get(range, id_beg); - end = rb_ivar_get(range, id_end); - if (r_le(beg, val)) { - if (EXCL(range)) { - if (r_lt(val, end)) return Qtrue; - } - else { - if (r_le(val, end)) return Qtrue; + if (nv) { + numeric_range: + if (r_le(beg, val)) { + if (EXCL(range)) { + if (r_lt(val, end)) return Qtrue; + } + else { + if (r_le(val, end)) return Qtrue; + } } } - return Qfalse; + if (!NIL_P(rb_check_to_integer(beg, "to_int")) || + !NIL_P(rb_check_to_integer(end, "to_int"))) + goto numeric_range; + return rb_call_super(1, &val); } diff --git a/sprintf.c b/sprintf.c index 07526756f2..e8c0d36164 100644 --- a/sprintf.c +++ b/sprintf.c @@ -116,11 +116,11 @@ sign_bits(int base, const char *p) t = p++; \ n = 0; \ for (; p < end && ISDIGIT(*p); p++) { \ - int times10 = n*10; \ - if (times10 / 10 != n) {\ + int next_n = 10 * n + (*p - '0'); \ + if (next_n / 10 != n) {\ rb_raise(rb_eArgError, #val " too big"); \ } \ - n = 10 * n + (*p - '0'); \ + n = next_n; \ } \ if (p >= end) { \ rb_raise(rb_eArgError, "malformed format string - %%*[0-9]"); \ @@ -320,11 +320,11 @@ rb_str_format(int argc, const VALUE *argv, VALUE fmt) case '5': case '6': case '7': case '8': case '9': n = 0; for (; p < end && ISDIGIT(*p); p++) { - int times10 = n*10; - if (times10 / 10 != n) { + int next_n = 10 * n + (*p - '0'); + if (next_n / 10 != n) { rb_raise(rb_eArgError, "width too big"); } - n = 10 * n + (*p - '0'); + n = next_n; } if (p >= end) { rb_raise(rb_eArgError, "malformed format string - %%[0-9]");