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

ruby-changes:30768

From: charliesome <ko1@a...>
Date: Thu, 5 Sep 2013 18:08:00 +0900 (JST)
Subject: [ruby-changes:30768] charliesome:r42847 (trunk): * string.c (fstring_cmp): take string encoding into account when

charliesome	2013-09-05 18:07:48 +0900 (Thu, 05 Sep 2013)

  New Revision: 42847

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

  Log:
    * string.c (fstring_cmp): take string encoding into account when
      comparing fstrings [ruby-core:57037] [Bug #8866]
    
    * test/ruby/test_string.rb: add test

  Modified files:
    trunk/ChangeLog
    trunk/string.c
    trunk/test/ruby/test_string.rb
Index: ChangeLog
===================================================================
--- ChangeLog	(revision 42846)
+++ ChangeLog	(revision 42847)
@@ -1,3 +1,10 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1
+Thu Sep  5 18:05:00 2013  Charlie Somerville  <charliesome@r...>
+
+	* string.c (fstring_cmp): take string encoding into account when
+	  comparing fstrings [ruby-core:57037] [Bug #8866]
+
+	* test/ruby/test_string.rb: add test
+
 Thu Sep  5 17:25:49 2013  Nobuyoshi Nakada  <nobu@r...>
 
 	* string.c (rb_fstring, rb_str_free): use st_data_t instead of VALUE.
Index: string.c
===================================================================
--- string.c	(revision 42846)
+++ string.c	(revision 42847)
@@ -131,11 +131,13 @@ VALUE rb_cSymbol; https://github.com/ruby/ruby/blob/trunk/string.c#L131
 
 #define STR_ENC_GET(str) rb_enc_from_index(ENCODING_GET(str))
 
+static int fstring_cmp(VALUE a, VALUE b);
+
 static st_table* frozen_strings;
 
 static const struct st_hash_type fstring_hash_type = {
-    rb_str_cmp,
-    rb_str_hash
+    fstring_cmp,
+    rb_str_hash,
 };
 
 VALUE
@@ -153,6 +155,16 @@ rb_fstring(VALUE str) https://github.com/ruby/ruby/blob/trunk/string.c#L155
     return str;
 }
 
+static int
+fstring_cmp(VALUE a, VALUE b)
+{
+    int cmp = rb_str_hash_cmp(a, b);
+    if (cmp != 0) {
+	return cmp;
+    }
+    return ENCODING_GET(b) - ENCODING_GET(a);
+}
+
 static inline int
 single_byte_optimizable(VALUE str)
 {
Index: test/ruby/test_string.rb
===================================================================
--- test/ruby/test_string.rb	(revision 42846)
+++ test/ruby/test_string.rb	(revision 42847)
@@ -2249,6 +2249,13 @@ class TestString < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_string.rb#L2249
     b = "hello"f
     assert_equal a.object_id, b.object_id
   end
+
+  def test_frozen_strings_are_deduplicated_with_encoding
+    a = eval("# coding: utf-8\n'hello'f")
+    b = eval("# coding: ascii\n'hello'f")
+    assert_equal Encoding::UTF_8, a.encoding
+    assert_equal Encoding::US_ASCII, b.encoding
+  end
 end
 
 class TestString2 < TestString

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

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