From 9387ff7315b498a6e7c8ab2a4b1582fd6c717524 Mon Sep 17 00:00:00 2001 From: nobu Date: Sat, 27 Aug 2016 01:26:17 +0000 Subject: multiple arguments * array.c (rb_ary_concat_multi): take multiple arguments. based on the patch by Satoru Horie. [Feature #12333] * string.c (rb_str_concat_multi, rb_str_prepend_multi): ditto. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@56021 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- array.c | 47 +++++++++++++++++++++++++++++++++++++---------- 1 file changed, 37 insertions(+), 10 deletions(-) (limited to 'array.c') diff --git a/array.c b/array.c index 6e0c62070b..5cb6fc449c 100644 --- a/array.c +++ b/array.c @@ -3623,31 +3623,58 @@ rb_ary_plus(VALUE x, VALUE y) return z; } +static VALUE +ary_append(VALUE x, VALUE y) +{ + long n = RARRAY_LEN(y); + if (n > 0) { + rb_ary_splice(x, RARRAY_LEN(x), 0, RARRAY_CONST_PTR(y), n); + } + return x; +} + /* * call-seq: - * ary.concat(other_ary) -> ary + * ary.concat(other_ary1, other_ary2,...) -> ary * - * Appends the elements of +other_ary+ to +self+. + * Appends the elements of +other_ary+s to +self+. * * [ "a", "b" ].concat( ["c", "d"] ) #=> [ "a", "b", "c", "d" ] + * [ "a" ].concat( ["b"], ["c", "d"] ) #=> [ "a", "b", "c", "d" ] + * [ "a" ].concat #=> [ "a" ] + * * a = [ 1, 2, 3 ] * a.concat( [ 4, 5 ] ) * a #=> [ 1, 2, 3, 4, 5 ] * + * a = [ 1, 2 ] + * a.concat(a, a) #=> [1, 2, 1, 2, 1, 2] + * * See also Array#+. */ -VALUE -rb_ary_concat(VALUE x, VALUE y) +static VALUE +rb_ary_concat_multi(int argc, VALUE *argv, VALUE ary) { - rb_ary_modify_check(x); - y = to_ary(y); - if (RARRAY_LEN(y) > 0) { - rb_ary_splice(x, RARRAY_LEN(x), 0, RARRAY_CONST_PTR(y), RARRAY_LEN(y)); + rb_ary_modify_check(ary); + + if (argc > 0) { + int i; + VALUE args = rb_ary_tmp_new(argc); + for (i = 0; i < argc; i++) { + rb_ary_concat(args, argv[i]); + } + ary_append(ary, args); } - return x; + + return ary; } +VALUE +rb_ary_concat(VALUE x, VALUE y) +{ + return ary_append(x, to_ary(y)); +} /* * call-seq: @@ -6073,7 +6100,7 @@ Init_Array(void) rb_define_method(rb_cArray, "fetch", rb_ary_fetch, -1); rb_define_method(rb_cArray, "first", rb_ary_first, -1); rb_define_method(rb_cArray, "last", rb_ary_last, -1); - rb_define_method(rb_cArray, "concat", rb_ary_concat, 1); + rb_define_method(rb_cArray, "concat", rb_ary_concat_multi, -1); rb_define_method(rb_cArray, "<<", rb_ary_push, 1); rb_define_method(rb_cArray, "push", rb_ary_push_m, -1); rb_define_method(rb_cArray, "pop", rb_ary_pop_m, -1); -- cgit v1.2.3