* 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:
parent
f679a6b106
commit
4466d4baa9
@ -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>
|
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
|
* file.c (rb_stat_wr, rb_stat_ww): call get_stat only once and
|
||||||
|
2
NEWS
2
NEWS
@ -132,6 +132,8 @@ with all sufficient information, see the ChangeLog file.
|
|||||||
* String#+@ and String#-@ are added to get mutable/frozen strings.
|
* String#+@ and String#-@ are added to get mutable/frozen strings.
|
||||||
[Feature #11782]
|
[Feature #11782]
|
||||||
|
|
||||||
|
* String.new now accepts new option parameter `encoding'.
|
||||||
|
|
||||||
* Struct
|
* Struct
|
||||||
* Struct#dig [Feature #11688]
|
* Struct#dig [Feature #11688]
|
||||||
|
|
||||||
|
21
string.c
21
string.c
@ -1324,17 +1324,34 @@ rb_str_resurrect(VALUE str)
|
|||||||
/*
|
/*
|
||||||
* call-seq:
|
* call-seq:
|
||||||
* String.new(str="") -> new_str
|
* String.new(str="") -> new_str
|
||||||
|
* String.new(str="", encoding: enc) -> new_str
|
||||||
*
|
*
|
||||||
* Returns a new string object containing a copy of <i>str</i>.
|
* 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
|
static VALUE
|
||||||
rb_str_init(int argc, VALUE *argv, VALUE str)
|
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);
|
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;
|
return str;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -11,12 +11,39 @@ class TestString < Test::Unit::TestCase
|
|||||||
super
|
super
|
||||||
end
|
end
|
||||||
|
|
||||||
def S(str)
|
def S(*args)
|
||||||
@cls.new(str)
|
@cls.new(*args)
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_s_new
|
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
|
end
|
||||||
|
|
||||||
def test_AREF # '[]'
|
def test_AREF # '[]'
|
||||||
|
Loading…
x
Reference in New Issue
Block a user