Project

General

Profile

Feature #12333 ยป multi_concat_prepend.patch

diff from: 0052ed9d806f3739df9200891a376a18dc8068f9 - spinute (Satoru Horie), 06/20/2016 03:31 PM

View differences:

array.c
3620 3620

  
3621 3621
/*
3622 3622
 *  call-seq:
3623
 *     ary.concat(other_ary)   -> ary
3623
 *     ary.concat(other_ary1, other_ary2,...)   -> ary
3624 3624
 *
3625
 *  Appends the elements of +other_ary+ to +self+.
3625
 *  Appends the elements of +other_ary+s to +self+.
3626 3626
 *
3627 3627
 *     [ "a", "b" ].concat( ["c", "d"] ) #=> [ "a", "b", "c", "d" ]
3628
 *     [ "a" ].concat( ["b"], ["c", "d"] ) #=> [ "a", "b", "c", "d" ]
3629
 *     [ "a" ].concat #=> [ "a" ]
3630
 *
3628 3631
 *     a = [ 1, 2, 3 ]
3629 3632
 *     a.concat( [ 4, 5 ] )
3630 3633
 *     a                                 #=> [ 1, 2, 3, 4, 5 ]
3631 3634
 *
3635
 *     a = [ 1, 2 ]
3636
 *     a.concat(a, a)                    #=> [1, 2, 1, 2, 1, 2]
3637
 *
3632 3638
 *  See also Array#+.
3633 3639
 */
3634 3640

  
3635 3641
VALUE
3636 3642
rb_ary_concat(VALUE x, VALUE y)
3637 3643
{
3638
    rb_ary_modify_check(x);
3639 3644
    y = to_ary(y);
3640 3645
    if (RARRAY_LEN(y) > 0) {
3641 3646
	rb_ary_splice(x, RARRAY_LEN(x), 0, y);
......
3643 3648
    return x;
3644 3649
}
3645 3650

  
3651
static VALUE
3652
rb_ary_concat_multi(int argc, VALUE *argv, VALUE ary)
3653
{
3654
    int i;
3655
    VALUE args = rb_ary_new();
3656

  
3657
    rb_ary_modify_check(ary);
3658

  
3659
    for (i = 0; i < argc; i++) {
3660
	rb_ary_concat(args, argv[i]);
3661
    }
3662

  
3663
    rb_ary_concat(ary, args);
3664

  
3665
    return ary;
3666
}
3667

  
3646 3668

  
3647 3669
/*
3648 3670
 *  call-seq:
......
5810 5832
    rb_define_method(rb_cArray, "fetch", rb_ary_fetch, -1);
5811 5833
    rb_define_method(rb_cArray, "first", rb_ary_first, -1);
5812 5834
    rb_define_method(rb_cArray, "last", rb_ary_last, -1);
5813
    rb_define_method(rb_cArray, "concat", rb_ary_concat, 1);
5835
    rb_define_method(rb_cArray, "concat", rb_ary_concat_multi, -1);
5814 5836
    rb_define_method(rb_cArray, "<<", rb_ary_push, 1);
5815 5837
    rb_define_method(rb_cArray, "push", rb_ary_push_m, -1);
5816 5838
    rb_define_method(rb_cArray, "pop", rb_ary_pop_m, -1);
string.c
1 1
/**********************************************************************
2

  
3 2
  string.c -
4 3

  
5 4
  $Author$
......
2690 2689

  
2691 2690
/*
2692 2691
 *  call-seq:
2693
 *     str << integer       -> str
2694
 *     str.concat(integer)  -> str
2695
 *     str << obj           -> str
2696
 *     str.concat(obj)      -> str
2692
 *     str << integer                      -> str
2693
 *     str.concat(integer1, integer2,...)  -> str
2694
 *     str << obj                          -> str
2695
 *     str.concat(obj1, obj2,...)          -> str
2697 2696
 *
2698 2697
 *  Append---Concatenates the given object to <i>str</i>. If the object is a
2699 2698
 *  <code>Integer</code>, it is considered as a codepoint, and is converted
2700
 *  to a character before concatenation.
2699
 *  to a character before concatenation. Concat can take multiple arguments.
2700
 *  All the arguments are concatenated in order.
2701 2701
 *
2702 2702
 *     a = "hello "
2703 2703
 *     a << "world"   #=> "hello world"
2704 2704
 *     a.concat(33)   #=> "hello world!"
2705
 *     a              #=> "hollo world!"
2706
 *
2707
 *     b = "sn"
2708
 *     b.concat(b, b)    #=> "snsnsn"
2705 2709
 */
2706 2710

  
2707 2711
VALUE
......
2766 2770
    return str1;
2767 2771
}
2768 2772

  
2773
static VALUE
2774
rb_str_concat_multi(int argc, VALUE *argv, VALUE str)
2775
{
2776
    int i;
2777
    VALUE arg_str = rb_str_new(0, 0);
2778

  
2779
    str_modifiable(str);
2780

  
2781
    for (i = 0; i < argc; i++) {
2782
	rb_str_concat(arg_str, argv[i]);
2783
    }
2784

  
2785
    rb_str_concat(str, arg_str);
2786

  
2787
    return str;
2788
}
2789

  
2769 2790
/*
2770 2791
 *  call-seq:
2771
 *     str.prepend(other_str)  -> str
2792
 *     str.prepend(other_str1, other_str2,...)  -> str
2772 2793
 *
2773
 *  Prepend---Prepend the given string to <i>str</i>.
2794
 *  Prepend---Prepend the given strings to <i>str</i>.
2774 2795
 *
2775
 *     a = "world"
2776
 *     a.prepend("hello ") #=> "hello world"
2777
 *     a                   #=> "hello world"
2796
 *     a = "!"
2797
 *     a.prepend("hello ", "world") #=> "hello world!"
2798
 *     a                            #=> "hello world!"
2799
 *
2800
 *  See also String#concat.
2778 2801
 */
