first commit
CI / Build (clang, clang++, ubuntu-22.04, linux, 3.10, master, x86,x86_64) (push) Canceled after 0s
CI / Build (clang, clang++, windows-2022, windows, 3.10, master, x86,x86_64) (push) Canceled after 0s
CI / Release (push) Canceled after 0s

This commit is contained in:
2026-09-13 22:18:11 +01:00
commit 08c03ac533
6701 changed files with 1577314 additions and 0 deletions
@@ -0,0 +1,5 @@
MRuby::Gem::Specification.new('mruby-range-ext') do |spec|
spec.license = 'MIT'
spec.author = 'mruby developers'
spec.summary = 'Range class extension'
end
@@ -0,0 +1,66 @@
class Range
##
# call-seq:
# rng.first -> obj
# rng.first(n) -> an_array
#
# Returns the first object in the range, or an array of the first +n+
# elements.
#
# (10..20).first #=> 10
# (10..20).first(3) #=> [10, 11, 12]
#
def first(*args)
return self.begin if args.empty?
raise ArgumentError, "wrong number of arguments (given #{args.length}, expected 1)" unless args.length == 1
nv = args[0]
n = nv.__to_int
raise ArgumentError, "negative array size (or size too big)" unless 0 <= n
ary = []
each do |i|
break if n <= 0
ary.push(i)
n -= 1
end
ary
end
def max(&block)
val = self.first
last = self.last
return super if block
# fast path for numerics
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?
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?(Numeric) && last.kind_of?(Numeric)
return nil if val > last
return nil if val == last && exclude_end?
min = val
return min
end
# delegate to Enumerable
super
end
end
@@ -0,0 +1,188 @@
#include <mruby.h>
#include <mruby/range.h>
#include <math.h>
static mrb_bool
r_le(mrb_state *mrb, mrb_value a, mrb_value b)
{
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)
{
return mrb_cmp(mrb, a, b) == -1;
}
/*
* call-seq:
* rng.cover?(obj) -> true or false
*
* Returns <code>true</code> if +obj+ is between the begin and end of
* the range.
*
* This tests <code>begin <= obj <= end</code> when #exclude_end? is +false+
* and <code>begin <= obj < end</code> when #exclude_end? is +true+.
*
* ("a".."z").cover?("c") #=> true
* ("a".."z").cover?("5") #=> false
* ("a".."z").cover?("cc") #=> true
*/
static mrb_value
range_cover(mrb_state *mrb, mrb_value range)
{
struct RRange *r = mrb_range_ptr(mrb, range);
mrb_value val = mrb_get_arg1(mrb);
mrb_value beg, end;
beg = RANGE_BEG(r);
end = RANGE_END(r);
if (r_le(mrb, beg, val)) {
if (RANGE_EXCL(r)) {
if (r_lt(mrb, val, end))
return mrb_true_value();
}
else {
if (r_le(mrb, val, end))
return mrb_true_value();
}
}
return mrb_false_value();
}
/*
* call-seq:
* rng.last -> obj
* rng.last(n) -> an_array
*
* Returns the last object in the range,
* or an array of the last +n+ elements.
*
* Note that with no arguments +last+ will return the object that defines
* the end of the range even if #exclude_end? is +true+.
*
* (10..20).last #=> 20
* (10...20).last #=> 20
* (10..20).last(3) #=> [18, 19, 20]
* (10...20).last(3) #=> [17, 18, 19]
*/
static mrb_value
range_last(mrb_state *mrb, mrb_value range)
{
mrb_value num;
mrb_value array;
if (mrb_get_args(mrb, "|o", &num) == 0) {
return mrb_range_end(mrb, range);
}
array = mrb_funcall(mrb, range, "to_a", 0);
return mrb_funcall(mrb, array, "last", 1, mrb_to_int(mrb, num));
}
/*
* call-seq:
* rng.size -> num
*
* Returns the number of elements in the range. Both the begin and the end of
* the Range must be Numeric, otherwise nil is returned.
*
* (10..20).size #=> 11
* ('a'..'z').size #=> nil
*/
#ifndef MRB_WITHOUT_FLOAT
static mrb_value
range_size(mrb_state *mrb, mrb_value range)
{
struct RRange *r = mrb_range_ptr(mrb, range);
mrb_value beg, end;
mrb_float beg_f, end_f;
mrb_bool num_p = TRUE;
mrb_bool excl;
beg = RANGE_BEG(r);
end = RANGE_END(r);
excl = RANGE_EXCL(r);
if (mrb_fixnum_p(beg)) {
beg_f = (mrb_float)mrb_fixnum(beg);
}
else if (mrb_float_p(beg)) {
beg_f = mrb_float(beg);
}
else {
num_p = FALSE;
}
if (mrb_fixnum_p(end)) {
end_f = (mrb_float)mrb_fixnum(end);
}
else if (mrb_float_p(end)) {
end_f = mrb_float(end);
}
else {
num_p = FALSE;
}
if (num_p) {
mrb_float n = end_f - beg_f;
mrb_float err = (fabs(beg_f) + fabs(end_f) + fabs(end_f-beg_f)) * MRB_FLOAT_EPSILON;
if (err>0.5) err=0.5;
if (excl) {
if (n<=0) return mrb_fixnum_value(0);
if (n<1)
n = 0;
else
n = floor(n - err);
}
else {
if (n<0) return mrb_fixnum_value(0);
n = floor(n + err);
}
if (isinf(n+1))
return mrb_float_value(mrb, INFINITY);
return mrb_fixnum_value((mrb_int)n+1);
}
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)
{
struct RClass * s = mrb_class_get(mrb, "Range");
mrb_define_method(mrb, s, "cover?", range_cover, MRB_ARGS_REQ(1));
mrb_define_method(mrb, s, "last", range_last, MRB_ARGS_OPT(1));
mrb_define_method(mrb, s, "size", range_size, MRB_ARGS_NONE());
}
void
mrb_mruby_range_ext_gem_final(mrb_state* mrb)
{
}
@@ -0,0 +1,142 @@
##
# Range(Ext) Test
assert('Range#cover?') do
assert_true ("a".."z").cover?("c")
assert_true !("a".."z").cover?("5")
assert_true ("a".."z").cover?("cc")
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
assert('Range#last') do
assert_equal 20, (10..20).last
assert_equal 20, (10...20).last
assert_equal [18, 19, 20], (10..20).last(3)
assert_equal [17, 18, 19], (10...20).last(3)
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
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 536870911, (0...2**29).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
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
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
assert_equal 1, (1...10).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
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
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