diff options
-rw-r--r-- | ChangeLog | 6 | ||||
-rw-r--r-- | range.c | 16 | ||||
-rw-r--r-- | test/ruby/test_range.rb | 10 |
3 files changed, 27 insertions, 5 deletions
@@ -1,3 +1,9 @@ +Wed Jan 30 14:46:28 2013 Marc-Andre Lafortune <ruby-core@marc-andre.ca> + + * range.c: Restrict bsearch to integers [#7728] + + * test/ruby/test_range.rb: Test for above + Wed Jan 30 14:10:52 2013 Marc-Andre Lafortune <ruby-core@marc-andre.ca> * array.c (rb_ary_bsearch): Return enumerator if no block [#7725] @@ -20,7 +20,7 @@ #include <math.h> VALUE rb_cRange; -static ID id_cmp, id_succ, id_beg, id_end, id_excl; +static ID id_cmp, id_succ, id_beg, id_end, id_excl, id_integer_p; #define RANGE_BEG(r) (RSTRUCT(r)->as.ary[0]) #define RANGE_END(r) (RSTRUCT(r)->as.ary[1]) @@ -497,6 +497,12 @@ double_as_int64(double d) { } #endif +static int +is_integer_p(VALUE v) { + VALUE is_int = rb_check_funcall(v, id_integer_p, 0, 0); + return RTEST(is_int) && is_int != Qundef; +} + /* * call-seq: * rng.bsearch {|obj| block } -> value @@ -635,10 +641,9 @@ range_bsearch(VALUE range) BSEARCH(int64_as_double_to_num); } #endif - else if (!NIL_P(rb_check_to_integer(beg, "to_int")) && - !NIL_P(rb_check_to_integer(end, "to_int"))) { - VALUE low = beg; - VALUE high = end; + else if (is_integer_p(beg) && is_integer_p(end)) { + VALUE low = rb_to_int(beg); + VALUE high = rb_to_int(end); VALUE mid, org_high; RETURN_ENUMERATOR(range, 0, 0); if (EXCL(range)) high = rb_funcall(high, '-', 1, INT2FIX(1)); @@ -1299,6 +1304,7 @@ Init_Range(void) id_beg = rb_intern("begin"); id_end = rb_intern("end"); id_excl = rb_intern("excl"); + id_integer_p = rb_intern("integer?"); rb_cRange = rb_struct_define_without_accessor( "Range", rb_cObject, range_alloc, diff --git a/test/ruby/test_range.rb b/test/ruby/test_range.rb index c8867cf2ec..d859969ded 100644 --- a/test/ruby/test_range.rb +++ b/test/ruby/test_range.rb @@ -1,6 +1,7 @@ require 'test/unit' require 'delegate' require 'timeout' +require 'bigdecimal' class TestRange < Test::Unit::TestCase def test_range_string @@ -362,6 +363,15 @@ class TestRange < Test::Unit::TestCase assert_equal 200, enum.each{|x| x >= 200 } end + def test_bsearch_for_other_numerics + assert_raise(TypeError) { + (Rational(-1,2)..Rational(9,4)).bsearch + } + assert_raise(TypeError) { + (BigDecimal('0.5')..BigDecimal('2.25')).bsearch + } + end + def test_bsearch_for_fixnum ary = [3, 4, 7, 9, 12] assert_equal(0, (0...ary.size).bsearch {|i| ary[i] >= 2 }) |