[前][次][番号順一覧][スレッド一覧]

ruby-changes:40897

From: usa <ko1@a...>
Date: Wed, 9 Dec 2015 01:49:13 +0900 (JST)
Subject: [ruby-changes:40897] usa:r52976 (trunk): * string.c (rb_str_init): now accepts new option parameter `encoding'.

usa	2015-12-09 01:48:52 +0900 (Wed, 09 Dec 2015)

  New Revision: 52976

  http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=52976

  Log:
    * string.c (rb_str_init): now accepts new option parameter `encoding'.
      [Feature #11785]

  Modified files:
    trunk/ChangeLog
    trunk/NEWS
    trunk/string.c
    trunk/test/ruby/test_string.rb
Index: ChangeLog
===================================================================
--- ChangeLog	(revision 52975)
+++ ChangeLog	(revision 52976)
@@ -1,3 +1,8 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1
+Wed Dec  9 01:46:35 2015  NAKAMURA Usaku  <usa@r...>
+
+	* string.c (rb_str_init): now accepts new option parameter `encoding'.
+	  [Feature #11785]
+
 Wed Dec  9 00:52:37 2015  Nobuyoshi Nakada  <nobu@r...>
 
 	* file.c (rb_stat_wr, rb_stat_ww): call get_stat only once and
Index: string.c
===================================================================
--- string.c	(revision 52975)
+++ string.c	(revision 52976)
@@ -1324,17 +1324,34 @@ rb_str_resurrect(VALUE str) https://github.com/ruby/ruby/blob/trunk/string.c#L1324
 /*
  *  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;
 }
 
Index: NEWS
===================================================================
--- NEWS	(revision 52975)
+++ NEWS	(revision 52976)
@@ -132,6 +132,8 @@ with all sufficient information, see the https://github.com/ruby/ruby/blob/trunk/NEWS#L132
   * 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]
 
Index: test/ruby/test_string.rb
===================================================================
--- test/ruby/test_string.rb	(revision 52975)
+++ test/ruby/test_string.rb	(revision 52976)
@@ -11,12 +11,39 @@ class TestString < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_string.rb#L11
     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 # '[]'

--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]