diff --git array.c array.c index 86cf09e..34961fb 100644 --- array.c +++ array.c @@ -3620,22 +3620,27 @@ rb_ary_plus(VALUE x, VALUE y) /* * 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) { - rb_ary_modify_check(x); y = to_ary(y); if (RARRAY_LEN(y) > 0) { rb_ary_splice(x, RARRAY_LEN(x), 0, y); @@ -3643,6 +3648,23 @@ rb_ary_concat(VALUE x, VALUE y) return x; } +static VALUE +rb_ary_concat_multi(int argc, VALUE *argv, VALUE ary) +{ + int i; + VALUE args = rb_ary_new(); + + rb_ary_modify_check(ary); + + for (i = 0; i < argc; i++) { + rb_ary_concat(args, argv[i]); + } + + rb_ary_concat(ary, args); + + return ary; +} + /* * call-seq: @@ -5810,7 +5832,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); diff --git string.c string.c index 2782d28..7fc93c2 100644 --- string.c +++ string.c @@ -1,5 +1,4 @@ /********************************************************************** - string.c - $Author$ @@ -2690,18 +2689,23 @@ rb_str_append_literal(VALUE str, VALUE str2) /* * call-seq: - * str << integer -> str - * str.concat(integer) -> str - * str << obj -> str - * str.concat(obj) -> str + * str << integer -> str + * str.concat(integer1, integer2,...) -> str + * str << obj -> str + * str.concat(obj1, obj2,...) -> str * * Append---Concatenates the given object to str. If the object is a * Integer, it is considered as a codepoint, and is converted - * to a character before concatenation. + * to a character before concatenation. Concat can take multiple arguments. + * All the arguments are concatenated in order. * * a = "hello " * a << "world" #=> "hello world" * a.concat(33) #=> "hello world!" + * a #=> "hollo world!" + * + * b = "sn" + * b.concat(b, b) #=> "snsnsn" */ VALUE @@ -2766,26 +2770,61 @@ rb_str_concat(VALUE str1, VALUE str2) return str1; } +static VALUE +rb_str_concat_multi(int argc, VALUE *argv, VALUE str) +{ + int i; + VALUE arg_str = rb_str_new(0, 0); + + str_modifiable(str); + + for (i = 0; i < argc; i++) { + rb_str_concat(arg_str, argv[i]); + } + + rb_str_concat(str, arg_str); + + return str; +} + /* * call-seq: - * str.prepend(other_str) -> str + * str.prepend(other_str1, other_str2,...) -> str * - * Prepend---Prepend the given string to str. + * Prepend---Prepend the given strings to str. * - * a = "world" - * a.prepend("hello ") #=> "hello world" - * a #=> "hello world" + * a = "!" + * a.prepend("hello ", "world") #=> "hello world!" + * a #=> "hello world!" + * + * See also String#concat. */ static VALUE rb_str_prepend(VALUE str, VALUE str2) { StringValue(str2); - StringValue(str); rb_str_update(str, 0L, 0L, str2); return str; } +static VALUE +rb_str_prepend_multi(int argc, VALUE *argv, VALUE str) +{ + int i; + VALUE arg_str = rb_str_new(0, 0); + + StringValue(str); + + for (i = 0; i < argc; i++) { + rb_str_concat(arg_str, argv[i]); + } + + rb_str_prepend(str, arg_str); + + return str; +} + st_index_t rb_str_hash(VALUE str) { @@ -9681,9 +9720,9 @@ Init_String(void) rb_define_method(rb_cString, "codepoints", rb_str_codepoints, 0); rb_define_method(rb_cString, "reverse", rb_str_reverse, 0); rb_define_method(rb_cString, "reverse!", rb_str_reverse_bang, 0); - rb_define_method(rb_cString, "concat", rb_str_concat, 1); + rb_define_method(rb_cString, "concat", rb_str_concat_multi, -1); rb_define_method(rb_cString, "<<", rb_str_concat, 1); - rb_define_method(rb_cString, "prepend", rb_str_prepend, 1); + rb_define_method(rb_cString, "prepend", rb_str_prepend_multi, -1); rb_define_method(rb_cString, "crypt", rb_str_crypt, 1); rb_define_method(rb_cString, "intern", rb_str_intern, 0); /* in symbol.c */ rb_define_method(rb_cString, "to_sym", rb_str_intern, 0); /* in symbol.c */ diff --git test/ruby/test_array.rb test/ruby/test_array.rb index 9364a27..42f155b 100644 --- test/ruby/test_array.rb +++ test/ruby/test_array.rb @@ -554,7 +554,9 @@ def test_compact! def test_concat assert_equal(@cls[1, 2, 3, 4], @cls[1, 2].concat(@cls[3, 4])) assert_equal(@cls[1, 2, 3, 4], @cls[].concat(@cls[1, 2, 3, 4])) + assert_equal(@cls[1, 2, 3, 4], @cls[1].concat(@cls[2, 3], [4])) assert_equal(@cls[1, 2, 3, 4], @cls[1, 2, 3, 4].concat(@cls[])) + assert_equal(@cls[1, 2, 3, 4], @cls[1, 2, 3, 4].concat()) assert_equal(@cls[], @cls[].concat(@cls[])) assert_equal(@cls[@cls[1, 2], @cls[3, 4]], @cls[@cls[1, 2]].concat(@cls[@cls[3, 4]])) @@ -562,6 +564,10 @@ def test_concat a.concat(a) assert_equal([1, 2, 3, 1, 2, 3], a) + b = @cls[4, 5] + b.concat(b, b) + assert_equal([4, 5, 4, 5, 4, 5], b) + assert_raise(TypeError) { [0].concat(:foo) } assert_raise(RuntimeError) { [0].freeze.concat(:foo) } end diff --git test/ruby/test_string.rb test/ruby/test_string.rb index 4df5f4f..567cf43 100644 --- test/ruby/test_string.rb +++ test/ruby/test_string.rb @@ -495,6 +495,8 @@ def test_clone def test_concat assert_equal(S("world!"), S("world").concat(33)) assert_equal(S("world!"), S("world").concat(S('!'))) + b = S("sn") + assert_equal(S("snsnsn"), b.concat(b, b)) bug7090 = '[ruby-core:47751]' result = S("").force_encoding(Encoding::UTF_16LE) @@ -502,6 +504,7 @@ def test_concat expected = S("\u0300".encode(Encoding::UTF_16LE)) assert_equal(expected, result, bug7090) assert_raise(TypeError) { 'foo' << :foo } + assert_raise(RuntimeError) { 'foo'.freeze.concat('bar') } end def test_count @@ -2293,7 +2296,10 @@ def test_dummy_inspect end def test_prepend - assert_equal(S("hello world!"), "world!".prepend("hello ")) + assert_equal(S("hello world!"), "!".prepend("hello ", "world")) + assert_equal(S("!hello world"), "hello world".prepend(33)) + b = S("ue") + assert_equal(S("ueueue"), b.prepend(b, b)) foo = Object.new def foo.to_str