From c9524768d4e6770721cee6aaab571861b2c658df Mon Sep 17 00:00:00 2001 From: nobu Date: Tue, 17 Feb 2009 03:50:59 +0000 Subject: * string.c (rb_str_shared_replace): no need for str_make_independent. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/branches/ruby_1_8@22368 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- string.c | 23 +++++++++++++++-------- 1 file changed, 15 insertions(+), 8 deletions(-) (limited to 'string.c') diff --git a/string.c b/string.c index a69972198f..ddaa6922b3 100644 --- a/string.c +++ b/string.c @@ -264,13 +264,14 @@ rb_str_to_str(str) return rb_convert_type(str, T_STRING, "String", "to_str"); } +static inline void str_discard _((VALUE str)); + static void rb_str_shared_replace(str, str2) VALUE str, str2; { if (str == str2) return; - rb_str_modify(str); - if (!FL_TEST(str, ELTS_SHARED)) free(RSTRING(str)->ptr); + str_discard(str); if (NIL_P(str2)) { RSTRING(str)->ptr = 0; RSTRING(str)->len = 0; @@ -512,6 +513,16 @@ rb_str_modify(str) str_make_independent(str); } +static inline void +str_discard(VALUE str) +{ + if (str_independent(str)) { + xfree(RSTRING_PTR(str)); + RSTRING(str)->ptr = 0; + RSTRING(str)->len = 0; + } +} + void rb_str_associate(str, add) VALUE str, add; @@ -2212,9 +2223,7 @@ str_gsub(argc, argv, str, bang) *bp = '\0'; rb_str_unlocktmp(dest); if (bang) { - if (str_independent(str)) { - free(RSTRING(str)->ptr); - } + str_discard(str); FL_UNSET(str, STR_NOCAPA); RSTRING(str)->ptr = buf; RSTRING(str)->aux.capa = blen; @@ -2312,9 +2321,7 @@ rb_str_replace(str, str2) StringValue(str2); if (FL_TEST(str2, ELTS_SHARED)) { - if (str_independent(str)) { - free(RSTRING(str)->ptr); - } + str_discard(str); RSTRING(str)->len = RSTRING(str2)->len; RSTRING(str)->ptr = RSTRING(str2)->ptr; FL_SET(str, ELTS_SHARED); -- cgit v1.2.3