diff options
author | nobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2016-08-27 01:26:17 +0000 |
---|---|---|
committer | nobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2016-08-27 01:26:17 +0000 |
commit | 9387ff7315b498a6e7c8ab2a4b1582fd6c717524 (patch) | |
tree | 0431117437708b8bd35ae46d86b0a6146de870fd /string.c | |
parent | 0783f55cf8b361ede75d7c7b86d82fe1fbbff107 (diff) |
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
Diffstat (limited to 'string.c')
-rw-r--r-- | string.c | 66 |
1 files changed, 50 insertions, 16 deletions
@@ -2798,20 +2798,43 @@ rb_str_concat_literals(size_t num, const VALUE *strary) /* * 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 an * <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" */ +static VALUE +rb_str_concat_multi(int argc, VALUE *argv, VALUE str) +{ + str_modifiable(str); + + if (argc > 0) { + int i; + VALUE arg_str = rb_str_tmp_new(0); + rb_enc_copy(arg_str, str); + for (i = 0; i < argc; i++) { + rb_str_concat(arg_str, argv[i]); + } + rb_str_buf_append(str, arg_str); + } + + return str; +} + VALUE rb_str_concat(VALUE str1, VALUE str2) { @@ -2878,21 +2901,32 @@ rb_str_concat(VALUE str1, VALUE str2) /* * call-seq: - * str.prepend(other_str) -> str + * str.prepend(other_str1, other_str2,...) -> str + * + * Prepend---Prepend the given strings to <i>str</i>. * - * Prepend---Prepend the given string to <i>str</i>. + * a = "!" + * a.prepend("hello ", "world") #=> "hello world!" + * a #=> "hello world!" * - * a = "world" - * a.prepend("hello ") #=> "hello world" - * a #=> "hello world" + * See also String#concat. */ static VALUE -rb_str_prepend(VALUE str, VALUE str2) +rb_str_prepend_multi(int argc, VALUE *argv, VALUE str) { - StringValue(str2); - StringValue(str); - rb_str_update(str, 0L, 0L, str2); + str_modifiable(str); + + if (argc > 0) { + int i; + VALUE arg_str = rb_str_tmp_new(0); + rb_enc_copy(arg_str, str); + for (i = 0; i < argc; i++) { + rb_str_append(arg_str, argv[i]); + } + rb_str_update(str, 0L, 0L, arg_str); + } + return str; } @@ -9826,9 +9860,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 */ |