From eab07daf92c9a8a2836923656d9e8f58583a52ba Mon Sep 17 00:00:00 2001 From: Ryan Lopopolo Date: Tue, 9 Jul 2019 01:41:10 -0700 Subject: Add Range#max and Range#min tests from Ruby Spec --- mrbgems/mruby-range-ext/test/range.rb | 101 ++++++++++++++++++++++++++++++++++ 1 file changed, 101 insertions(+) (limited to 'mrbgems/mruby-range-ext') diff --git a/mrbgems/mruby-range-ext/test/range.rb b/mrbgems/mruby-range-ext/test/range.rb index efcbdabe4..6b135aeff 100644 --- a/mrbgems/mruby-range-ext/test/range.rb +++ b/mrbgems/mruby-range-ext/test/range.rb @@ -30,3 +30,104 @@ assert('Range#size') do assert_equal Float::INFINITY, (0..Float::INFINITY).size assert_nil ('a'..'z').size end + +assert('Range#max') do + # returns the maximum value in the range when called with no arguments + assert_equal 10, (1..10).max + assert_equal 9, (1...10).max + assert_equal 4294967295, (0...2**32).max + + # returns the maximum value in the Float range when called with no arguments + assert_equal 908.1111, (303.20..908.1111).max + + # raises TypeError when called on an exclusive range and a non Integer value + assert_raise(TypeError) { (303.20...908.1111).max } + + # returns nil when the endpoint is less than the start point + assert_equal nil, (100..10).max + + # returns nil when the endpoint equals the start point and the range is exclusive + assert_equal nil, (5...5).max + + # returns the endpoint when the endpoint equals the start point and the range is inclusive + assert_equal 5, (5..5).max + + # returns nil when the endpoint is less than the start point in a Float range + assert_equal nil, (3003.20..908.1111).max +end + +assert('Range#max given a block') do + # passes each pair of values in the range to the block + acc = [] + (1..10).max do |a, b| + acc << a + acc << b + a + end + (1..10).each do |value| + assert_true acc.include?(value) + end + + # passes each pair of elements to the block in reversed order + acc = [] + (1..5).max do |a, b| + acc << [a, b] + a + end + assert_equal [[2, 1], [3, 2], [4, 3], [5, 4]], acc + + # returns the element the block determines to be the maximum + assert_equal 1, ((1..3).max { |_a, _b| -3 }) + + # returns nil when the endpoint is less than the start point + assert_equal nil, ((100..10).max { |x, y| x <=> y }) + assert_equal nil, ((5...5).max { |x, y| x <=> y }) +end + +assert('Range#min') do + # returns the minimum value in the range when called with no arguments + assert_equal 1, (1..10).min + + # returns the minimum value in the Float range when called with no arguments + assert_equal 303.20, (303.20..908.1111).min + + # returns nil when the start point is greater than the endpoint + assert_equal nil, (100..10).min + + # returns nil when the endpoint equals the start point and the range is exclusive + assert_equal nil, (5...5).max + + # returns the endpoint when the endpoint equals the start point and the range is inclusive + assert_equal 5, (5..5).max + + # returns nil when the start point is greater than the endpoint in a Float range + assert_equal nil, (3003.20..908.1111).max +end + +assert('Range#min given a block') do + # passes each pair of values in the range to the block + acc = [] + (1..10).min do |a, b| + acc << a + acc << b + a + end + (1..10).each do |value| + assert_true acc.include?(value) + end + + # passes each pair of elements to the block in reversed order + acc = [] + (1..5).min do |a, b| + acc << [a, b] + a + end + assert_equal [[2, 1], [3, 1], [4, 1], [5, 1]], acc + + # returns the element the block determines to be the minimum + assert_equal 3, ((1..3).min { |_a, _b| -3 }) + + # returns nil when the start point is greater than the endpoint + assert_equal nil, ((100..10).min { |x, y| x <=> y }) + assert_equal nil, ((5...5).min { |x, y| x <=> y }) +end -- cgit v1.2.3 From 0ad5ba7a0f819cff87460d9b6f5691656ea75ade Mon Sep 17 00:00:00 2001 From: Ryan Lopopolo Date: Tue, 9 Jul 2019 01:42:40 -0700 Subject: Add a fast path for Float and Fixnum ranges for Range#max and Range#min If no block is given and the Range has Fixnum or Float endpoints, do not iterate with each and instead compare the endpoints directly. This implementation passes all of the applicable specs from Ruby Spec. --- mrbgems/mruby-range-ext/mrblib/range.rb | 40 +++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) (limited to 'mrbgems/mruby-range-ext') diff --git a/mrbgems/mruby-range-ext/mrblib/range.rb b/mrbgems/mruby-range-ext/mrblib/range.rb index de7925ba7..e09b2d096 100644 --- a/mrbgems/mruby-range-ext/mrblib/range.rb +++ b/mrbgems/mruby-range-ext/mrblib/range.rb @@ -25,4 +25,44 @@ class Range end ary end + + def max(&block) + val = self.first + last = self.last + return super if block + + # fast path for numerics + if (val.kind_of?(Fixnum) || val.kind_of?(Float)) && (last.kind_of?(Fixnum) || last.kind_of?(Float)) + raise TypeError if exclude_end? && !last.kind_of?(Fixnum) + return nil if val > last + return nil if val == last && exclude_end? + + max = last + max -= 1 if exclude_end? + return max + end + + # delegate to Enumerable + super + end + + def min(&block) + val = self.first + last = self.last + return super if block + + # fast path for numerics + if (val.kind_of?(Fixnum) || val.kind_of?(Float)) && (last.kind_of?(Fixnum) || last.kind_of?(Float)) + raise TypeError if exclude_end? && !last.kind_of?(Fixnum) + return nil if val > last + return nil if val == last && exclude_end? + + min = val + min -= 1 if exclude_end? + return min + end + + # delegate to Enumerable + super + end end -- cgit v1.2.3 From 56929362f58ba5ad3ebe4131a6cc4259e6479dc0 Mon Sep 17 00:00:00 2001 From: Ryan Lopopolo Date: Tue, 9 Jul 2019 01:48:15 -0700 Subject: Fix Range#min fast path with exclusive range --- mrbgems/mruby-range-ext/mrblib/range.rb | 1 - mrbgems/mruby-range-ext/test/range.rb | 1 + 2 files changed, 1 insertion(+), 1 deletion(-) (limited to 'mrbgems/mruby-range-ext') diff --git a/mrbgems/mruby-range-ext/mrblib/range.rb b/mrbgems/mruby-range-ext/mrblib/range.rb index e09b2d096..a149a57dc 100644 --- a/mrbgems/mruby-range-ext/mrblib/range.rb +++ b/mrbgems/mruby-range-ext/mrblib/range.rb @@ -58,7 +58,6 @@ class Range return nil if val == last && exclude_end? min = val - min -= 1 if exclude_end? return min end diff --git a/mrbgems/mruby-range-ext/test/range.rb b/mrbgems/mruby-range-ext/test/range.rb index 6b135aeff..b56d6b58e 100644 --- a/mrbgems/mruby-range-ext/test/range.rb +++ b/mrbgems/mruby-range-ext/test/range.rb @@ -87,6 +87,7 @@ end assert('Range#min') do # returns the minimum value in the range when called with no arguments assert_equal 1, (1..10).min + assert_equal 1, (1...10).min # returns the minimum value in the Float range when called with no arguments assert_equal 303.20, (303.20..908.1111).min -- cgit v1.2.3 From c96b517ea28efe7f45a91873142d6449b2a4c59c Mon Sep 17 00:00:00 2001 From: dearblue Date: Sun, 14 Jul 2019 00:29:17 +0900 Subject: Error needed/conflicts configuration The purpose is to clarify the error if there is a needed/conflicts configuration at compile time. --- mrbgems/mruby-bin-debugger/tools/mrdb/mrdbconf.h | 4 ++++ mrbgems/mruby-math/src/math.c | 4 ++++ mrbgems/mruby-random/src/random.c | 4 ++++ mrbgems/mruby-range-ext/src/range.c | 4 ++++ mrbgems/mruby-time/src/time.c | 4 ++++ 5 files changed, 20 insertions(+) (limited to 'mrbgems/mruby-range-ext') diff --git a/mrbgems/mruby-bin-debugger/tools/mrdb/mrdbconf.h b/mrbgems/mruby-bin-debugger/tools/mrdb/mrdbconf.h index f17f9c57d..de2f90144 100644 --- a/mrbgems/mruby-bin-debugger/tools/mrdb/mrdbconf.h +++ b/mrbgems/mruby-bin-debugger/tools/mrdb/mrdbconf.h @@ -6,6 +6,10 @@ #ifndef MRDBCONF_H #define MRDBCONF_H +#ifndef MRB_ENABLE_DEBUG_HOOK +# error Need 'MRB_ENABLE_DEBUG_HOOK' configuration in your 'build_config.rb' +#endif + /* configuration options: */ /* maximum size for command buffer */ #define MAX_COMMAND_LINE 1024 diff --git a/mrbgems/mruby-math/src/math.c b/mrbgems/mruby-math/src/math.c index caa16b789..35fcd0fa6 100644 --- a/mrbgems/mruby-math/src/math.c +++ b/mrbgems/mruby-math/src/math.c @@ -4,6 +4,10 @@ ** See Copyright Notice in mruby.h */ +#ifdef MRB_WITHOUT_FLOAT +# error Conflict 'MRB_WITHOUT_FLOAT' configuration in your 'build_config.rb' +#endif + #include #include diff --git a/mrbgems/mruby-random/src/random.c b/mrbgems/mruby-random/src/random.c index 68209840a..99f2b02e4 100644 --- a/mrbgems/mruby-random/src/random.c +++ b/mrbgems/mruby-random/src/random.c @@ -4,6 +4,10 @@ ** See Copyright Notice in mruby.h */ +#ifdef MRB_WITHOUT_FLOAT +# error Conflict 'MRB_WITHOUT_FLOAT' configuration in your 'build_config.rb' +#endif + #include #include #include diff --git a/mrbgems/mruby-range-ext/src/range.c b/mrbgems/mruby-range-ext/src/range.c index 1f6690904..fb76fe0d8 100644 --- a/mrbgems/mruby-range-ext/src/range.c +++ b/mrbgems/mruby-range-ext/src/range.c @@ -1,3 +1,7 @@ +#ifdef MRB_WITHOUT_FLOAT +# error Conflict 'MRB_WITHOUT_FLOAT' configuration in your 'build_config.rb' +#endif + #include #include #include diff --git a/mrbgems/mruby-time/src/time.c b/mrbgems/mruby-time/src/time.c index 34376c286..4f0afd6c6 100644 --- a/mrbgems/mruby-time/src/time.c +++ b/mrbgems/mruby-time/src/time.c @@ -4,6 +4,10 @@ ** See Copyright Notice in mruby.h */ +#ifdef MRB_WITHOUT_FLOAT +# error Conflict 'MRB_WITHOUT_FLOAT' configuration in your 'build_config.rb' +#endif + #include #include #include -- cgit v1.2.3 From 2add8641648e00717c97b730be8762aece80029e Mon Sep 17 00:00:00 2001 From: "Yukihiro \"Matz\" Matsumoto" Date: Tue, 16 Jul 2019 12:39:30 +0900 Subject: Allow `mruby-range-ext` to work with `MRB_WITHOUT_FLOAT`; ref #4576 --- mrbgems/mruby-range-ext/src/range.c | 27 +++++++++++++++++++++++---- 1 file changed, 23 insertions(+), 4 deletions(-) (limited to 'mrbgems/mruby-range-ext') diff --git a/mrbgems/mruby-range-ext/src/range.c b/mrbgems/mruby-range-ext/src/range.c index fb76fe0d8..b8c76b796 100644 --- a/mrbgems/mruby-range-ext/src/range.c +++ b/mrbgems/mruby-range-ext/src/range.c @@ -1,7 +1,3 @@ -#ifdef MRB_WITHOUT_FLOAT -# error Conflict 'MRB_WITHOUT_FLOAT' configuration in your 'build_config.rb' -#endif - #include #include #include @@ -110,6 +106,7 @@ range_last(mrb_state *mrb, mrb_value range) * ('a'..'z').size #=> nil */ +#ifndef MRB_WITHOUT_FLOAT static mrb_value range_size(mrb_state *mrb, mrb_value range) { @@ -162,6 +159,28 @@ range_size(mrb_state *mrb, mrb_value range) } return mrb_nil_value(); } +#else +static mrb_value +range_size(mrb_state *mrb, mrb_value range) +{ + struct RRange *r = mrb_range_ptr(mrb, range); + mrb_value beg, end; + mrb_int excl; + + beg = RANGE_BEG(r); + end = RANGE_END(r); + excl = RANGE_EXCL(r) ? 0 : 1; + + if (mrb_fixnum_p(beg) && mrb_fixnum_p(end)) { + mrb_int a = mrb_fixnum(beg); + mrb_int b = mrb_fixnum(end); + mrb_int c = b - a + excl; + + return mrb_fixnum_value(c); + } + return mrb_nil_value(); +} +#endif /* MRB_WITHOUT_FLOAT */ void mrb_mruby_range_ext_gem_init(mrb_state* mrb) -- cgit v1.2.3 From 8cb37804c16b1506d019081bb5d366b078b0b42a Mon Sep 17 00:00:00 2001 From: KOBAYASHI Shuji Date: Wed, 17 Jul 2019 18:58:21 +0900 Subject: Allow `mruby-range-ext` to work with `MRB_WITHOUT_FLOAT`; ref 2add8641 --- mrbgems/mruby-range-ext/mrblib/range.rb | 4 ++-- mrbgems/mruby-range-ext/test/range.rb | 28 ++++++++++++++++++---------- 2 files changed, 20 insertions(+), 12 deletions(-) (limited to 'mrbgems/mruby-range-ext') diff --git a/mrbgems/mruby-range-ext/mrblib/range.rb b/mrbgems/mruby-range-ext/mrblib/range.rb index a149a57dc..7c5787fac 100644 --- a/mrbgems/mruby-range-ext/mrblib/range.rb +++ b/mrbgems/mruby-range-ext/mrblib/range.rb @@ -32,7 +32,7 @@ class Range return super if block # fast path for numerics - if (val.kind_of?(Fixnum) || val.kind_of?(Float)) && (last.kind_of?(Fixnum) || last.kind_of?(Float)) + if val.kind_of?(Numeric) && last.kind_of?(Numeric) raise TypeError if exclude_end? && !last.kind_of?(Fixnum) return nil if val > last return nil if val == last && exclude_end? @@ -52,7 +52,7 @@ class Range return super if block # fast path for numerics - if (val.kind_of?(Fixnum) || val.kind_of?(Float)) && (last.kind_of?(Fixnum) || last.kind_of?(Float)) + if val.kind_of?(Numeric) && last.kind_of?(Numeric) raise TypeError if exclude_end? && !last.kind_of?(Fixnum) return nil if val > last return nil if val == last && exclude_end? diff --git a/mrbgems/mruby-range-ext/test/range.rb b/mrbgems/mruby-range-ext/test/range.rb index b56d6b58e..e2c549d04 100644 --- a/mrbgems/mruby-range-ext/test/range.rb +++ b/mrbgems/mruby-range-ext/test/range.rb @@ -10,6 +10,8 @@ end assert('Range#first') do assert_equal 10, (10..20).first assert_equal [10, 11, 12], (10..20).first(3) + + skip unless Object.const_defined?(:Float) assert_equal [0, 1, 2], (0..Float::INFINITY).first(3) end @@ -23,12 +25,14 @@ end assert('Range#size') do assert_equal 42, (1..42).size assert_equal 41, (1...42).size + assert_nil ('a'..'z').size + + skip unless Object.const_defined?(:Float) assert_equal 6, (1...6.3).size assert_equal 5, (1...6.0).size assert_equal 5, (1.1...6).size assert_equal 15, (1.0..15.9).size assert_equal Float::INFINITY, (0..Float::INFINITY).size - assert_nil ('a'..'z').size end assert('Range#max') do @@ -37,12 +41,6 @@ assert('Range#max') do assert_equal 9, (1...10).max assert_equal 4294967295, (0...2**32).max - # returns the maximum value in the Float range when called with no arguments - assert_equal 908.1111, (303.20..908.1111).max - - # raises TypeError when called on an exclusive range and a non Integer value - assert_raise(TypeError) { (303.20...908.1111).max } - # returns nil when the endpoint is less than the start point assert_equal nil, (100..10).max @@ -52,6 +50,14 @@ assert('Range#max') do # returns the endpoint when the endpoint equals the start point and the range is inclusive assert_equal 5, (5..5).max + skip unless Object.const_defined?(:Float) + + # returns the maximum value in the Float range when called with no arguments + assert_equal 908.1111, (303.20..908.1111).max + + # raises TypeError when called on an exclusive range and a non Integer value + assert_raise(TypeError) { (303.20...908.1111).max } + # returns nil when the endpoint is less than the start point in a Float range assert_equal nil, (3003.20..908.1111).max end @@ -89,9 +95,6 @@ assert('Range#min') do assert_equal 1, (1..10).min assert_equal 1, (1...10).min - # returns the minimum value in the Float range when called with no arguments - assert_equal 303.20, (303.20..908.1111).min - # returns nil when the start point is greater than the endpoint assert_equal nil, (100..10).min @@ -101,6 +104,11 @@ assert('Range#min') do # returns the endpoint when the endpoint equals the start point and the range is inclusive assert_equal 5, (5..5).max + skip unless Object.const_defined?(:Float) + + # returns the minimum value in the Float range when called with no arguments + assert_equal 303.20, (303.20..908.1111).min + # returns nil when the start point is greater than the endpoint in a Float range assert_equal nil, (3003.20..908.1111).max end -- cgit v1.2.3 From 1b7516a4ae144bfdc36356294e93b8af9c4a0a3f Mon Sep 17 00:00:00 2001 From: KOBAYASHI Shuji Date: Wed, 17 Jul 2019 19:16:44 +0900 Subject: Fix `(fixnum_value ... float_value).min` with `mruby-range-ext` Before this patch: $ bin/mruby -e 'p (2...4.0).min' #=> TypeError After this patch (same as CRuby and without `mruby-range-ext`): $ bin/mruby -e 'p (2...4.0).min' #=> 2 --- mrbgems/mruby-range-ext/mrblib/range.rb | 1 - 1 file changed, 1 deletion(-) (limited to 'mrbgems/mruby-range-ext') diff --git a/mrbgems/mruby-range-ext/mrblib/range.rb b/mrbgems/mruby-range-ext/mrblib/range.rb index a149a57dc..da2fff67a 100644 --- a/mrbgems/mruby-range-ext/mrblib/range.rb +++ b/mrbgems/mruby-range-ext/mrblib/range.rb @@ -53,7 +53,6 @@ class Range # fast path for numerics if (val.kind_of?(Fixnum) || val.kind_of?(Float)) && (last.kind_of?(Fixnum) || last.kind_of?(Float)) - raise TypeError if exclude_end? && !last.kind_of?(Fixnum) return nil if val > last return nil if val == last && exclude_end? -- cgit v1.2.3 From 13f58f9bc2bcff38cd88b7c59c366f8a97d60a08 Mon Sep 17 00:00:00 2001 From: KOBAYASHI Shuji Date: Sat, 24 Aug 2019 14:22:10 +0900 Subject: Fix `Range#max` test (`TypeError` is raised) on 32-bit mode --- mrbgems/mruby-range-ext/test/range.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'mrbgems/mruby-range-ext') diff --git a/mrbgems/mruby-range-ext/test/range.rb b/mrbgems/mruby-range-ext/test/range.rb index e2c549d04..8179cef53 100644 --- a/mrbgems/mruby-range-ext/test/range.rb +++ b/mrbgems/mruby-range-ext/test/range.rb @@ -39,7 +39,7 @@ assert('Range#max') do # returns the maximum value in the range when called with no arguments assert_equal 10, (1..10).max assert_equal 9, (1...10).max - assert_equal 4294967295, (0...2**32).max + assert_equal 1073741823, (0...2**30).max # returns nil when the endpoint is less than the start point assert_equal nil, (100..10).max -- cgit v1.2.3 From 6cfcff6f10c2f3274b36c0025fb807468e79eef2 Mon Sep 17 00:00:00 2001 From: KOBAYASHI Shuji Date: Thu, 5 Sep 2019 19:51:32 +0900 Subject: Fix `Range#max` test (`TypeError` is raised) on 32-bit mode with word boxing --- mrbgems/mruby-range-ext/test/range.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'mrbgems/mruby-range-ext') diff --git a/mrbgems/mruby-range-ext/test/range.rb b/mrbgems/mruby-range-ext/test/range.rb index 8179cef53..865e46d02 100644 --- a/mrbgems/mruby-range-ext/test/range.rb +++ b/mrbgems/mruby-range-ext/test/range.rb @@ -39,7 +39,7 @@ assert('Range#max') do # returns the maximum value in the range when called with no arguments assert_equal 10, (1..10).max assert_equal 9, (1...10).max - assert_equal 1073741823, (0...2**30).max + assert_equal 536870911, (0...2**29).max # returns nil when the endpoint is less than the start point assert_equal nil, (100..10).max -- cgit v1.2.3 From 76f1aa7de5cf449c1f75188a2a6bc9953133fa91 Mon Sep 17 00:00:00 2001 From: "Yukihiro \"Matz\" Matsumoto" Date: Sat, 14 Sep 2019 09:01:08 +0900 Subject: Remove `mrb_funcall` from `<=>` operations. --- include/mruby.h | 2 ++ mrbgems/mruby-range-ext/src/range.c | 14 +++----------- src/numeric.c | 21 +++++++++++++++++++++ src/range.c | 30 ++++++++---------------------- 4 files changed, 34 insertions(+), 33 deletions(-) (limited to 'mrbgems/mruby-range-ext') diff --git a/include/mruby.h b/include/mruby.h index 52c233211..c50054b79 100644 --- a/include/mruby.h +++ b/include/mruby.h @@ -1104,6 +1104,8 @@ MRB_API mrb_value mrb_Float(mrb_state *mrb, mrb_value val); #endif MRB_API mrb_value mrb_inspect(mrb_state *mrb, mrb_value obj); MRB_API mrb_bool mrb_eql(mrb_state *mrb, mrb_value obj1, mrb_value obj2); +/* mrb_cmp(mrb, obj1, obj2): 1:0:-1; -2 for error */ +MRB_API mrb_int mrb_cmp(mrb_state *mrb, mrb_value obj1, mrb_value obj2); MRB_INLINE int mrb_gc_arena_save(mrb_state *mrb) diff --git a/mrbgems/mruby-range-ext/src/range.c b/mrbgems/mruby-range-ext/src/range.c index b8c76b796..36b684fad 100644 --- a/mrbgems/mruby-range-ext/src/range.c +++ b/mrbgems/mruby-range-ext/src/range.c @@ -5,24 +5,16 @@ static mrb_bool r_le(mrb_state *mrb, mrb_value a, mrb_value b) { - mrb_value r = mrb_funcall(mrb, a, "<=>", 1, b); /* compare result */ - /* output :a < b => -1, a = b => 0, a > b => +1 */ - - if (mrb_fixnum_p(r)) { - mrb_int c = mrb_fixnum(r); - if (c == 0 || c == -1) return TRUE; - } + mrb_int n = mrb_cmp(mrb, a, b); + if (n == 0 || n == -1) return TRUE; return FALSE; } static mrb_bool r_lt(mrb_state *mrb, mrb_value a, mrb_value b) { - mrb_value r = mrb_funcall(mrb, a, "<=>", 1, b); - /* output :a < b => -1, a = b => 0, a > b => +1 */ - - return mrb_fixnum_p(r) && mrb_fixnum(r) == -1; + return mrb_cmp(mrb, a, b) == -1; } /* diff --git a/src/numeric.c b/src/numeric.c index 638f75fd8..d4ada1809 100644 --- a/src/numeric.c +++ b/src/numeric.c @@ -1553,6 +1553,27 @@ integral_ge(mrb_state *mrb, mrb_value self) return mrb_false_value(); } +MRB_API mrb_int +mrb_cmp(mrb_state *mrb, mrb_value obj1, mrb_value obj2) +{ + mrb_value v; + + switch (mrb_type(obj1)) { + case MRB_TT_FIXNUM: + case MRB_TT_FLOAT: + return cmpnum(mrb, obj1, obj2); + case MRB_TT_STRING: + if (mrb_type(obj2) != MRB_TT_STRING) + return -2; + return mrb_str_cmp(mrb, obj1, obj2); + default: + v = mrb_funcall(mrb, obj1, "<=>", 1, obj2); + if (mrb_nil_p(v) || !mrb_fixnum_p(v)) + return -2; + return mrb_fixnum(v); + } +} + static mrb_value num_finite_p(mrb_state *mrb, mrb_value self) { diff --git a/src/range.c b/src/range.c index 97fdcdb97..181e1e243 100644 --- a/src/range.c +++ b/src/range.c @@ -17,9 +17,9 @@ static void r_check(mrb_state *mrb, mrb_value a, mrb_value b) { - mrb_value ans; enum mrb_vtype ta; enum mrb_vtype tb; + mrb_int n; ta = mrb_type(a); tb = mrb_type(b); @@ -32,9 +32,8 @@ r_check(mrb_state *mrb, mrb_value a, mrb_value b) return; } - ans = mrb_funcall(mrb, a, "<=>", 1, b); - if (mrb_nil_p(ans)) { - /* can not be compared */ + n = mrb_cmp(mrb, a, b); + if (n == -2) { /* can not be compared */ mrb_raise(mrb, E_ARGUMENT_ERROR, "bad value for range"); } } @@ -42,37 +41,24 @@ r_check(mrb_state *mrb, mrb_value a, mrb_value b) static mrb_bool r_le(mrb_state *mrb, mrb_value a, mrb_value b) { - mrb_value r = mrb_funcall(mrb, a, "<=>", 1, b); /* compare result */ - /* output :a < b => -1, a = b => 0, a > b => +1 */ - - if (mrb_fixnum_p(r)) { - mrb_int c = mrb_fixnum(r); - if (c == 0 || c == -1) return TRUE; - } + mrb_int n = mrb_cmp(mrb, a, b); + if (n == 0 || n == -1) return TRUE; return FALSE; } static mrb_bool r_gt(mrb_state *mrb, mrb_value a, mrb_value b) { - mrb_value r = mrb_funcall(mrb, a, "<=>", 1, b); - /* output :a < b => -1, a = b => 0, a > b => +1 */ - - return mrb_fixnum_p(r) && mrb_fixnum(r) == 1; + return mrb_cmp(mrb, a, b) == 1; } static mrb_bool r_ge(mrb_state *mrb, mrb_value a, mrb_value b) { - mrb_value r = mrb_funcall(mrb, a, "<=>", 1, b); /* compare result */ - /* output :a < b => -1, a = b => 0, a > b => +1 */ - - if (mrb_fixnum_p(r)) { - mrb_int c = mrb_fixnum(r); - if (c == 0 || c == 1) return TRUE; - } + mrb_int n = mrb_cmp(mrb, a, b); + if (n == 0 || n == 1) return TRUE; return FALSE; } -- cgit v1.2.3