summaryrefslogtreecommitdiff
path: root/string.c
diff options
context:
space:
mode:
authorusa <usa@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2015-12-08 16:48:52 +0000
committerusa <usa@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2015-12-08 16:48:52 +0000
commit4466d4baa94bb23f0cfb9aa48252a5ce0b6a1976 (patch)
treed61fc43ac6427095b34d31f6e884ef4303e00b52 /string.c
parentf679a6b10692729dbaa5248f1708e889143ed307 (diff)
* string.c (rb_str_init): now accepts new option parameter `encoding'.
[Feature #11785] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@52976 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'string.c')
-rw-r--r--string.c21
1 files changed, 19 insertions, 2 deletions
diff --git a/string.c b/string.c
index d34359f328..c39e34c1a3 100644
--- a/string.c
+++ b/string.c
@@ -1324,17 +1324,34 @@ rb_str_resurrect(VALUE str)
/*
* call-seq:
* String.new(str="") -> new_str
+ * String.new(str="", encoding: enc) -> new_str
*
* Returns a new string object containing a copy of <i>str</i>.
+ * The optional <i>enc</i> argument specifies the encoding of the new string.
+ * If not specified, the encoding of <i>str</i> (or ASCII-8BIT, if <i>str</i>
+ * is not specified) is used.
*/
static VALUE
rb_str_init(int argc, VALUE *argv, VALUE str)
{
- VALUE orig;
+ static ID keyword_ids[1];
+ VALUE orig, opt, enc;
+ int n;
+
+ if (!keyword_ids[0])
+ keyword_ids[0] = rb_intern("encoding");
- if (argc > 0 && rb_scan_args(argc, argv, "01", &orig) == 1)
+ n = rb_scan_args(argc, argv, "01:", &orig, &opt);
+ if (argc > 0 && n == 1)
rb_str_replace(str, orig);
+ if (!NIL_P(opt)) {
+ rb_get_kwargs(opt, keyword_ids, 0, 1, &enc);
+ if (enc != Qundef && !NIL_P(enc)) {
+ rb_enc_associate(str, rb_to_encoding(enc));
+ ENC_CODERANGE_CLEAR(str);
+ }
+ }
return str;
}