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
/*
* 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);
......
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:
......
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);
string.c
/**********************************************************************
string.c -
$Author$
......
/*
* 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 <i>str</i>. If the object is a
* <code>Integer</code>, 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
......
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 <i>str</i>.
* Prepend---Prepend the given strings to <i>str</i>.
*
* 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)
{
......
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 */
test/ruby/test_array.rb
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]]))
......
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
test/ruby/test_string.rb
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)
......
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
......
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
(3-3/3)