1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

* 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
This commit is contained in:
usa 2015-12-08 16:48:52 +00:00
parent f679a6b106
commit 4466d4baa9
4 changed files with 56 additions and 5 deletions

View file

@ -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

2
NEWS
View file

@ -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]

View file

@ -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 (argc > 0 && rb_scan_args(argc, argv, "01", &orig) == 1)
if (!keyword_ids[0])
keyword_ids[0] = rb_intern("encoding");
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;
}

View file

@ -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 # '[]'