From 5adef8ba44b92ca01692451e21af5c3f47e8853c Mon Sep 17 00:00:00 2001 From: KOBAYASHI Shuji Date: Sat, 6 Apr 2019 17:40:51 +0900 Subject: Move `Array#(append|prepend)` from core to `mruby-ary-ext` They are not included in ISO standard. --- mrbgems/mruby-array-ext/mrblib/array.rb | 3 +++ 1 file changed, 3 insertions(+) (limited to 'mrbgems/mruby-array-ext') diff --git a/mrbgems/mruby-array-ext/mrblib/array.rb b/mrbgems/mruby-array-ext/mrblib/array.rb index 387bd6c90..59b6087d2 100644 --- a/mrbgems/mruby-array-ext/mrblib/array.rb +++ b/mrbgems/mruby-array-ext/mrblib/array.rb @@ -936,4 +936,7 @@ class Array end h end + + alias append push + alias prepend unshift end -- cgit v1.2.3 From 56e0e1934d4ec751d83f9f54ce93ca74b0e21194 Mon Sep 17 00:00:00 2001 From: dearblue Date: Sat, 25 May 2019 12:12:21 +0900 Subject: Name the return value of `mrb_range_beg_len()` --- include/mruby/range.h | 8 +++++++- mrbgems/mruby-array-ext/src/array.c | 2 +- mrbgems/mruby-kernel-ext/src/kernel.c | 2 +- mrbgems/mruby-string-ext/src/string.c | 6 +++--- src/array.c | 8 ++++---- src/range.c | 12 ++++++------ src/string.c | 4 ++-- 7 files changed, 24 insertions(+), 18 deletions(-) (limited to 'mrbgems/mruby-array-ext') diff --git a/include/mruby/range.h b/include/mruby/range.h index b5626993a..ee6eb8d1d 100644 --- a/include/mruby/range.h +++ b/include/mruby/range.h @@ -64,7 +64,13 @@ MRB_API struct RRange* mrb_range_ptr(mrb_state *mrb, mrb_value range); */ MRB_API mrb_value mrb_range_new(mrb_state *mrb, mrb_value start, mrb_value end, mrb_bool exclude); -MRB_API mrb_int mrb_range_beg_len(mrb_state *mrb, mrb_value range, mrb_int *begp, mrb_int *lenp, mrb_int len, mrb_bool trunc); +enum mrb_range_beg_len { + MRB_RANGE_TYPE_MISMATCH = 0, /* (failure) not range */ + MRB_RANGE_OK = 1, /* (success) range */ + MRB_RANGE_OUT = 2 /* (failure) out of range */ +}; + +MRB_API enum mrb_range_beg_len mrb_range_beg_len(mrb_state *mrb, mrb_value range, mrb_int *begp, mrb_int *lenp, mrb_int len, mrb_bool trunc); mrb_value mrb_get_values_at(mrb_state *mrb, mrb_value obj, mrb_int olen, mrb_int argc, const mrb_value *argv, mrb_value (*func)(mrb_state*, mrb_value, mrb_int)); void mrb_gc_mark_range(mrb_state *mrb, struct RRange *r); diff --git a/mrbgems/mruby-array-ext/src/array.c b/mrbgems/mruby-array-ext/src/array.c index b6d9c9c80..20c771a97 100644 --- a/mrbgems/mruby-array-ext/src/array.c +++ b/mrbgems/mruby-array-ext/src/array.c @@ -145,7 +145,7 @@ mrb_ary_slice_bang(mrb_state *mrb, mrb_value self) mrb_get_args(mrb, "o|i", &index, &len); switch (mrb_type(index)) { case MRB_TT_RANGE: - if (mrb_range_beg_len(mrb, index, &i, &len, ARY_LEN(a), TRUE) == 1) { + if (mrb_range_beg_len(mrb, index, &i, &len, ARY_LEN(a), TRUE) == MRB_RANGE_OK) { goto delete_pos_len; } else { diff --git a/mrbgems/mruby-kernel-ext/src/kernel.c b/mrbgems/mruby-kernel-ext/src/kernel.c index 9288b0e6f..8e7e03c56 100644 --- a/mrbgems/mruby-kernel-ext/src/kernel.c +++ b/mrbgems/mruby-kernel-ext/src/kernel.c @@ -22,7 +22,7 @@ mrb_f_caller(mrb_state *mrb, mrb_value self) case 1: if (mrb_type(v) == MRB_TT_RANGE) { mrb_int beg, len; - if (mrb_range_beg_len(mrb, v, &beg, &len, bt_len, TRUE) == 1) { + if (mrb_range_beg_len(mrb, v, &beg, &len, bt_len, TRUE) == MRB_RANGE_OK) { lev = beg; n = len; } diff --git a/mrbgems/mruby-string-ext/src/string.c b/mrbgems/mruby-string-ext/src/string.c index ba7e3c610..85180516c 100644 --- a/mrbgems/mruby-string-ext/src/string.c +++ b/mrbgems/mruby-string-ext/src/string.c @@ -58,11 +58,11 @@ mrb_str_byteslice(mrb_state *mrb, mrb_value str) len = RSTRING_LEN(str); switch (mrb_range_beg_len(mrb, a1, &beg, &len, len, TRUE)) { - case 0: /* not range */ + case MRB_RANGE_TYPE_MISMATCH: break; - case 1: /* range */ + case MRB_RANGE_OK: return mrb_str_substr(mrb, str, beg, len); - case 2: /* out of range */ + case MRB_RANGE_OUT: mrb_raisef(mrb, E_RANGE_ERROR, "%S out of range", a1); break; } diff --git a/src/array.c b/src/array.c index d4302cb22..bd9b4d358 100644 --- a/src/array.c +++ b/src/array.c @@ -858,7 +858,7 @@ mrb_ary_aget(mrb_state *mrb, mrb_value self) switch (mrb_type(index)) { /* a[n..m] */ case MRB_TT_RANGE: - if (mrb_range_beg_len(mrb, index, &i, &len, ARY_LEN(a), TRUE) == 1) { + if (mrb_range_beg_len(mrb, index, &i, &len, ARY_LEN(a), TRUE) == MRB_RANGE_OK) { return ary_subseq(mrb, a, i, len); } else { @@ -927,13 +927,13 @@ mrb_ary_aset(mrb_state *mrb, mrb_value self) if (mrb_get_args(mrb, "oo|o", &v1, &v2, &v3) == 2) { /* a[n..m] = v */ switch (mrb_range_beg_len(mrb, v1, &i, &len, RARRAY_LEN(self), FALSE)) { - case 0: /* not range */ + case MRB_RANGE_TYPE_MISMATCH: mrb_ary_set(mrb, self, aget_index(mrb, v1), v2); break; - case 1: /* range */ + case MRB_RANGE_OK: mrb_ary_splice(mrb, self, i, len, v2); break; - case 2: /* out of range */ + case MRB_RANGE_OUT: mrb_raisef(mrb, E_RANGE_ERROR, "%S out of range", v1); break; } diff --git a/src/range.c b/src/range.c index 21771c8ec..9036ef093 100644 --- a/src/range.c +++ b/src/range.c @@ -352,7 +352,7 @@ mrb_get_values_at(mrb_state *mrb, mrb_value obj, mrb_int olen, mrb_int argc, con if (mrb_fixnum_p(argv[i])) { mrb_ary_push(mrb, result, func(mrb, obj, mrb_fixnum(argv[i]))); } - else if (mrb_range_beg_len(mrb, argv[i], &beg, &len, olen, FALSE) == 1) { + else if (mrb_range_beg_len(mrb, argv[i], &beg, &len, olen, FALSE) == MRB_RANGE_OK) { mrb_int const end = olen < beg + len ? olen : beg + len; for (j = beg; j < end; ++j) { mrb_ary_push(mrb, result, func(mrb, obj, j)); @@ -398,13 +398,13 @@ mrb_range_new(mrb_state *mrb, mrb_value beg, mrb_value end, mrb_bool excl) return mrb_range_value(r); } -MRB_API mrb_int +MRB_API enum mrb_range_beg_len mrb_range_beg_len(mrb_state *mrb, mrb_value range, mrb_int *begp, mrb_int *lenp, mrb_int len, mrb_bool trunc) { mrb_int beg, end; struct RRange *r; - if (mrb_type(range) != MRB_TT_RANGE) return 0; + if (mrb_type(range) != MRB_TT_RANGE) return MRB_RANGE_TYPE_MISMATCH; r = mrb_range_ptr(mrb, range); beg = mrb_int(mrb, RANGE_BEG(r)); @@ -412,11 +412,11 @@ mrb_range_beg_len(mrb_state *mrb, mrb_value range, mrb_int *begp, mrb_int *lenp, if (beg < 0) { beg += len; - if (beg < 0) return 2; + if (beg < 0) return MRB_RANGE_OUT; } if (trunc) { - if (beg > len) return 2; + if (beg > len) return MRB_RANGE_OUT; if (end > len) end = len; } @@ -427,7 +427,7 @@ mrb_range_beg_len(mrb_state *mrb, mrb_value range, mrb_int *begp, mrb_int *lenp, *begp = beg; *lenp = len; - return 1; + return MRB_RANGE_OK; } void diff --git a/src/string.c b/src/string.c index 578e3bdcc..db2d73e32 100644 --- a/src/string.c +++ b/src/string.c @@ -1041,9 +1041,9 @@ num_index: len = RSTRING_CHAR_LEN(str); switch (mrb_range_beg_len(mrb, indx, &beg, &len, len, TRUE)) { - case 1: + case MRB_RANGE_OK: return str_subseq(mrb, str, beg, len); - case 2: + case MRB_RANGE_OUT: return mrb_nil_value(); default: break; -- cgit v1.2.3 From c1e6f324c29d16014a8c6c27b9e27cf9af9fc1d5 Mon Sep 17 00:00:00 2001 From: KOBAYASHI Shuji Date: Fri, 31 May 2019 20:50:54 +0900 Subject: Comment out the empty `Array#bsearch_index` test --- mrbgems/mruby-array-ext/test/array.rb | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'mrbgems/mruby-array-ext') diff --git a/mrbgems/mruby-array-ext/test/array.rb b/mrbgems/mruby-array-ext/test/array.rb index 853554bcc..82f09297a 100644 --- a/mrbgems/mruby-array-ext/test/array.rb +++ b/mrbgems/mruby-array-ext/test/array.rb @@ -268,9 +268,9 @@ assert("Array#bsearch") do end end -assert("Array#bsearch_index") do - # tested through Array#bsearch -end +# tested through Array#bsearch +#assert("Array#bsearch_index") do +#end assert("Array#delete_if") do a = [1, 2, 3, 4, 5] -- cgit v1.2.3 From 1fd08aee15942c3b3e3133f8cc92d4025df024a8 Mon Sep 17 00:00:00 2001 From: KOBAYASHI Shuji Date: Sun, 23 Jun 2019 20:01:33 +0900 Subject: Fix argument specs to `Array` --- mrbgems/mruby-array-ext/src/array.c | 2 +- src/array.c | 58 ++++++++++++++++++------------------- 2 files changed, 30 insertions(+), 30 deletions(-) (limited to 'mrbgems/mruby-array-ext') diff --git a/mrbgems/mruby-array-ext/src/array.c b/mrbgems/mruby-array-ext/src/array.c index 20c771a97..cb4798d49 100644 --- a/mrbgems/mruby-array-ext/src/array.c +++ b/mrbgems/mruby-array-ext/src/array.c @@ -194,7 +194,7 @@ mrb_mruby_array_ext_gem_init(mrb_state* mrb) mrb_define_method(mrb, a, "at", mrb_ary_at, MRB_ARGS_REQ(1)); mrb_define_method(mrb, a, "rassoc", mrb_ary_rassoc, MRB_ARGS_REQ(1)); mrb_define_method(mrb, a, "values_at", mrb_ary_values_at, MRB_ARGS_ANY()); - mrb_define_method(mrb, a, "slice!", mrb_ary_slice_bang, MRB_ARGS_ANY()); + mrb_define_method(mrb, a, "slice!", mrb_ary_slice_bang, MRB_ARGS_ARG(1,1)); } void diff --git a/src/array.c b/src/array.c index bd9b4d358..8cf813743 100644 --- a/src/array.c +++ b/src/array.c @@ -1262,39 +1262,39 @@ mrb_init_array(mrb_state *mrb) { struct RClass *a; - mrb->array_class = a = mrb_define_class(mrb, "Array", mrb->object_class); /* 15.2.12 */ + mrb->array_class = a = mrb_define_class(mrb, "Array", mrb->object_class); /* 15.2.12 */ MRB_SET_INSTANCE_TT(a, MRB_TT_ARRAY); - mrb_define_class_method(mrb, a, "[]", mrb_ary_s_create, MRB_ARGS_ANY()); /* 15.2.12.4.1 */ - - mrb_define_method(mrb, a, "+", mrb_ary_plus, MRB_ARGS_REQ(1)); /* 15.2.12.5.1 */ - mrb_define_method(mrb, a, "*", mrb_ary_times, MRB_ARGS_REQ(1)); /* 15.2.12.5.2 */ - mrb_define_method(mrb, a, "<<", mrb_ary_push_m, MRB_ARGS_REQ(1)); /* 15.2.12.5.3 */ - mrb_define_method(mrb, a, "[]", mrb_ary_aget, MRB_ARGS_ANY()); /* 15.2.12.5.4 */ - mrb_define_method(mrb, a, "[]=", mrb_ary_aset, MRB_ARGS_ANY()); /* 15.2.12.5.5 */ - mrb_define_method(mrb, a, "clear", mrb_ary_clear_m, MRB_ARGS_NONE()); /* 15.2.12.5.6 */ - mrb_define_method(mrb, a, "concat", mrb_ary_concat_m, MRB_ARGS_REQ(1)); /* 15.2.12.5.8 */ - mrb_define_method(mrb, a, "delete_at", mrb_ary_delete_at, MRB_ARGS_REQ(1)); /* 15.2.12.5.9 */ - mrb_define_method(mrb, a, "empty?", mrb_ary_empty_p, MRB_ARGS_NONE()); /* 15.2.12.5.12 */ - mrb_define_method(mrb, a, "first", mrb_ary_first, MRB_ARGS_OPT(1)); /* 15.2.12.5.13 */ - mrb_define_method(mrb, a, "index", mrb_ary_index_m, MRB_ARGS_REQ(1)); /* 15.2.12.5.14 */ - mrb_define_method(mrb, a, "initialize_copy", mrb_ary_replace_m, MRB_ARGS_REQ(1)); /* 15.2.12.5.16 */ - mrb_define_method(mrb, a, "join", mrb_ary_join_m, MRB_ARGS_ANY()); /* 15.2.12.5.17 */ - mrb_define_method(mrb, a, "last", mrb_ary_last, MRB_ARGS_ANY()); /* 15.2.12.5.18 */ - mrb_define_method(mrb, a, "length", mrb_ary_size, MRB_ARGS_NONE()); /* 15.2.12.5.19 */ - mrb_define_method(mrb, a, "pop", mrb_ary_pop, MRB_ARGS_NONE()); /* 15.2.12.5.21 */ - mrb_define_method(mrb, a, "push", mrb_ary_push_m, MRB_ARGS_ANY()); /* 15.2.12.5.22 */ - mrb_define_method(mrb, a, "replace", mrb_ary_replace_m, MRB_ARGS_REQ(1)); /* 15.2.12.5.23 */ - mrb_define_method(mrb, a, "reverse", mrb_ary_reverse, MRB_ARGS_NONE()); /* 15.2.12.5.24 */ - mrb_define_method(mrb, a, "reverse!", mrb_ary_reverse_bang, MRB_ARGS_NONE()); /* 15.2.12.5.25 */ - mrb_define_method(mrb, a, "rindex", mrb_ary_rindex_m, MRB_ARGS_REQ(1)); /* 15.2.12.5.26 */ - mrb_define_method(mrb, a, "shift", mrb_ary_shift, MRB_ARGS_NONE()); /* 15.2.12.5.27 */ - mrb_define_method(mrb, a, "size", mrb_ary_size, MRB_ARGS_NONE()); /* 15.2.12.5.28 */ - mrb_define_method(mrb, a, "slice", mrb_ary_aget, MRB_ARGS_ANY()); /* 15.2.12.5.29 */ - mrb_define_method(mrb, a, "unshift", mrb_ary_unshift_m, MRB_ARGS_ANY()); /* 15.2.12.5.30 */ + mrb_define_class_method(mrb, a, "[]", mrb_ary_s_create, MRB_ARGS_ANY()); /* 15.2.12.4.1 */ + + mrb_define_method(mrb, a, "+", mrb_ary_plus, MRB_ARGS_REQ(1)); /* 15.2.12.5.1 */ + mrb_define_method(mrb, a, "*", mrb_ary_times, MRB_ARGS_REQ(1)); /* 15.2.12.5.2 */ + mrb_define_method(mrb, a, "<<", mrb_ary_push_m, MRB_ARGS_REQ(1)); /* 15.2.12.5.3 */ + mrb_define_method(mrb, a, "[]", mrb_ary_aget, MRB_ARGS_ARG(1,1)); /* 15.2.12.5.4 */ + mrb_define_method(mrb, a, "[]=", mrb_ary_aset, MRB_ARGS_ARG(2,1)); /* 15.2.12.5.5 */ + mrb_define_method(mrb, a, "clear", mrb_ary_clear_m, MRB_ARGS_NONE()); /* 15.2.12.5.6 */ + mrb_define_method(mrb, a, "concat", mrb_ary_concat_m, MRB_ARGS_REQ(1)); /* 15.2.12.5.8 */ + mrb_define_method(mrb, a, "delete_at", mrb_ary_delete_at, MRB_ARGS_REQ(1)); /* 15.2.12.5.9 */ + mrb_define_method(mrb, a, "empty?", mrb_ary_empty_p, MRB_ARGS_NONE()); /* 15.2.12.5.12 */ + mrb_define_method(mrb, a, "first", mrb_ary_first, MRB_ARGS_OPT(1)); /* 15.2.12.5.13 */ + mrb_define_method(mrb, a, "index", mrb_ary_index_m, MRB_ARGS_REQ(1)); /* 15.2.12.5.14 */ + mrb_define_method(mrb, a, "initialize_copy", mrb_ary_replace_m, MRB_ARGS_REQ(1)); /* 15.2.12.5.16 */ + mrb_define_method(mrb, a, "join", mrb_ary_join_m, MRB_ARGS_OPT(1)); /* 15.2.12.5.17 */ + mrb_define_method(mrb, a, "last", mrb_ary_last, MRB_ARGS_OPT(1)); /* 15.2.12.5.18 */ + mrb_define_method(mrb, a, "length", mrb_ary_size, MRB_ARGS_NONE()); /* 15.2.12.5.19 */ + mrb_define_method(mrb, a, "pop", mrb_ary_pop, MRB_ARGS_NONE()); /* 15.2.12.5.21 */ + mrb_define_method(mrb, a, "push", mrb_ary_push_m, MRB_ARGS_ANY()); /* 15.2.12.5.22 */ + mrb_define_method(mrb, a, "replace", mrb_ary_replace_m, MRB_ARGS_REQ(1)); /* 15.2.12.5.23 */ + mrb_define_method(mrb, a, "reverse", mrb_ary_reverse, MRB_ARGS_NONE()); /* 15.2.12.5.24 */ + mrb_define_method(mrb, a, "reverse!", mrb_ary_reverse_bang, MRB_ARGS_NONE()); /* 15.2.12.5.25 */ + mrb_define_method(mrb, a, "rindex", mrb_ary_rindex_m, MRB_ARGS_REQ(1)); /* 15.2.12.5.26 */ + mrb_define_method(mrb, a, "shift", mrb_ary_shift, MRB_ARGS_NONE()); /* 15.2.12.5.27 */ + mrb_define_method(mrb, a, "size", mrb_ary_size, MRB_ARGS_NONE()); /* 15.2.12.5.28 */ + mrb_define_method(mrb, a, "slice", mrb_ary_aget, MRB_ARGS_ARG(1,1)); /* 15.2.12.5.29 */ + mrb_define_method(mrb, a, "unshift", mrb_ary_unshift_m, MRB_ARGS_ANY()); /* 15.2.12.5.30 */ mrb_define_method(mrb, a, "__ary_eq", mrb_ary_eq, MRB_ARGS_REQ(1)); mrb_define_method(mrb, a, "__ary_cmp", mrb_ary_cmp, MRB_ARGS_REQ(1)); - mrb_define_method(mrb, a, "__ary_index", mrb_ary_index_m, MRB_ARGS_REQ(1)); /* kept for mruby-array-ext */ + mrb_define_method(mrb, a, "__ary_index", mrb_ary_index_m, MRB_ARGS_REQ(1)); /* kept for mruby-array-ext */ mrb_define_method(mrb, a, "__svalue", mrb_ary_svalue, MRB_ARGS_NONE()); } -- cgit v1.2.3 From 87cc58ba3d851651ba90947df475388a3dc7ded8 Mon Sep 17 00:00:00 2001 From: KOBAYASHI Shuji Date: Thu, 25 Jul 2019 22:31:54 +0900 Subject: Refine `Array#(permutation|combination) test` - No guarantees about the order in which the permutations/combinations are yielded. - Drop dependency on `Enumerator`. --- mrbgems/mruby-array-ext/test/array.rb | 46 ++++++++++++++++++++--------------- 1 file changed, 26 insertions(+), 20 deletions(-) (limited to 'mrbgems/mruby-array-ext') diff --git a/mrbgems/mruby-array-ext/test/array.rb b/mrbgems/mruby-array-ext/test/array.rb index 82f09297a..4fad42518 100644 --- a/mrbgems/mruby-array-ext/test/array.rb +++ b/mrbgems/mruby-array-ext/test/array.rb @@ -1,6 +1,20 @@ ## # Array(Ext) Test +def assert_permutation_combination(exp, receiver, meth, *args) + act = [] + receiver.__send__(meth, *args) { |v| act << v } + assert_equal(exp, act.sort) +end + +def assert_permutation(exp, receiver, *args) + assert_permutation_combination(exp, receiver, :permutation, *args) +end + +def assert_combination(exp, receiver, *args) + assert_permutation_combination(exp, receiver, :combination, *args) +end + assert("Array#assoc") do s1 = [ "colors", "red", "blue", "green" ] s2 = [ "letters", "a", "b", "c" ] @@ -369,30 +383,22 @@ end assert("Array#permutation") do a = [1, 2, 3] - assert_equal([[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]], - a.permutation.to_a) - assert_equal([[1],[2],[3]], - a.permutation(1).to_a) - assert_equal([[1,2],[1,3],[2,1],[2,3],[3,1],[3,2]], - a.permutation(2).to_a) - assert_equal([[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]], - a.permutation(3).to_a) - assert_equal([[]], a.permutation(0).to_a) - assert_equal([], a.permutation(4).to_a) + assert_permutation([[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]], a) + assert_permutation([[1],[2],[3]], a, 1) + assert_permutation([[1,2],[1,3],[2,1],[2,3],[3,1],[3,2]], a, 2) + assert_permutation([[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]], a, 3) + assert_permutation([[]], a, 0) + assert_permutation([], a, 4) end assert("Array#combination") do a = [1, 2, 3, 4] - assert_equal([[1],[2],[3],[4]], - a.combination(1).to_a) - assert_equal([[1,2],[1,3],[1,4],[2,3],[2,4],[3,4]], - a.combination(2).to_a) - assert_equal([[1,2,3],[1,2,4],[1,3,4],[2,3,4]], - a.combination(3).to_a) - assert_equal([[1,2,3,4]], - a.combination(4).to_a) - assert_equal([[]], a.combination(0).to_a) - assert_equal([], a.combination(5).to_a) + assert_combination([[1],[2],[3],[4]], a, 1) + assert_combination([[1,2],[1,3],[1,4],[2,3],[2,4],[3,4]], a, 2) + assert_combination([[1,2,3],[1,2,4],[1,3,4],[2,3,4]], a, 3) + assert_combination([[1,2,3,4]], a, 4) + assert_combination([[]], a, 0) + assert_combination([], a, 5) end assert('Array#transpose') do -- cgit v1.2.3 From 778310260ca51b91125bd6deeb836c5032fa837e Mon Sep 17 00:00:00 2001 From: KOBAYASHI Shuji Date: Fri, 26 Jul 2019 11:48:53 +0900 Subject: Drop dependency from `mruby-array-ext` to `mruby-enum-ext` --- mrbgems/mruby-array-ext/mrbgem.rake | 1 - mrbgems/mruby-array-ext/mrblib/array.rb | 4 ++-- 2 files changed, 2 insertions(+), 3 deletions(-) (limited to 'mrbgems/mruby-array-ext') diff --git a/mrbgems/mruby-array-ext/mrbgem.rake b/mrbgems/mruby-array-ext/mrbgem.rake index 58d4428d4..882caf1ab 100644 --- a/mrbgems/mruby-array-ext/mrbgem.rake +++ b/mrbgems/mruby-array-ext/mrbgem.rake @@ -2,5 +2,4 @@ MRuby::Gem::Specification.new('mruby-array-ext') do |spec| spec.license = 'MIT' spec.author = 'mruby developers' spec.summary = 'Array class extension' - spec.add_test_dependency 'mruby-enumerator', core: 'mruby-enumerator' end diff --git a/mrbgems/mruby-array-ext/mrblib/array.rb b/mrbgems/mruby-array-ext/mrblib/array.rb index 59b6087d2..1cd1eb643 100644 --- a/mrbgems/mruby-array-ext/mrblib/array.rb +++ b/mrbgems/mruby-array-ext/mrblib/array.rb @@ -903,8 +903,8 @@ class Array column_count = nil self.each do |row| raise TypeError unless row.is_a?(Array) - column_count ||= row.count - raise IndexError, 'element size differs' unless column_count == row.count + column_count ||= row.size + raise IndexError, 'element size differs' unless column_count == row.size end Array.new(column_count) do |column_index| -- cgit v1.2.3 From 28d1f6cdc48782247b6d39be0e1a794404da26f5 Mon Sep 17 00:00:00 2001 From: KOBAYASHI Shuji Date: Fri, 30 Aug 2019 17:04:25 +0900 Subject: `Array#(permutation|combination)` without block should return `self` --- mrbgems/mruby-array-ext/mrblib/array.rb | 14 +++++++------- mrbgems/mruby-array-ext/test/array.rb | 7 +++++-- 2 files changed, 12 insertions(+), 9 deletions(-) (limited to 'mrbgems/mruby-array-ext') diff --git a/mrbgems/mruby-array-ext/mrblib/array.rb b/mrbgems/mruby-array-ext/mrblib/array.rb index 1cd1eb643..fef78f83f 100644 --- a/mrbgems/mruby-array-ext/mrblib/array.rb +++ b/mrbgems/mruby-array-ext/mrblib/array.rb @@ -815,12 +815,11 @@ class Array # a.permutation(0).to_a #=> [[]] # one permutation of length 0 # a.permutation(4).to_a #=> [] # no permutations of length 4 def permutation(n=self.size, &block) - size = self.size return to_enum(:permutation, n) unless block - return if n > size + size = self.size if n == 0 - yield [] - else + yield [] + elsif n <= size i = 0 while i [] # no combinations of length 5 def combination(n, &block) - size = self.size return to_enum(:combination, n) unless block - return if n > size + size = self.size if n == 0 yield [] elsif n == 1 @@ -872,7 +871,7 @@ class Array yield [self[i]] i += 1 end - else + elsif n <= size i = 0 while i Date: Sat, 31 Aug 2019 13:03:30 +0900 Subject: `Array#permutation` with a negative argument should not yield Before this patch: $ bin/mruby -e '[1].permutation(-1){|v| p v}' #=> [1] After this patch (same as Ruby): $ bin/mruby -e '[1].permutation(-1){|v| p v}' #=> no output --- mrbgems/mruby-array-ext/mrblib/array.rb | 2 +- mrbgems/mruby-array-ext/test/array.rb | 2 ++ 2 files changed, 3 insertions(+), 1 deletion(-) (limited to 'mrbgems/mruby-array-ext') diff --git a/mrbgems/mruby-array-ext/mrblib/array.rb b/mrbgems/mruby-array-ext/mrblib/array.rb index fef78f83f..fc5d87f2c 100644 --- a/mrbgems/mruby-array-ext/mrblib/array.rb +++ b/mrbgems/mruby-array-ext/mrblib/array.rb @@ -819,7 +819,7 @@ class Array size = self.size if n == 0 yield [] - elsif n <= size + elsif 0 < n && n <= size i = 0 while i Date: Mon, 16 Sep 2019 07:52:19 +0900 Subject: Add `Array#difference` method from Ruby2.6. --- mrbgems/mruby-array-ext/mrblib/array.rb | 16 ++++++++++++++++ mrbgems/mruby-array-ext/test/array.rb | 8 ++++++++ 2 files changed, 24 insertions(+) (limited to 'mrbgems/mruby-array-ext') diff --git a/mrbgems/mruby-array-ext/mrblib/array.rb b/mrbgems/mruby-array-ext/mrblib/array.rb index fc5d87f2c..f9f232321 100644 --- a/mrbgems/mruby-array-ext/mrblib/array.rb +++ b/mrbgems/mruby-array-ext/mrblib/array.rb @@ -124,6 +124,22 @@ class Array ary end + ## + # call-seq: + # ary.difference(other_ary1, other_ary2, ...) -> new_ary + # + # Returns a new array that is a copy of the original array, removing all + # occurences of any item that also appear in +other_ary+. The order is + # preserved from the original array. + # + def difference(*args) + ary = self.dup + args.each do |x| + ary = self - x + end + ary + end + ## # call-seq: # ary & other_ary -> new_ary diff --git a/mrbgems/mruby-array-ext/test/array.rb b/mrbgems/mruby-array-ext/test/array.rb index a4e328b71..029325aab 100644 --- a/mrbgems/mruby-array-ext/test/array.rb +++ b/mrbgems/mruby-array-ext/test/array.rb @@ -93,6 +93,14 @@ assert("Array#union") do assert_equal [1, 2, 3, 4, 5], a.union(b,c) end +assert("Array#difference") do + a = [1, 2, 3, 1] + b = [1, 4] + c = [1, 5] + + assert_equal [2, 3], a.difference(b,c) +end + assert("Array#&") do a = [1, 2, 3, 1] b = [1, 4] -- cgit v1.2.3 From 57a0132b41920ea14b903113586bf970c8210efa Mon Sep 17 00:00:00 2001 From: "Yukihiro \"Matz\" Matsumoto" Date: Mon, 16 Sep 2019 07:55:18 +0900 Subject: Add `filter` aliases for `Enumerable` and `Hash`. --- mrbgems/mruby-array-ext/mrblib/array.rb | 1 + mrbgems/mruby-enum-ext/mrblib/enum.rb | 1 + mrbgems/mruby-hash-ext/mrblib/hash.rb | 3 +++ 3 files changed, 5 insertions(+) (limited to 'mrbgems/mruby-array-ext') diff --git a/mrbgems/mruby-array-ext/mrblib/array.rb b/mrbgems/mruby-array-ext/mrblib/array.rb index f9f232321..5201cbe57 100644 --- a/mrbgems/mruby-array-ext/mrblib/array.rb +++ b/mrbgems/mruby-array-ext/mrblib/array.rb @@ -955,4 +955,5 @@ class Array alias append push alias prepend unshift + alias filter! select! end diff --git a/mrbgems/mruby-enum-ext/mrblib/enum.rb b/mrbgems/mruby-enum-ext/mrblib/enum.rb index b427bd67e..171737e28 100644 --- a/mrbgems/mruby-enum-ext/mrblib/enum.rb +++ b/mrbgems/mruby-enum-ext/mrblib/enum.rb @@ -826,4 +826,5 @@ module Enumerable end hash.values end + alias filter select end diff --git a/mrbgems/mruby-hash-ext/mrblib/hash.rb b/mrbgems/mruby-hash-ext/mrblib/hash.rb index 547f3404a..33e2dcb9f 100644 --- a/mrbgems/mruby-hash-ext/mrblib/hash.rb +++ b/mrbgems/mruby-hash-ext/mrblib/hash.rb @@ -494,4 +494,7 @@ class Hash self.fetch(k, &block) end end + + alias filter select + alias filter! select! end -- cgit v1.2.3 From 1fb4e73981d2c9588eb25acfcc2d34fca3a415bd Mon Sep 17 00:00:00 2001 From: KOBAYASHI Shuji Date: Fri, 20 Sep 2019 09:28:56 +0900 Subject: Fix typo in `Array#difference` document [ci skip] --- mrbgems/mruby-array-ext/mrblib/array.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'mrbgems/mruby-array-ext') diff --git a/mrbgems/mruby-array-ext/mrblib/array.rb b/mrbgems/mruby-array-ext/mrblib/array.rb index 5201cbe57..7cec44dba 100644 --- a/mrbgems/mruby-array-ext/mrblib/array.rb +++ b/mrbgems/mruby-array-ext/mrblib/array.rb @@ -129,7 +129,7 @@ class Array # ary.difference(other_ary1, other_ary2, ...) -> new_ary # # Returns a new array that is a copy of the original array, removing all - # occurences of any item that also appear in +other_ary+. The order is + # occurrences of any item that also appear in +other_ary+. The order is # preserved from the original array. # def difference(*args) -- cgit v1.2.3 From feaf80d8996340bd0316fda72418b1abd774bd59 Mon Sep 17 00:00:00 2001 From: KOBAYASHI Shuji Date: Thu, 26 Sep 2019 22:23:27 +0900 Subject: Use type predicate macros instead of `mrb_type` if possible For efficiency with `MRB_WORD_BOXING` (implement type predicate macros for all `enum mrb_vtype`). --- include/mruby/boxing_word.h | 14 +++++++++ include/mruby/data.h | 2 +- include/mruby/value.h | 57 ++++++++++++++++++++++++++++++----- mrbgems/mruby-array-ext/src/array.c | 2 +- mrbgems/mruby-class-ext/src/class.c | 2 +- mrbgems/mruby-compiler/core/codegen.c | 4 +-- mrbgems/mruby-fiber/src/fiber.c | 2 +- mrbgems/mruby-io/src/io.c | 4 +-- mrbgems/mruby-io/test/mruby_io_test.c | 8 ++--- mrbgems/mruby-kernel-ext/src/kernel.c | 2 +- mrbgems/mruby-method/src/method.c | 4 +-- mrbgems/mruby-random/src/random.c | 2 +- mrbgems/mruby-socket/src/socket.c | 2 +- src/class.c | 12 ++++---- src/etc.c | 4 +-- src/gc.c | 4 +-- src/hash.c | 2 +- src/kernel.c | 6 ++-- src/numeric.c | 2 +- src/object.c | 2 +- src/proc.c | 4 +-- src/range.c | 4 +-- src/state.c | 4 +-- src/string.c | 4 +-- src/symbol.c | 2 +- src/variable.c | 2 +- src/vm.c | 10 +++--- 27 files changed, 112 insertions(+), 55 deletions(-) (limited to 'mrbgems/mruby-array-ext') diff --git a/include/mruby/boxing_word.h b/include/mruby/boxing_word.h index 22d549687..d763ffaf8 100644 --- a/include/mruby/boxing_word.h +++ b/include/mruby/boxing_word.h @@ -133,6 +133,20 @@ MRB_API mrb_value mrb_word_boxing_float_pool(struct mrb_state*, mrb_float); #define mrb_hash_p(o) BOXWORD_OBJ_TYPE_P(o, HASH) #define mrb_cptr_p(o) BOXWORD_OBJ_TYPE_P(o, CPTR) #define mrb_exception_p(o) BOXWORD_OBJ_TYPE_P(o, EXCEPTION) +#define mrb_free_p(o) BOXWORD_OBJ_TYPE_P(o, FREE) +#define mrb_object_p(o) BOXWORD_OBJ_TYPE_P(o, OBJECT) +#define mrb_class_p(o) BOXWORD_OBJ_TYPE_P(o, CLASS) +#define mrb_module_p(o) BOXWORD_OBJ_TYPE_P(o, MODULE) +#define mrb_iclass_p(o) BOXWORD_OBJ_TYPE_P(o, ICLASS) +#define mrb_sclass_p(o) BOXWORD_OBJ_TYPE_P(o, SCLASS) +#define mrb_proc_p(o) BOXWORD_OBJ_TYPE_P(o, PROC) +#define mrb_range_p(o) BOXWORD_OBJ_TYPE_P(o, RANGE) +#define mrb_file_p(o) BOXWORD_OBJ_TYPE_P(o, FILE) +#define mrb_env_p(o) BOXWORD_OBJ_TYPE_P(o, ENV) +#define mrb_data_p(o) BOXWORD_OBJ_TYPE_P(o, DATA) +#define mrb_fiber_p(o) BOXWORD_OBJ_TYPE_P(o, FIBER) +#define mrb_istruct_p(o) BOXWORD_OBJ_TYPE_P(o, ISTRUCT) +#define mrb_break_p(o) BOXWORD_OBJ_TYPE_P(o, BREAK) #ifndef MRB_WITHOUT_FLOAT #define SET_FLOAT_VALUE(mrb,r,v) ((r) = mrb_word_boxing_float_value(mrb, v)) diff --git a/include/mruby/data.h b/include/mruby/data.h index 35ec2c25b..7bdf1c34e 100644 --- a/include/mruby/data.h +++ b/include/mruby/data.h @@ -66,7 +66,7 @@ MRB_API void *mrb_data_check_get_ptr(mrb_state *mrb, mrb_value, const mrb_data_t MRB_INLINE void mrb_data_init(mrb_value v, void *ptr, const mrb_data_type *type) { - mrb_assert(mrb_type(v) == MRB_TT_DATA); + mrb_assert(mrb_data_p(v)); DATA_PTR(v) = ptr; DATA_TYPE(v) = type; } diff --git a/include/mruby/value.h b/include/mruby/value.h index b318e9042..84ea7fb0a 100644 --- a/include/mruby/value.h +++ b/include/mruby/value.h @@ -169,6 +169,11 @@ typedef void mrb_value; #include "boxing_no.h" #endif +#if !defined(MRB_SYMBOL_BITSIZE) +#define MRB_SYMBOL_BITSIZE (sizeof(mrb_sym) * CHAR_BIT) +#define MRB_SYMBOL_MAX UINT32_MAX +#endif + #ifndef mrb_immediate_p #define mrb_immediate_p(o) (mrb_type(o) < MRB_TT_FREE) #endif @@ -190,13 +195,6 @@ typedef void mrb_value; #ifndef mrb_true_p #define mrb_true_p(o) (mrb_type(o) == MRB_TT_TRUE) #endif -#ifndef mrb_bool -#define mrb_bool(o) (mrb_type(o) != MRB_TT_FALSE) -#endif -#if !defined(MRB_SYMBOL_BITSIZE) -#define MRB_SYMBOL_BITSIZE (sizeof(mrb_sym) * CHAR_BIT) -#define MRB_SYMBOL_MAX UINT32_MAX -#endif #ifndef MRB_WITHOUT_FLOAT #ifndef mrb_float_p #define mrb_float_p(o) (mrb_type(o) == MRB_TT_FLOAT) @@ -217,6 +215,51 @@ typedef void mrb_value; #ifndef mrb_exception_p #define mrb_exception_p(o) (mrb_type(o) == MRB_TT_EXCEPTION) #endif +#ifndef mrb_free_p +#define mrb_free_p(o) (mrb_type(o) == MRB_TT_FREE) +#endif +#ifndef mrb_object_p +#define mrb_object_p(o) (mrb_type(o) == MRB_TT_OBJECT) +#endif +#ifndef mrb_class_p +#define mrb_class_p(o) (mrb_type(o) == MRB_TT_CLASS) +#endif +#ifndef mrb_module_p +#define mrb_module_p(o) (mrb_type(o) == MRB_TT_MODULE) +#endif +#ifndef mrb_iclass_p +#define mrb_iclass_p(o) (mrb_type(o) == MRB_TT_ICLASS) +#endif +#ifndef mrb_sclass_p +#define mrb_sclass_p(o) (mrb_type(o) == MRB_TT_SCLASS) +#endif +#ifndef mrb_proc_p +#define mrb_proc_p(o) (mrb_type(o) == MRB_TT_PROC) +#endif +#ifndef mrb_range_p +#define mrb_range_p(o) (mrb_type(o) == MRB_TT_RANGE) +#endif +#ifndef mrb_file_p +#define mrb_file_p(o) (mrb_type(o) == MRB_TT_FILE) +#endif +#ifndef mrb_env_p +#define mrb_env_p(o) (mrb_type(o) == MRB_TT_ENV) +#endif +#ifndef mrb_data_p +#define mrb_data_p(o) (mrb_type(o) == MRB_TT_DATA) +#endif +#ifndef mrb_fiber_p +#define mrb_fiber_p(o) (mrb_type(o) == MRB_TT_FIBER) +#endif +#ifndef mrb_istruct_p +#define mrb_istruct_p(o) (mrb_type(o) == MRB_TT_ISTRUCT) +#endif +#ifndef mrb_break_p +#define mrb_break_p(o) (mrb_type(o) == MRB_TT_BREAK) +#endif +#ifndef mrb_bool +#define mrb_bool(o) (mrb_type(o) != MRB_TT_FALSE) +#endif #define mrb_test(o) mrb_bool(o) /** diff --git a/mrbgems/mruby-array-ext/src/array.c b/mrbgems/mruby-array-ext/src/array.c index cb4798d49..ab6d99133 100644 --- a/mrbgems/mruby-array-ext/src/array.c +++ b/mrbgems/mruby-array-ext/src/array.c @@ -65,7 +65,7 @@ mrb_ary_rassoc(mrb_state *mrb, mrb_value ary) for (i = 0; i < RARRAY_LEN(ary); ++i) { v = RARRAY_PTR(ary)[i]; - if (mrb_type(v) == MRB_TT_ARRAY && + if (mrb_array_p(v) && RARRAY_LEN(v) > 1 && mrb_equal(mrb, RARRAY_PTR(v)[1], value)) return v; diff --git a/mrbgems/mruby-class-ext/src/class.c b/mrbgems/mruby-class-ext/src/class.c index 255e62f6b..0d27c30ed 100644 --- a/mrbgems/mruby-class-ext/src/class.c +++ b/mrbgems/mruby-class-ext/src/class.c @@ -11,7 +11,7 @@ mrb_mod_name(mrb_state *mrb, mrb_value self) static mrb_value mrb_mod_singleton_class_p(mrb_state *mrb, mrb_value self) { - return mrb_bool_value(mrb_type(self) == MRB_TT_SCLASS); + return mrb_bool_value(mrb_sclass_p(self)); } /* diff --git a/mrbgems/mruby-compiler/core/codegen.c b/mrbgems/mruby-compiler/core/codegen.c index 1989c0cf1..0af76bd17 100644 --- a/mrbgems/mruby-compiler/core/codegen.c +++ b/mrbgems/mruby-compiler/core/codegen.c @@ -559,7 +559,7 @@ new_lit(codegen_scope *s, mrb_value val) mrb_int len; pv = &s->irep->pool[i]; - if (mrb_type(*pv) != MRB_TT_STRING) continue; + if (!mrb_string_p(*pv)) continue; if ((len = RSTRING_LEN(*pv)) != RSTRING_LEN(val)) continue; if (memcmp(RSTRING_PTR(*pv), RSTRING_PTR(val), len) == 0) return i; @@ -570,7 +570,7 @@ new_lit(codegen_scope *s, mrb_value val) for (i=0; iirep->plen; i++) { mrb_float f1, f2; pv = &s->irep->pool[i]; - if (mrb_type(*pv) != MRB_TT_FLOAT) continue; + if (!mrb_float_p(*pv)) continue; f1 = mrb_float(*pv); f2 = mrb_float(val); if (f1 == f2 && !signbit(f1) == !signbit(f2)) return i; diff --git a/mrbgems/mruby-fiber/src/fiber.c b/mrbgems/mruby-fiber/src/fiber.c index e47849dda..e22985bd3 100644 --- a/mrbgems/mruby-fiber/src/fiber.c +++ b/mrbgems/mruby-fiber/src/fiber.c @@ -294,7 +294,7 @@ fiber_eq(mrb_state *mrb, mrb_value self) mrb_value other; mrb_get_args(mrb, "o", &other); - if (mrb_type(other) != MRB_TT_FIBER) { + if (!mrb_fiber_p(other)) { return mrb_false_value(); } return mrb_bool_value(fiber_ptr(self) == fiber_ptr(other)); diff --git a/mrbgems/mruby-io/src/io.c b/mrbgems/mruby-io/src/io.c index 8228097cb..624c27f47 100644 --- a/mrbgems/mruby-io/src/io.c +++ b/mrbgems/mruby-io/src/io.c @@ -907,7 +907,7 @@ mrb_io_syswrite(mrb_state *mrb, mrb_value io) } mrb_get_args(mrb, "S", &str); - if (mrb_type(str) != MRB_TT_STRING) { + if (!mrb_string_p(str)) { buf = mrb_funcall(mrb, str, "to_s", 0); } else { buf = str; @@ -1000,7 +1000,7 @@ static int mrb_io_read_data_pending(mrb_state *mrb, mrb_value io) { mrb_value buf = mrb_iv_get(mrb, io, mrb_intern_cstr(mrb, "@buf")); - if (mrb_type(buf) == MRB_TT_STRING && RSTRING_LEN(buf) > 0) { + if (mrb_string_p(buf) && RSTRING_LEN(buf) > 0) { return 1; } return 0; diff --git a/mrbgems/mruby-io/test/mruby_io_test.c b/mrbgems/mruby-io/test/mruby_io_test.c index 2c8a75fc9..eb552c41a 100644 --- a/mrbgems/mruby-io/test/mruby_io_test.c +++ b/mrbgems/mruby-io/test/mruby_io_test.c @@ -154,16 +154,16 @@ mrb_io_test_io_cleanup(mrb_state *mrb, mrb_value self) mrb_value symlinkname = mrb_gv_get(mrb, mrb_intern_cstr(mrb, "$mrbtest_io_symlinkname")); mrb_value socketname = mrb_gv_get(mrb, mrb_intern_cstr(mrb, "$mrbtest_io_socketname")); - if (mrb_type(rfname) == MRB_TT_STRING) { + if (mrb_string_p(rfname)) { remove(RSTRING_PTR(rfname)); } - if (mrb_type(wfname) == MRB_TT_STRING) { + if (mrb_string_p(wfname)) { remove(RSTRING_PTR(wfname)); } - if (mrb_type(symlinkname) == MRB_TT_STRING) { + if (mrb_string_p(symlinkname)) { remove(RSTRING_PTR(symlinkname)); } - if (mrb_type(socketname) == MRB_TT_STRING) { + if (mrb_string_p(socketname)) { remove(RSTRING_PTR(socketname)); } diff --git a/mrbgems/mruby-kernel-ext/src/kernel.c b/mrbgems/mruby-kernel-ext/src/kernel.c index 376751e10..a2af6b46f 100644 --- a/mrbgems/mruby-kernel-ext/src/kernel.c +++ b/mrbgems/mruby-kernel-ext/src/kernel.c @@ -20,7 +20,7 @@ mrb_f_caller(mrb_state *mrb, mrb_value self) n = bt_len - lev; break; case 1: - if (mrb_type(v) == MRB_TT_RANGE) { + if (mrb_range_p(v)) { mrb_int beg, len; if (mrb_range_beg_len(mrb, v, &beg, &len, bt_len, TRUE) == MRB_RANGE_OK) { lev = beg; diff --git a/mrbgems/mruby-method/src/method.c b/mrbgems/mruby-method/src/method.c index d95ca1664..1557e60ca 100644 --- a/mrbgems/mruby-method/src/method.c +++ b/mrbgems/mruby-method/src/method.c @@ -14,10 +14,10 @@ method_object_alloc(mrb_state *mrb, struct RClass *mclass) static void bind_check(mrb_state *mrb, mrb_value recv, mrb_value owner) { - if (mrb_type(owner) != MRB_TT_MODULE && + if (!mrb_module_p(owner) && mrb_class_ptr(owner) != mrb_obj_class(mrb, recv) && !mrb_obj_is_kind_of(mrb, recv, mrb_class_ptr(owner))) { - if (mrb_type(owner) == MRB_TT_SCLASS) { + if (mrb_sclass_p(owner)) { mrb_raise(mrb, E_TYPE_ERROR, "singleton method called for a different object"); } else { mrb_raisef(mrb, E_TYPE_ERROR, "bind argument must be an instance of %v", owner); diff --git a/mrbgems/mruby-random/src/random.c b/mrbgems/mruby-random/src/random.c index af9876ce7..515c0707a 100644 --- a/mrbgems/mruby-random/src/random.c +++ b/mrbgems/mruby-random/src/random.c @@ -136,7 +136,7 @@ get_opt(mrb_state* mrb) static void random_check(mrb_state *mrb, mrb_value random) { struct RClass *c = mrb_class_get(mrb, "Random"); - if (!mrb_obj_is_kind_of(mrb, random, c) || mrb_type(random) != MRB_TT_ISTRUCT) { + if (!mrb_obj_is_kind_of(mrb, random, c) || !mrb_istruct_p(random)) { mrb_raise(mrb, E_TYPE_ERROR, "Random instance required"); } } diff --git a/mrbgems/mruby-socket/src/socket.c b/mrbgems/mruby-socket/src/socket.c index 53f761617..53cd9f4aa 100644 --- a/mrbgems/mruby-socket/src/socket.c +++ b/mrbgems/mruby-socket/src/socket.c @@ -455,7 +455,7 @@ mrb_basicsocket_setsockopt(mrb_state *mrb, mrb_value self) level = mrb_fixnum(so); if (mrb_string_p(optval)) { /* that's good */ - } else if (mrb_type(optval) == MRB_TT_TRUE || mrb_type(optval) == MRB_TT_FALSE) { + } else if (mrb_true_p(optval) || mrb_false_p(optval)) { mrb_int i = mrb_test(optval) ? 1 : 0; optval = mrb_str_new(mrb, (char*)&i, sizeof(i)); } else if (mrb_fixnum_p(optval)) { diff --git a/src/class.c b/src/class.c index adb8954cc..745e0a72c 100644 --- a/src/class.c +++ b/src/class.c @@ -215,7 +215,7 @@ mrb_vm_define_module(mrb_state *mrb, mrb_value outer, mrb_sym id) if (mrb_const_defined_at(mrb, outer, id)) { mrb_value old = mrb_const_get(mrb, outer, id); - if (mrb_type(old) != MRB_TT_MODULE) { + if (!mrb_module_p(old)) { mrb_raisef(mrb, E_TYPE_ERROR, "%!v is not a module", old); } return mrb_class_ptr(old); @@ -312,7 +312,7 @@ mrb_vm_define_class(mrb_state *mrb, mrb_value outer, mrb_value super, mrb_sym id struct RClass *c; if (!mrb_nil_p(super)) { - if (mrb_type(super) != MRB_TT_CLASS) { + if (!mrb_class_p(super)) { mrb_raisef(mrb, E_TYPE_ERROR, "superclass must be a Class (%!v given)", super); } s = mrb_class_ptr(super); @@ -324,7 +324,7 @@ mrb_vm_define_class(mrb_state *mrb, mrb_value outer, mrb_value super, mrb_sym id if (mrb_const_defined_at(mrb, outer, id)) { mrb_value old = mrb_const_get(mrb, outer, id); - if (mrb_type(old) != MRB_TT_CLASS) { + if (!mrb_class_p(old)) { mrb_raisef(mrb, E_TYPE_ERROR, "%!v is not a class", old); } c = mrb_class_ptr(old); @@ -381,7 +381,7 @@ mrb_exc_get(mrb_state *mrb, const char *name) mrb_value c = mrb_const_get(mrb, mrb_obj_value(mrb->object_class), mrb_intern_cstr(mrb, name)); - if (mrb_type(c) != MRB_TT_CLASS) { + if (!mrb_class_p(c)) { mrb_raise(mrb, mrb->eException_class, "exception corrupted"); } exc = e = mrb_class_ptr(c); @@ -791,7 +791,7 @@ mrb_get_args(mrb_state *mrb, const char *format, ...) p = va_arg(ap, void**); if (i < argc) { ss = argv[arg_i]; - if (mrb_type(ss) != MRB_TT_ISTRUCT) + if (!mrb_istruct_p(ss)) { mrb_raisef(mrb, E_TYPE_ERROR, "%v is not inline struct", ss); } @@ -1789,7 +1789,7 @@ mrb_value mrb_mod_to_s(mrb_state *mrb, mrb_value klass) { - if (mrb_type(klass) == MRB_TT_SCLASS) { + if (mrb_sclass_p(klass)) { mrb_value v = mrb_iv_get(mrb, klass, mrb_intern_lit(mrb, "__attached__")); mrb_value str = mrb_str_new_lit(mrb, "#range_class)) return mrb_false_value(); - if (mrb_type(obj) != MRB_TT_RANGE) return mrb_false_value(); + if (!mrb_range_p(obj)) return mrb_false_value(); r = mrb_range_ptr(mrb, range); o = mrb_range_ptr(mrb, obj); @@ -391,7 +391,7 @@ mrb_range_beg_len(mrb_state *mrb, mrb_value range, mrb_int *begp, mrb_int *lenp, mrb_int beg, end; struct RRange *r; - if (mrb_type(range) != MRB_TT_RANGE) return MRB_RANGE_TYPE_MISMATCH; + if (!mrb_range_p(range)) return MRB_RANGE_TYPE_MISMATCH; r = mrb_range_ptr(mrb, range); beg = mrb_int(mrb, RANGE_BEG(r)); diff --git a/src/state.c b/src/state.c index 99b523dd5..3e5ebb483 100644 --- a/src/state.c +++ b/src/state.c @@ -119,12 +119,12 @@ mrb_irep_free(mrb_state *mrb, mrb_irep *irep) if (!(irep->flags & MRB_ISEQ_NO_FREE)) mrb_free(mrb, (void*)irep->iseq); if (irep->pool) for (i=0; iplen; i++) { - if (mrb_type(irep->pool[i]) == MRB_TT_STRING) { + if (mrb_string_p(irep->pool[i])) { mrb_gc_free_str(mrb, RSTRING(irep->pool[i])); mrb_free(mrb, mrb_obj_ptr(irep->pool[i])); } #if defined(MRB_WORD_BOXING) && !defined(MRB_WITHOUT_FLOAT) - else if (mrb_type(irep->pool[i]) == MRB_TT_FLOAT) { + else if (mrb_float_p(irep->pool[i])) { mrb_free(mrb, mrb_obj_ptr(irep->pool[i])); } #endif diff --git a/src/string.c b/src/string.c index f53985ab9..b49fdfc2e 100644 --- a/src/string.c +++ b/src/string.c @@ -1688,7 +1688,7 @@ mrb_str_eql(mrb_state *mrb, mrb_value self) mrb_bool eql_p; mrb_get_args(mrb, "o", &str2); - eql_p = (mrb_type(str2) == MRB_TT_STRING) && str_eql(mrb, self, str2); + eql_p = (mrb_string_p(str2)) && str_eql(mrb, self, str2); return mrb_bool_value(eql_p); } @@ -2977,7 +2977,7 @@ mrb_str_byteslice(mrb_state *mrb, mrb_value str) beg = mrb_fixnum(mrb_to_int(mrb, a1)); len = mrb_fixnum(mrb_to_int(mrb, a2)); } - else if (mrb_type(a1) == MRB_TT_RANGE) { + else if (mrb_range_p(a1)) { if (mrb_range_beg_len(mrb, a1, &beg, &len, str_len, TRUE) != MRB_RANGE_OK) { return mrb_nil_value(); } diff --git a/src/symbol.c b/src/symbol.c index 6ff1e54da..90cb49fd8 100644 --- a/src/symbol.c +++ b/src/symbol.c @@ -551,7 +551,7 @@ sym_cmp(mrb_state *mrb, mrb_value s1) mrb_sym sym1, sym2; mrb_get_args(mrb, "o", &s2); - if (mrb_type(s2) != MRB_TT_SYMBOL) return mrb_nil_value(); + if (!mrb_symbol_p(s2)) return mrb_nil_value(); sym1 = mrb_symbol(s1); sym2 = mrb_symbol(s2); if (sym1 == sym2) return mrb_fixnum_value(0); diff --git a/src/variable.c b/src/variable.c index d7a07d5be..7d4470186 100644 --- a/src/variable.c +++ b/src/variable.c @@ -486,7 +486,7 @@ inspect_i(mrb_state *mrb, mrb_sym sym, mrb_value v, void *p) s = mrb_sym_name_len(mrb, sym, &len); mrb_str_cat(mrb, str, s, len); mrb_str_cat_lit(mrb, str, "="); - if (mrb_type(v) == MRB_TT_OBJECT) { + if (mrb_object_p(v)) { ins = mrb_any_to_s(mrb, v); } else { diff --git a/src/vm.c b/src/vm.c index 6d342a381..54f74907e 100644 --- a/src/vm.c +++ b/src/vm.c @@ -816,7 +816,7 @@ mrb_yield_cont(mrb_state *mrb, mrb_value b, mrb_value self, mrb_int argc, const if (mrb_nil_p(b)) { mrb_raise(mrb, E_ARGUMENT_ERROR, "no block given"); } - if (mrb_type(b) != MRB_TT_PROC) { + if (!mrb_proc_p(b)) { mrb_raise(mrb, E_TYPE_ERROR, "not a block"); } @@ -1387,7 +1387,7 @@ RETRY_TRY_BLOCK: recv = regs[a]; blk = regs[bidx]; - if (!mrb_nil_p(blk) && mrb_type(blk) != MRB_TT_PROC) { + if (!mrb_nil_p(blk) && !mrb_proc_p(blk)) { blk = mrb_convert_type(mrb, blk, MRB_TT_PROC, "Proc", "to_proc"); /* The stack might have been reallocated during mrb_convert_type(), see #3622 */ @@ -1447,7 +1447,7 @@ RETRY_TRY_BLOCK: mrb_gc_arena_shrink(mrb, ai); if (mrb->exc) goto L_RAISE; ci = mrb->c->ci; - if (mrb_type(blk) == MRB_TT_PROC) { + if (mrb_proc_p(blk)) { struct RProc *p = mrb_proc_ptr(blk); if (p && !MRB_PROC_STRICT_P(p) && MRB_PROC_ENV(p) == ci[-1].env) { p->flags |= MRB_PROC_ORPHAN; @@ -1588,7 +1588,7 @@ RETRY_TRY_BLOCK: goto L_RAISE; } blk = regs[bidx]; - if (!mrb_nil_p(blk) && mrb_type(blk) != MRB_TT_PROC) { + if (!mrb_nil_p(blk) && !mrb_proc_p(blk)) { blk = mrb_convert_type(mrb, blk, MRB_TT_PROC, "Proc", "to_proc"); /* The stack or ci stack might have been reallocated during mrb_convert_type(), see #3622 and #3784 */ @@ -1939,7 +1939,7 @@ RETRY_TRY_BLOCK: else { blk = regs[ci->argc+1]; } - if (mrb_type(blk) == MRB_TT_PROC) { + if (mrb_proc_p(blk)) { struct RProc *p = mrb_proc_ptr(blk); if (!MRB_PROC_STRICT_P(p) && -- cgit v1.2.3 From 67ea80b2bd2df64bbfefd0ba8f74f6eeb52074aa Mon Sep 17 00:00:00 2001 From: "Yukihiro \"Matz\" Matsumoto" Date: Sat, 12 Oct 2019 12:38:25 +0900 Subject: Fixed a bug in `Array#difference`. --- mrbgems/mruby-array-ext/mrblib/array.rb | 4 ++-- mrbgems/mruby-array-ext/test/array.rb | 6 +++--- 2 files changed, 5 insertions(+), 5 deletions(-) (limited to 'mrbgems/mruby-array-ext') diff --git a/mrbgems/mruby-array-ext/mrblib/array.rb b/mrbgems/mruby-array-ext/mrblib/array.rb index 7cec44dba..d4a72a927 100644 --- a/mrbgems/mruby-array-ext/mrblib/array.rb +++ b/mrbgems/mruby-array-ext/mrblib/array.rb @@ -133,9 +133,9 @@ class Array # preserved from the original array. # def difference(*args) - ary = self.dup + ary = self args.each do |x| - ary = self - x + ary = ary - x end ary end diff --git a/mrbgems/mruby-array-ext/test/array.rb b/mrbgems/mruby-array-ext/test/array.rb index 029325aab..0cfb1e857 100644 --- a/mrbgems/mruby-array-ext/test/array.rb +++ b/mrbgems/mruby-array-ext/test/array.rb @@ -94,9 +94,9 @@ assert("Array#union") do end assert("Array#difference") do - a = [1, 2, 3, 1] - b = [1, 4] - c = [1, 5] + a = [1, 2, 3, 1, 6, 7] + b = [1, 4, 6] + c = [1, 5, 7] assert_equal [2, 3], a.difference(b,c) end -- cgit v1.2.3 From 682406a38425fd6389a67580e524b9c5c15c25fb Mon Sep 17 00:00:00 2001 From: "Yukihiro \"Matz\" Matsumoto" Date: Sat, 12 Oct 2019 12:39:04 +0900 Subject: Move `Array#difference` just after `Array#-`. --- mrbgems/mruby-array-ext/mrblib/array.rb | 32 ++++++++++++++++---------------- 1 file changed, 16 insertions(+), 16 deletions(-) (limited to 'mrbgems/mruby-array-ext') diff --git a/mrbgems/mruby-array-ext/mrblib/array.rb b/mrbgems/mruby-array-ext/mrblib/array.rb index d4a72a927..fa3db3a70 100644 --- a/mrbgems/mruby-array-ext/mrblib/array.rb +++ b/mrbgems/mruby-array-ext/mrblib/array.rb @@ -88,6 +88,22 @@ class Array array end + ## + # call-seq: + # ary.difference(other_ary1, other_ary2, ...) -> new_ary + # + # Returns a new array that is a copy of the original array, removing all + # occurrences of any item that also appear in +other_ary+. The order is + # preserved from the original array. + # + def difference(*args) + ary = self + args.each do |x| + ary = ary - x + end + ary + end + ## # call-seq: # ary | other_ary -> new_ary @@ -124,22 +140,6 @@ class Array ary end - ## - # call-seq: - # ary.difference(other_ary1, other_ary2, ...) -> new_ary - # - # Returns a new array that is a copy of the original array, removing all - # occurrences of any item that also appear in +other_ary+. The order is - # preserved from the original array. - # - def difference(*args) - ary = self - args.each do |x| - ary = ary - x - end - ary - end - ## # call-seq: # ary & other_ary -> new_ary -- cgit v1.2.3 From bdacdfaea9bf4b52770c32d60b7d402b48297c58 Mon Sep 17 00:00:00 2001 From: "Yukihiro \"Matz\" Matsumoto" Date: Sat, 12 Oct 2019 12:39:29 +0900 Subject: Add `Array#intersection` which is new in Ruby2.7. --- mrbgems/mruby-array-ext/mrblib/array.rb | 18 ++++++++++++++++++ mrbgems/mruby-array-ext/test/array.rb | 8 ++++++++ 2 files changed, 26 insertions(+) (limited to 'mrbgems/mruby-array-ext') diff --git a/mrbgems/mruby-array-ext/mrblib/array.rb b/mrbgems/mruby-array-ext/mrblib/array.rb index fa3db3a70..be1676666 100644 --- a/mrbgems/mruby-array-ext/mrblib/array.rb +++ b/mrbgems/mruby-array-ext/mrblib/array.rb @@ -173,6 +173,24 @@ class Array array end + ## + # call-seq: + # ary.intersection(other_ary,...) -> new_ary + # + # Set Intersection---Returns a new array containing elements common to + # this array and other_ary, removing duplicates. + # + # ["a", "b", "c"].union(["c", "d", "a"], ["a", "c", "e"]) + # #=> ["a", "b", "c", "d", "e"] + # + def intersection(*args) + ary = self + args.each do |x| + ary = ary & x + end + ary + end + ## # call-seq: # ary.flatten -> new_ary diff --git a/mrbgems/mruby-array-ext/test/array.rb b/mrbgems/mruby-array-ext/test/array.rb index 0cfb1e857..cb76559c7 100644 --- a/mrbgems/mruby-array-ext/test/array.rb +++ b/mrbgems/mruby-array-ext/test/array.rb @@ -111,6 +111,14 @@ assert("Array#&") do assert_equal [1, 2, 3, 1], a end +assert("Array#intersection") do + a = [1, 2, 3, 1, 8, 6, 7, 8] + b = [1, 4, 6, 8] + c = [1, 5, 7, 8] + + assert_equal [1, 8], a.intersection(b,c) +end + assert("Array#flatten") do assert_equal [1, 2, "3", {4=>5}, :'6'], [1, 2, "3", {4=>5}, :'6'].flatten assert_equal [1, 2, 3, 4, 5, 6], [1, 2, [3, 4, 5], 6].flatten -- cgit v1.2.3 From ac29638a8ac5ba25d4004c06e25074d1c1503a99 Mon Sep 17 00:00:00 2001 From: KOBAYASHI Shuji Date: Mon, 14 Oct 2019 18:16:57 +0900 Subject: Fix the example of `Array#intersection` in the document [ci skip] --- mrbgems/mruby-array-ext/mrblib/array.rb | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'mrbgems/mruby-array-ext') diff --git a/mrbgems/mruby-array-ext/mrblib/array.rb b/mrbgems/mruby-array-ext/mrblib/array.rb index be1676666..5492ba2eb 100644 --- a/mrbgems/mruby-array-ext/mrblib/array.rb +++ b/mrbgems/mruby-array-ext/mrblib/array.rb @@ -178,10 +178,10 @@ class Array # ary.intersection(other_ary,...) -> new_ary # # Set Intersection---Returns a new array containing elements common to - # this array and other_ary, removing duplicates. + # this array and other_arys, removing duplicates. The order is + # preserved from the original array. # - # ["a", "b", "c"].union(["c", "d", "a"], ["a", "c", "e"]) - # #=> ["a", "b", "c", "d", "e"] + # [1, 2, 3].intersection([3, 4, 1], [1, 3, 5]) #=> [1, 3] # def intersection(*args) ary = self -- cgit v1.2.3