ruby-changes:26933
From: marcandre <ko1@a...>
Date: Wed, 30 Jan 2013 14:47:00 +0900 (JST)
Subject: [ruby-changes:26933] marcandRe: r38985 (trunk): * range.c: Restrict bsearch to integers [#7728]
marcandre 2013-01-30 14:46:48 +0900 (Wed, 30 Jan 2013) New Revision: 38985 http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=38985 Log: * range.c: Restrict bsearch to integers [#7728] * test/ruby/test_range.rb: Test for above Modified files: trunk/ChangeLog trunk/range.c trunk/test/ruby/test_range.rb Index: ChangeLog =================================================================== --- ChangeLog (revision 38984) +++ ChangeLog (revision 38985) @@ -1,3 +1,9 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1 +Wed Jan 30 14:46:28 2013 Marc-Andre Lafortune <ruby-core@m...> + + * 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@m...> * array.c (rb_ary_bsearch): Return enumerator if no block [#7725] Index: range.c =================================================================== --- range.c (revision 38984) +++ range.c (revision 38985) @@ -20,7 +20,7 @@ https://github.com/ruby/ruby/blob/trunk/range.c#L20 #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) { https://github.com/ruby/ruby/blob/trunk/range.c#L497 } #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) https://github.com/ruby/ruby/blob/trunk/range.c#L641 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) https://github.com/ruby/ruby/blob/trunk/range.c#L1304 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, Index: test/ruby/test_range.rb =================================================================== --- test/ruby/test_range.rb (revision 38984) +++ test/ruby/test_range.rb (revision 38985) @@ -1,6 +1,7 @@ https://github.com/ruby/ruby/blob/trunk/test/ruby/test_range.rb#L1 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 https://github.com/ruby/ruby/blob/trunk/test/ruby/test_range.rb#L363 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 }) -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/