1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

intern.h: define rb_enumerator_size_func

* include/ruby/intern.h (rb_enumerator_size_func): define strict
  function declaration for rb_enumeratorize_with_size().


git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@41656 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
nobu 2013-06-26 13:43:22 +00:00
parent 26646186af
commit 5e2f227d21
11 changed files with 85 additions and 49 deletions

View file

@ -1,3 +1,8 @@
Wed Jun 26 22:43:20 2013 Nobuyoshi Nakada <nobu@ruby-lang.org>
* include/ruby/intern.h (rb_enumerator_size_func): define strict
function declaration for rb_enumeratorize_with_size().
Wed Jun 26 21:01:22 2013 Hiroshi Shirosaki <h.shirosaki@gmail.com>
* test/ruby/test_io.rb (TestIO#test_write_32bit_boundary): skip if

40
array.c
View file

@ -1665,6 +1665,12 @@ rb_ary_insert(int argc, VALUE *argv, VALUE ary)
static VALUE
rb_ary_length(VALUE ary);
static VALUE
ary_enum_length(VALUE ary, VALUE args, VALUE eobj)
{
return rb_ary_length(ary);
}
/*
* call-seq:
* ary.each { |item| block } -> ary
@ -1689,7 +1695,7 @@ rb_ary_each(VALUE array)
long i;
volatile VALUE ary = array;
RETURN_SIZED_ENUMERATOR(ary, 0, 0, rb_ary_length);
RETURN_SIZED_ENUMERATOR(ary, 0, 0, ary_enum_length);
for (i=0; i<RARRAY_LEN(ary); i++) {
rb_yield(RARRAY_AREF(ary, i));
}
@ -1718,7 +1724,7 @@ static VALUE
rb_ary_each_index(VALUE ary)
{
long i;
RETURN_SIZED_ENUMERATOR(ary, 0, 0, rb_ary_length);
RETURN_SIZED_ENUMERATOR(ary, 0, 0, ary_enum_length);
for (i=0; i<RARRAY_LEN(ary); i++) {
rb_yield(LONG2NUM(i));
@ -1746,7 +1752,7 @@ rb_ary_reverse_each(VALUE ary)
{
long len;
RETURN_SIZED_ENUMERATOR(ary, 0, 0, rb_ary_length);
RETURN_SIZED_ENUMERATOR(ary, 0, 0, ary_enum_length);
len = RARRAY_LEN(ary);
while (len--) {
rb_yield(RARRAY_AREF(ary, len));
@ -2507,7 +2513,7 @@ rb_ary_sort_by_bang(VALUE ary)
{
VALUE sorted;
RETURN_SIZED_ENUMERATOR(ary, 0, 0, rb_ary_length);
RETURN_SIZED_ENUMERATOR(ary, 0, 0, ary_enum_length);
rb_ary_modify(ary);
sorted = rb_block_call(ary, rb_intern("sort_by"), 0, 0, sort_by_i, 0);
rb_ary_replace(ary, sorted);
@ -2541,7 +2547,7 @@ rb_ary_collect(VALUE ary)
long i;
VALUE collect;
RETURN_SIZED_ENUMERATOR(ary, 0, 0, rb_ary_length);
RETURN_SIZED_ENUMERATOR(ary, 0, 0, ary_enum_length);
collect = rb_ary_new2(RARRAY_LEN(ary));
for (i = 0; i < RARRAY_LEN(ary); i++) {
rb_ary_push(collect, rb_yield(RARRAY_AREF(ary, i)));
@ -2574,7 +2580,7 @@ rb_ary_collect_bang(VALUE ary)
{
long i;
RETURN_SIZED_ENUMERATOR(ary, 0, 0, rb_ary_length);
RETURN_SIZED_ENUMERATOR(ary, 0, 0, ary_enum_length);
rb_ary_modify(ary);
for (i = 0; i < RARRAY_LEN(ary); i++) {
rb_ary_store(ary, i, rb_yield(RARRAY_AREF(ary, i)));
@ -2657,7 +2663,7 @@ rb_ary_select(VALUE ary)
VALUE result;
long i;
RETURN_SIZED_ENUMERATOR(ary, 0, 0, rb_ary_length);
RETURN_SIZED_ENUMERATOR(ary, 0, 0, ary_enum_length);
result = rb_ary_new2(RARRAY_LEN(ary));
for (i = 0; i < RARRAY_LEN(ary); i++) {
if (RTEST(rb_yield(RARRAY_AREF(ary, i)))) {
@ -2688,7 +2694,7 @@ rb_ary_select_bang(VALUE ary)
{
long i1, i2;
RETURN_SIZED_ENUMERATOR(ary, 0, 0, rb_ary_length);
RETURN_SIZED_ENUMERATOR(ary, 0, 0, ary_enum_length);
rb_ary_modify(ary);
for (i1 = i2 = 0; i1 < RARRAY_LEN(ary); i1++) {
VALUE v = RARRAY_AREF(ary, i1);
@ -2724,7 +2730,7 @@ rb_ary_select_bang(VALUE ary)
static VALUE
rb_ary_keep_if(VALUE ary)
{
RETURN_SIZED_ENUMERATOR(ary, 0, 0, rb_ary_length);
RETURN_SIZED_ENUMERATOR(ary, 0, 0, ary_enum_length);
rb_ary_select_bang(ary);
return ary;
}
@ -2982,7 +2988,7 @@ ary_reject_bang(VALUE ary)
static VALUE
rb_ary_reject_bang(VALUE ary)
{
RETURN_SIZED_ENUMERATOR(ary, 0, 0, rb_ary_length);
RETURN_SIZED_ENUMERATOR(ary, 0, 0, ary_enum_length);
return ary_reject_bang(ary);
}
@ -3004,7 +3010,7 @@ rb_ary_reject(VALUE ary)
{
VALUE rejected_ary;
RETURN_SIZED_ENUMERATOR(ary, 0, 0, rb_ary_length);
RETURN_SIZED_ENUMERATOR(ary, 0, 0, ary_enum_length);
rejected_ary = rb_ary_new();
ary_reject(ary, rejected_ary);
return rejected_ary;
@ -3031,7 +3037,7 @@ rb_ary_reject(VALUE ary)
static VALUE
rb_ary_delete_if(VALUE ary)
{
RETURN_SIZED_ENUMERATOR(ary, 0, 0, rb_ary_length);
RETURN_SIZED_ENUMERATOR(ary, 0, 0, ary_enum_length);
ary_reject_bang(ary);
return ary;
}
@ -4485,7 +4491,7 @@ rb_ary_sample(int argc, VALUE *argv, VALUE ary)
}
static VALUE
rb_ary_cycle_size(VALUE self, VALUE args)
rb_ary_cycle_size(VALUE self, VALUE args, VALUE eobj)
{
long mul;
VALUE n = Qnil;
@ -4622,7 +4628,7 @@ binomial_coefficient(long comb, long size)
}
static VALUE
rb_ary_permutation_size(VALUE ary, VALUE args)
rb_ary_permutation_size(VALUE ary, VALUE args, VALUE eobj)
{
long n = RARRAY_LEN(ary);
long k = (args && (RARRAY_LEN(args) > 0)) ? NUM2LONG(RARRAY_AREF(args, 0)) : n;
@ -4699,7 +4705,7 @@ rb_ary_permutation(int argc, VALUE *argv, VALUE ary)
}
static VALUE
rb_ary_combination_size(VALUE ary, VALUE args)
rb_ary_combination_size(VALUE ary, VALUE args, VALUE eobj)
{
long n = RARRAY_LEN(ary);
long k = NUM2LONG(RARRAY_AREF(args, 0));
@ -4822,7 +4828,7 @@ rpermute0(long n, long r, long *p, long index, VALUE values)
}
static VALUE
rb_ary_repeated_permutation_size(VALUE ary, VALUE args)
rb_ary_repeated_permutation_size(VALUE ary, VALUE args, VALUE eobj)
{
long n = RARRAY_LEN(ary);
long k = NUM2LONG(RARRAY_AREF(args, 0));
@ -4915,7 +4921,7 @@ rcombinate0(long n, long r, long *p, long index, long rest, VALUE values)
}
static VALUE
rb_ary_repeated_combination_size(VALUE ary, VALUE args)
rb_ary_repeated_combination_size(VALUE ary, VALUE args, VALUE eobj)
{
long n = RARRAY_LEN(ary);
long k = NUM2LONG(RARRAY_AREF(args, 0));

14
enum.c
View file

@ -312,7 +312,7 @@ find_all_i(VALUE i, VALUE ary, int argc, VALUE *argv)
}
static VALUE
enum_size(VALUE self, VALUE args)
enum_size(VALUE self, VALUE args, VALUE eobj)
{
VALUE r;
r = rb_check_funcall(self, id_size, 0, 0);
@ -1795,13 +1795,13 @@ each_slice_i(VALUE i, VALUE m, int argc, VALUE *argv)
}
static VALUE
enum_each_slice_size(VALUE obj, VALUE args)
enum_each_slice_size(VALUE obj, VALUE args, VALUE eobj)
{
VALUE n, size;
long slice_size = NUM2LONG(RARRAY_AREF(args, 0));
if (slice_size <= 0) rb_raise(rb_eArgError, "invalid slice size");
size = enum_size(obj, 0);
size = enum_size(obj, 0, 0);
if (size == Qnil) return Qnil;
n = rb_funcall(size, '+', 1, LONG2NUM(slice_size-1));
@ -1862,13 +1862,13 @@ each_cons_i(VALUE i, VALUE args, int argc, VALUE *argv)
}
static VALUE
enum_each_cons_size(VALUE obj, VALUE args)
enum_each_cons_size(VALUE obj, VALUE args, VALUE eobj)
{
VALUE n, size;
long cons_size = NUM2LONG(RARRAY_AREF(args, 0));
if (cons_size <= 0) rb_raise(rb_eArgError, "invalid size");
size = enum_size(obj, 0);
size = enum_size(obj, 0, 0);
if (size == Qnil) return Qnil;
n = rb_funcall(size, '+', 1, LONG2NUM(1 - cons_size));
@ -2251,11 +2251,11 @@ cycle_i(VALUE i, VALUE ary, int argc, VALUE *argv)
}
static VALUE
enum_cycle_size(VALUE self, VALUE args)
enum_cycle_size(VALUE self, VALUE args, VALUE eobj)
{
long mul;
VALUE n = Qnil;
VALUE size = enum_size(self, args);
VALUE size = enum_size(self, args, 0);
if (size == Qnil) return Qnil;

View file

@ -120,7 +120,7 @@ struct enumerator {
VALUE feedvalue;
VALUE stop_exc;
VALUE size;
VALUE (*size_fn)(ANYARGS);
rb_enumerator_size_func *size_fn;
};
static VALUE rb_cGenerator, rb_cYielder;
@ -474,6 +474,12 @@ enumerator_with_index_i(VALUE val, VALUE m, int argc, VALUE *argv)
static VALUE
enumerator_size(VALUE obj);
static VALUE
enumerator_enum_size(VALUE obj, VALUE args, VALUE eobj)
{
return enumerator_size(obj);
}
/*
* call-seq:
* e.with_index(offset = 0) {|(*args), idx| ... }
@ -492,7 +498,7 @@ enumerator_with_index(int argc, VALUE *argv, VALUE obj)
VALUE memo;
rb_scan_args(argc, argv, "01", &memo);
RETURN_SIZED_ENUMERATOR(obj, argc, argv, enumerator_size);
RETURN_SIZED_ENUMERATOR(obj, argc, argv, enumerator_enum_size);
if (NIL_P(memo))
memo = INT2FIX(0);
else
@ -557,7 +563,7 @@ enumerator_with_object_i(VALUE val, VALUE memo, int argc, VALUE *argv)
static VALUE
enumerator_with_object(VALUE obj, VALUE memo)
{
RETURN_SIZED_ENUMERATOR(obj, 1, &memo, enumerator_size);
RETURN_SIZED_ENUMERATOR(obj, 1, &memo, enumerator_enum_size);
enumerator_block_call(obj, enumerator_with_object_i, memo);
return memo;

24
hash.c
View file

@ -1014,6 +1014,12 @@ delete_if_i(VALUE key, VALUE value, VALUE hash)
static VALUE rb_hash_size(VALUE hash);
static VALUE
hash_enum_size(VALUE hash, VALUE args, VALUE eobj)
{
return rb_hash_size(hash);
}
/*
* call-seq:
* hsh.delete_if {| key, value | block } -> hsh
@ -1032,7 +1038,7 @@ static VALUE rb_hash_size(VALUE hash);
VALUE
rb_hash_delete_if(VALUE hash)
{
RETURN_SIZED_ENUMERATOR(hash, 0, 0, rb_hash_size);
RETURN_SIZED_ENUMERATOR(hash, 0, 0, hash_enum_size);
rb_hash_modify_check(hash);
if (RHASH(hash)->ntbl)
rb_hash_foreach(hash, delete_if_i, hash);
@ -1053,7 +1059,7 @@ rb_hash_reject_bang(VALUE hash)
{
st_index_t n;
RETURN_SIZED_ENUMERATOR(hash, 0, 0, rb_hash_size);
RETURN_SIZED_ENUMERATOR(hash, 0, 0, hash_enum_size);
rb_hash_modify(hash);
if (!RHASH(hash)->ntbl)
return Qnil;
@ -1130,7 +1136,7 @@ rb_hash_select(VALUE hash)
{
VALUE result;
RETURN_SIZED_ENUMERATOR(hash, 0, 0, rb_hash_size);
RETURN_SIZED_ENUMERATOR(hash, 0, 0, hash_enum_size);
result = rb_hash_new();
rb_hash_foreach(hash, select_i, result);
return result;
@ -1159,7 +1165,7 @@ rb_hash_select_bang(VALUE hash)
{
st_index_t n;
RETURN_SIZED_ENUMERATOR(hash, 0, 0, rb_hash_size);
RETURN_SIZED_ENUMERATOR(hash, 0, 0, hash_enum_size);
rb_hash_modify_check(hash);
if (!RHASH(hash)->ntbl)
return Qnil;
@ -1184,7 +1190,7 @@ rb_hash_select_bang(VALUE hash)
VALUE
rb_hash_keep_if(VALUE hash)
{
RETURN_SIZED_ENUMERATOR(hash, 0, 0, rb_hash_size);
RETURN_SIZED_ENUMERATOR(hash, 0, 0, hash_enum_size);
rb_hash_modify_check(hash);
if (RHASH(hash)->ntbl)
rb_hash_foreach(hash, keep_if_i, hash);
@ -1424,7 +1430,7 @@ each_value_i(VALUE key, VALUE value)
static VALUE
rb_hash_each_value(VALUE hash)
{
RETURN_SIZED_ENUMERATOR(hash, 0, 0, rb_hash_size);
RETURN_SIZED_ENUMERATOR(hash, 0, 0, hash_enum_size);
rb_hash_foreach(hash, each_value_i, 0);
return hash;
}
@ -1457,7 +1463,7 @@ each_key_i(VALUE key, VALUE value)
static VALUE
rb_hash_each_key(VALUE hash)
{
RETURN_SIZED_ENUMERATOR(hash, 0, 0, rb_hash_size);
RETURN_SIZED_ENUMERATOR(hash, 0, 0, hash_enum_size);
rb_hash_foreach(hash, each_key_i, 0);
return hash;
}
@ -1494,7 +1500,7 @@ each_pair_i(VALUE key, VALUE value)
static VALUE
rb_hash_each_pair(VALUE hash)
{
RETURN_SIZED_ENUMERATOR(hash, 0, 0, rb_hash_size);
RETURN_SIZED_ENUMERATOR(hash, 0, 0, hash_enum_size);
rb_hash_foreach(hash, each_pair_i, 0);
return hash;
}
@ -2667,7 +2673,7 @@ env_keys(void)
}
static VALUE
rb_env_size(VALUE ehash)
rb_env_size(VALUE ehash, VALUE args, VALUE eobj)
{
char **env;
long cnt = 0;

View file

@ -198,7 +198,8 @@ VALUE rb_fiber_alive_p(VALUE);
VALUE rb_enum_values_pack(int, VALUE*);
/* enumerator.c */
VALUE rb_enumeratorize(VALUE, VALUE, int, VALUE *);
VALUE rb_enumeratorize_with_size(VALUE, VALUE, int, VALUE *, VALUE (*)(ANYARGS));
typedef VALUE rb_enumerator_size_func(VALUE, VALUE, VALUE);
VALUE rb_enumeratorize_with_size(VALUE, VALUE, int, VALUE *, rb_enumerator_size_func *);
#define RETURN_SIZED_ENUMERATOR(obj, argc, argv, size_fn) do { \
if (!rb_block_given_p()) \
return rb_enumeratorize_with_size((obj), ID2SYM(rb_frame_this_func()),\

View file

@ -1829,7 +1829,7 @@ ruby_num_interval_step_size(VALUE from, VALUE to, VALUE step, int excl)
}
static VALUE
num_step_size(VALUE from, VALUE args)
num_step_size(VALUE from, VALUE args, VALUE eobj)
{
VALUE to = RARRAY_AREF(args, 0);
VALUE step = (RARRAY_LEN(args) > 1) ? RARRAY_AREF(args, 1) : INT2FIX(1);
@ -3473,7 +3473,7 @@ fix_size(VALUE fix)
}
static VALUE
int_upto_size(VALUE from, VALUE args)
int_upto_size(VALUE from, VALUE args, VALUE eobj)
{
return ruby_num_interval_step_size(from, RARRAY_AREF(args, 0), INT2FIX(1), FALSE);
}
@ -3520,7 +3520,7 @@ int_upto(VALUE from, VALUE to)
}
static VALUE
int_downto_size(VALUE from, VALUE args)
int_downto_size(VALUE from, VALUE args, VALUE eobj)
{
return ruby_num_interval_step_size(from, RARRAY_AREF(args, 0), INT2FIX(-1), FALSE);
}
@ -3568,7 +3568,7 @@ int_downto(VALUE from, VALUE to)
}
static VALUE
int_dotimes_size(VALUE num)
int_dotimes_size(VALUE num, VALUE args, VALUE eobj)
{
if (FIXNUM_P(num)) {
if (NUM2LONG(num) <= 0) return INT2FIX(0);

10
range.c
View file

@ -327,7 +327,7 @@ discrete_object_p(VALUE obj)
}
static VALUE
range_step_size(VALUE range, VALUE args)
range_step_size(VALUE range, VALUE args, VALUE eobj)
{
VALUE b = RANGE_BEG(range), e = RANGE_END(range);
VALUE step = INT2FIX(1);
@ -716,6 +716,12 @@ range_size(VALUE range)
return Qnil;
}
static VALUE
range_enum_size(VALUE range, VALUE args, VALUE eobj)
{
return range_size(range);
}
/*
* call-seq:
* rng.each {| i | block } -> rng
@ -742,7 +748,7 @@ range_each(VALUE range)
{
VALUE beg, end;
RETURN_SIZED_ENUMERATOR(range, 0, 0, range_size);
RETURN_SIZED_ENUMERATOR(range, 0, 0, range_enum_size);
beg = RANGE_BEG(range);
end = RANGE_END(range);

View file

@ -6291,7 +6291,7 @@ rb_str_lines(int argc, VALUE *argv, VALUE str)
}
static VALUE
rb_str_each_byte_size(VALUE str, VALUE args)
rb_str_each_byte_size(VALUE str, VALUE args, VALUE eobj)
{
return LONG2FIX(RSTRING_LEN(str));
}
@ -6371,7 +6371,7 @@ rb_str_bytes(VALUE str)
}
static VALUE
rb_str_each_char_size(VALUE str)
rb_str_each_char_size(VALUE str, VALUE args, VALUE eobj)
{
long len = RSTRING_LEN(str);
if (!single_byte_optimizable(str)) {

View file

@ -460,6 +460,12 @@ rb_struct_new(VALUE klass, ...)
static VALUE
rb_struct_size(VALUE s);
static VALUE
struct_enum_size(VALUE s, VALUE args, VALUE eobj)
{
return rb_struct_size(s);
}
/*
* call-seq:
* struct.each {|obj| block } -> struct
@ -484,7 +490,7 @@ rb_struct_each(VALUE s)
{
long i;
RETURN_SIZED_ENUMERATOR(s, 0, 0, rb_struct_size);
RETURN_SIZED_ENUMERATOR(s, 0, 0, struct_enum_size);
for (i=0; i<RSTRUCT_LEN(s); i++) {
rb_yield(RSTRUCT_GET(s, i));
}
@ -516,7 +522,7 @@ rb_struct_each_pair(VALUE s)
VALUE members;
long i;
RETURN_SIZED_ENUMERATOR(s, 0, 0, rb_struct_size);
RETURN_SIZED_ENUMERATOR(s, 0, 0, struct_enum_size);
members = rb_struct_members(s);
for (i=0; i<RSTRUCT_LEN(s); i++) {
VALUE key = rb_ary_entry(members, i);
@ -827,7 +833,7 @@ rb_struct_select(int argc, VALUE *argv, VALUE s)
long i;
rb_check_arity(argc, 0, 0);
RETURN_SIZED_ENUMERATOR(s, 0, 0, rb_struct_size);
RETURN_SIZED_ENUMERATOR(s, 0, 0, struct_enum_size);
result = rb_ary_new();
for (i = 0; i < RSTRUCT_LEN(s); i++) {
if (RTEST(rb_yield(RSTRUCT_GET(s, i)))) {

View file

@ -998,7 +998,7 @@ loop_i(void)
}
static VALUE
rb_f_loop_size(VALUE self, VALUE args)
rb_f_loop_size(VALUE self, VALUE args, VALUE eobj)
{
return DBL2NUM(INFINITY);
}