diff options
author | nobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2009-02-17 03:50:59 +0000 |
---|---|---|
committer | nobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2009-02-17 03:50:59 +0000 |
commit | 62dd7a00fcef20ef5c74007bc93f8851e002a449 (patch) | |
tree | 8de7e32ddb3a7516b704de0bd7a6eb2d1dca1abd /string.c | |
parent | c9e4dc308f51122e0401756ac3aa4972d0b4ad32 (diff) |
* string.c (rb_str_shared_replace): no need for
str_make_independent.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@22368 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'string.c')
-rw-r--r-- | string.c | 27 |
1 files changed, 16 insertions, 11 deletions
@@ -759,6 +759,8 @@ rb_str_to_str(VALUE str) return rb_convert_type(str, T_STRING, "String", "to_str"); } +static inline void str_discard(VALUE str); + void rb_str_shared_replace(VALUE str, VALUE str2) { @@ -767,11 +769,8 @@ rb_str_shared_replace(VALUE str, VALUE str2) if (str == str2) return; enc = STR_ENC_GET(str2); cr = ENC_CODERANGE(str2); - rb_str_modify(str); + str_discard(str); OBJ_INFECT(str, str2); - if (!STR_SHARED_P(str) && !STR_EMBED_P(str)) { - xfree(RSTRING_PTR(str)); - } if (RSTRING_LEN(str2) <= RSTRING_EMBED_LEN_MAX) { STR_SET_EMBED(str); memcpy(RSTRING_PTR(str), RSTRING_PTR(str2), RSTRING_LEN(str2)+1); @@ -1194,6 +1193,17 @@ str_modify_keep_cr(VALUE str) ENC_CODERANGE_CLEAR(str); } +static inline void +str_discard(VALUE str) +{ + str_modifiable(str); + if (!STR_SHARED_P(str) && !STR_EMBED_P(str)) { + xfree(RSTRING_PTR(str)); + RSTRING(str)->as.heap.ptr = 0; + RSTRING(str)->as.heap.len = 0; + } +} + void rb_str_associate(VALUE str, VALUE add) { @@ -3789,9 +3799,7 @@ rb_str_replace(VALUE str, VALUE str2) if (STR_ASSOC_P(str2)) { str2 = rb_str_new4(str2); } - if (str_independent(str) && !STR_EMBED_P(str)) { - xfree(RSTRING_PTR(str)); - } + str_discard(str); if (STR_SHARED_P(str2)) { STR_SET_NOEMBED(str); RSTRING(str)->as.heap.len = len; @@ -3822,10 +3830,7 @@ rb_str_replace(VALUE str, VALUE str2) static VALUE rb_str_clear(VALUE str) { - /* rb_str_modify() */ /* no need for str_make_independent */ - if (str_independent(str) && !STR_EMBED_P(str)) { - xfree(RSTRING_PTR(str)); - } + str_discard(str); STR_SET_EMBED(str); STR_SET_EMBED_LEN(str, 0); RSTRING_PTR(str)[0] = 0; |