2779 2802

  
2780 2803
static VALUE
2781 2804
rb_str_prepend(VALUE str, VALUE str2)
2782 2805
{
2783 2806
    StringValue(str2);
2784
    StringValue(str);
2785 2807
    rb_str_update(str, 0L, 0L, str2);
2786 2808
    return str;
2787 2809
}
2788 2810

  
2811
static VALUE
2812
rb_str_prepend_multi(int argc, VALUE *argv, VALUE str)
2813
{
2814
    int i;
2815
    VALUE arg_str = rb_str_new(0, 0);
2816

  
2817
    StringValue(str);
2818

  
2819
    for (i = 0; i < argc; i++) {
2820
	rb_str_concat(arg_str, argv[i]);
2821
    }
2822

  
2823
    rb_str_prepend(str, arg_str);
2824

  
2825
    return str;
2826
}
2827

  
2789 2828
st_index_t
2790 2829
rb_str_hash(VALUE str)
2791 2830
{
......
9681 9720
    rb_define_method(rb_cString, "codepoints", rb_str_codepoints, 0);
9682 9721
    rb_define_method(rb_cString, "reverse", rb_str_reverse, 0);
9683 9722
    rb_define_method(rb_cString, "reverse!", rb_str_reverse_bang, 0);
9684
    rb_define_method(rb_cString, "concat", rb_str_concat, 1);
9723
    rb_define_method(rb_cString, "concat", rb_str_concat_multi, -1);
9685 9724
    rb_define_method(rb_cString, "<<", rb_str_concat, 1);
9686
    rb_define_method(rb_cString, "prepend", rb_str_prepend, 1);
9725
    rb_define_method(rb_cString, "prepend", rb_str_prepend_multi, -1);
9687 9726
    rb_define_method(rb_cString, "crypt", rb_str_crypt, 1);
9688 9727
    rb_define_method(rb_cString, "intern", rb_str_intern, 0); /* in symbol.c */
9689 9728
    rb_define_method(rb_cString, "to_sym", rb_str_intern, 0); /* in symbol.c */
test/ruby/test_array.rb
554 554
  def test_concat
555 555
    assert_equal(@cls[1, 2, 3, 4],     @cls[1, 2].concat(@cls[3, 4]))
556 556
    assert_equal(@cls[1, 2, 3, 4],     @cls[].concat(@cls[1, 2, 3, 4]))
557
    assert_equal(@cls[1, 2, 3, 4],     @cls[1].concat(@cls[2, 3], [4]))
557 558
    assert_equal(@cls[1, 2, 3, 4],     @cls[1, 2, 3, 4].concat(@cls[]))
559
    assert_equal(@cls[1, 2, 3, 4],     @cls[1, 2, 3, 4].concat())
558 560
    assert_equal(@cls[],               @cls[].concat(@cls[]))
559 561
    assert_equal(@cls[@cls[1, 2], @cls[3, 4]], @cls[@cls[1, 2]].concat(@cls[@cls[3, 4]]))
560 562

  
......
562 564
    a.concat(a)
563 565
    assert_equal([1, 2, 3, 1, 2, 3], a)
564 566

  
567
    b = @cls[4, 5]
568
    b.concat(b, b)
569
    assert_equal([4, 5, 4, 5, 4, 5], b)
570

  
565 571
    assert_raise(TypeError) { [0].concat(:foo) }
566 572
    assert_raise(RuntimeError) { [0].freeze.concat(:foo) }
567 573
  end
test/ruby/test_string.rb
495 495
  def test_concat
496 496
    assert_equal(S("world!"), S("world").concat(33))
497 497
    assert_equal(S("world!"), S("world").concat(S('!')))
498
    b = S("sn")
499
    assert_equal(S("snsnsn"), b.concat(b, b))
498 500

  
499 501
    bug7090 = '[ruby-core:47751]'
500 502
    result = S("").force_encoding(Encoding::UTF_16LE)
......
502 504
    expected = S("\u0300".encode(Encoding::UTF_16LE))
503 505
    assert_equal(expected, result, bug7090)
504 506
    assert_raise(TypeError) { 'foo' << :foo }
507
    assert_raise(RuntimeError) { 'foo'.freeze.concat('bar') }
505 508
  end
506 509

  
507 510
  def test_count
......
2293 2296
  end
2294 2297

  
2295 2298
  def test_prepend
2296
    assert_equal(S("hello world!"), "world!".prepend("hello "))
2299
    assert_equal(S("hello world!"), "!".prepend("hello ", "world"))
2300
    assert_equal(S("!hello world"), "hello world".prepend(33))
2301
    b = S("ue")
2302
    assert_equal(S("ueueue"), b.prepend(b, b))
2297 2303

  
2298 2304
    foo = Object.new
2299 2305
    def foo.to_str