summaryrefslogtreecommitdiff
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
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
-rw-r--r--ChangeLog5
-rw-r--r--NEWS2
-rw-r--r--string.c21
-rw-r--r--test/ruby/test_string.rb33
4 files changed, 56 insertions, 5 deletions
diff --git a/ChangeLog b/ChangeLog
index 64de792d03..132374aef1 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,8 @@
+Wed Dec 9 01:46:35 2015 NAKAMURA Usaku <usa@ruby-lang.org>
+
+ * string.c (rb_str_init): now accepts new option parameter `encoding'.
+ [Feature #11785]
+
Wed Dec 9 00:52:37 2015 Nobuyoshi Nakada <nobu@ruby-lang.org>
* file.c (rb_stat_wr, rb_stat_ww): call get_stat only once and
diff --git a/NEWS b/NEWS
index 80bd3cd683..5dd370ad39 100644
--- a/NEWS
+++ b/NEWS
@@ -132,6 +132,8 @@ with all sufficient information, see the ChangeLog file.
* String#+@ and String#-@ are added to get mutable/frozen strings.
[Feature #11782]
+ * String.new now accepts new option parameter `encoding'.
+
* Struct
* Struct#dig [Feature #11688]
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;
}
diff --git a/test/ruby/test_string.rb b/test/ruby/test_string.rb
index 824c0e6b1c..eed7c69a02 100644
--- a/test/ruby/test_string.rb
+++ b/test/ruby/test_string.rb
@@ -11,12 +11,39 @@ class TestString < Test::Unit::TestCase
super
end
- def S(str)
- @cls.new(str)
+ def S(*args)
+ @cls.new(*args)
end
def test_s_new
- assert_equal("RUBY", S("RUBY"))
+ assert_equal("", S())
+ assert_equal(Encoding::ASCII_8BIT, S().encoding)
+
+ assert_equal("", S(""))
+ assert_equal(__ENCODING__, S("").encoding)
+
+ src = "RUBY"
+ assert_equal(src, S(src))
+ assert_equal(__ENCODING__, S(src).encoding)
+
+ src.force_encoding("euc-jp")
+ assert_equal(src, S(src))
+ assert_equal(Encoding::EUC_JP, S(src).encoding)
+
+
+ assert_equal("", S(encoding: "euc-jp"))
+ assert_equal(Encoding::EUC_JP, S(encoding: "euc-jp").encoding)
+
+ assert_equal("", S("", encoding: "euc-jp"))
+ assert_equal(Encoding::EUC_JP, S("", encoding: "euc-jp").encoding)
+
+ src = "RUBY"
+ assert_equal(src, S(src, encoding: "euc-jp"))
+ assert_equal(Encoding::EUC_JP, S(src, encoding: "euc-jp").encoding)
+
+ src.force_encoding("euc-jp")
+ assert_equal(src, S(src, encoding: "utf-8"))
+ assert_equal(Encoding::UTF_8, S(src, encoding: "utf-8").encoding)
end
def test_AREF # '[]'