ruby-changes:32743
From: ko1 <ko1@a...>
Date: Wed, 5 Feb 2014 11:45:39 +0900 (JST)
Subject: [ruby-changes:32743] ko1:r44822 (trunk): * string.c (rb_str_new_frozen): refactoring code.
ko1 2014-02-05 11:45:29 +0900 (Wed, 05 Feb 2014) New Revision: 44822 http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=44822 Log: * string.c (rb_str_new_frozen): refactoring code. * Move code from str_new_frozen_with_klass() (and remove it) * `aux.shared' should not be 0 for STR_SHARED strings. Modified files: trunk/ChangeLog trunk/string.c Index: ChangeLog =================================================================== --- ChangeLog (revision 44821) +++ ChangeLog (revision 44822) @@ -1,3 +1,9 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1 +Wed Feb 5 11:27:22 2014 Koichi Sasada <ko1@a...> + + * string.c (rb_str_new_frozen): refactoring code. + * Move code from str_new_frozen_with_klass() (and remove it) + * `aux.shared' should not be 0 for STR_SHARED strings. + Wed Feb 5 04:23:41 2014 Aaron Patterson <aaron@t...> * ext/psych/lib/psych.rb: New release of psych. Index: string.c =================================================================== --- string.c (revision 44821) +++ string.c (revision 44822) @@ -798,58 +798,48 @@ rb_str_new_shared(VALUE str) https://github.com/ruby/ruby/blob/trunk/string.c#L798 return str2; } -static VALUE -str_new_frozen_with_klass(VALUE klass, VALUE str) -{ - VALUE str2; - - str2 = str_alloc(klass); - STR_SET_NOEMBED(str2); - RSTRING(str2)->as.heap.len = RSTRING_LEN(str); - RSTRING(str2)->as.heap.ptr = RSTRING_PTR(str); - if (STR_SHARED_P(str)) { - VALUE shared = RSTRING(str)->as.heap.aux.shared; - assert(OBJ_FROZEN(shared)); - STR_SET_SHARED(str2, shared); /* TODO: WB is not needed because str2 is *new* object */ - } - else { - RSTRING(str2)->as.heap.aux.capa = RSTRING(str)->as.heap.aux.capa; - STR_SET_SHARED(str, str2); - } - rb_enc_cr_str_exact_copy(str2, str); - OBJ_INFECT(str2, str); - return str2; -} - VALUE rb_str_new_frozen(VALUE orig) { VALUE klass, str; if (OBJ_FROZEN(orig)) return orig; + klass = rb_obj_class(orig); - if (STR_SHARED_P(orig) && (str = RSTRING(orig)->as.heap.aux.shared)) { - long ofs; - assert(OBJ_FROZEN(str)); - ofs = RSTRING_LEN(str) - RSTRING_LEN(orig); - if ((ofs > 0) || (klass != RBASIC(str)->klass) || - ((RBASIC(str)->flags ^ RBASIC(orig)->flags) & FL_TAINT) || - ENCODING_GET(str) != ENCODING_GET(orig)) { - str = str_new_shared(klass, str); - RSTRING(str)->as.heap.ptr += ofs; - RSTRING(str)->as.heap.len -= ofs; - rb_enc_cr_str_exact_copy(str, orig); - OBJ_INFECT(str, orig); - } - } - else if (STR_EMBED_P(orig)) { + + if (STR_EMBED_P(orig)) { str = str_new(klass, RSTRING_PTR(orig), RSTRING_LEN(orig)); - rb_enc_cr_str_exact_copy(str, orig); - OBJ_INFECT(str, orig); } else { - str = str_new_frozen_with_klass(klass, orig); + if (FL_TEST(orig, STR_SHARED)) { + VALUE shared = RSTRING(orig)->as.heap.aux.shared; + long ofs = RSTRING_LEN(shared) - RSTRING_LEN(orig); + assert(OBJ_FROZEN(shared)); + + if ((ofs > 0) || + (klass != RBASIC(shared)->klass) || + ((RBASIC(shared)->flags ^ RBASIC(orig)->flags) & FL_TAINT) || + ENCODING_GET(shared) != ENCODING_GET(orig)) { + str = str_new_shared(klass, shared); + RSTRING(str)->as.heap.ptr += ofs; + RSTRING(str)->as.heap.len -= ofs; + } + else { + return shared; + } + } + else { + str = str_alloc(klass); + STR_SET_NOEMBED(str); + RSTRING(str)->as.heap.len = RSTRING_LEN(orig); + RSTRING(str)->as.heap.ptr = RSTRING_PTR(orig); + RSTRING(str)->as.heap.aux.capa = RSTRING(orig)->as.heap.aux.capa; + STR_SET_SHARED(orig, str); + } } + + rb_enc_cr_str_exact_copy(str, orig); + OBJ_INFECT(str, orig); OBJ_FREEZE(str); return str; } -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/