ruby-changes:47627
From: nobu <ko1@a...>
Date: Mon, 4 Sep 2017 23:04:58 +0900 (JST)
Subject: [ruby-changes:47627] nobu:r59743 (trunk): string.c: grapheme clusters on frozen string
nobu 2017-09-04 23:04:54 +0900 (Mon, 04 Sep 2017) New Revision: 59743 https://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=59743 Log: string.c: grapheme clusters on frozen string * string.c (rb_str_enumerate_grapheme_clusters): enumerate on shared frozen string. Modified files: trunk/string.c trunk/test/ruby/test_string.rb Index: string.c =================================================================== --- string.c (revision 59742) +++ string.c (revision 59743) @@ -8019,6 +8019,7 @@ rb_str_codepoints(VALUE str) https://github.com/ruby/ruby/blob/trunk/string.c#L8019 static VALUE rb_str_enumerate_grapheme_clusters(VALUE str, VALUE ary) { + VALUE orig = str; regex_t *reg_grapheme_cluster = NULL; static regex_t *reg_grapheme_cluster_utf8 = NULL; int encidx = ENCODING_GET(str); @@ -8046,6 +8047,7 @@ rb_str_enumerate_grapheme_clusters(VALUE https://github.com/ruby/ruby/blob/trunk/string.c#L8047 } } + if (!ary) str = rb_str_new_frozen(str); ptr = RSTRING_PTR(str); end = RSTRING_END(str); @@ -8060,10 +8062,11 @@ rb_str_enumerate_grapheme_clusters(VALUE https://github.com/ruby/ruby/blob/trunk/string.c#L8062 ENUM_ELEM(ary, rb_enc_str_new(ptr, len, enc)); ptr += len; } + RB_GC_GUARD(str); if (ary) return ary; else - return str; + return orig; } /* Index: test/ruby/test_string.rb =================================================================== --- test/ruby/test_string.rb (revision 59742) +++ test/ruby/test_string.rb (revision 59743) @@ -919,6 +919,8 @@ CODE https://github.com/ruby/ruby/blob/trunk/test/ruby/test_string.rb#L919 assert_equal ["\u000A", "\u0308"], "\u{a 308}".each_grapheme_cluster.to_a assert_equal ["\u000D", "\u0308"], "\u{d 308}".each_grapheme_cluster.to_a assert_equal ["a", "b", "c"], "abc".b.each_grapheme_cluster.to_a + s = ("x"+"\u{10ABCD}"*250000) + assert_empty(s.each_grapheme_cluster {s.clear}) end def test_grapheme_clusters -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/