From 970cfee510c3d536ae5348ecbb4d3d8232d72c20 Mon Sep 17 00:00:00 2001 From: matz Date: Mon, 25 May 2009 00:43:23 +0000 Subject: [PATCH] * string.c (rb_str_hash): avoid calling rb_enc_str_asciionly_p(). * string.c (rb_str_replace): avoid redundant calling rb_str_new4(). * string.c (str_replace): factor out replacement from rb_str_replace() without type check nor discarding the destination contents. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@23565 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- ChangeLog | 10 ++++++++++ string.c | 55 +++++++++++++++++++++++++++++++------------------------ 2 files changed, 41 insertions(+), 24 deletions(-) diff --git a/ChangeLog b/ChangeLog index b5ce069c17..4358718939 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,13 @@ +Mon May 25 09:34:09 2009 Yukihiro Matsumoto + + * string.c (rb_str_hash): avoid calling rb_enc_str_asciionly_p(). + + * string.c (rb_str_replace): avoid redundant calling rb_str_new4(). + + * string.c (str_replace): factor out replacement from + rb_str_replace() without type check nor discarding the + destination contents. + Mon May 25 08:06:52 2009 Nobuyoshi Nakada * string.c (rb_str_partition): should use the converted result. a diff --git a/string.c b/string.c index c75bb1003c..573c5cafc7 100644 --- a/string.c +++ b/string.c @@ -814,11 +814,37 @@ rb_obj_as_string(VALUE obj) return str; } +static VALUE +str_replace(VALUE str, VALUE str2) +{ + long len; + + len = RSTRING_LEN(str2); + if (STR_ASSOC_P(str2)) { + str2 = rb_str_new4(str2); + } + if (STR_SHARED_P(str2)) { + STR_SET_NOEMBED(str); + RSTRING(str)->as.heap.len = len; + RSTRING(str)->as.heap.ptr = RSTRING_PTR(str2); + FL_SET(str, ELTS_SHARED); + FL_UNSET(str, STR_ASSOC); + RSTRING(str)->as.heap.aux.shared = RSTRING(str2)->as.heap.aux.shared; + } + else { + str_replace_shared(str, str2); + } + + OBJ_INFECT(str, str2); + rb_enc_cr_str_exact_copy(str, str2); + return str; +} + static VALUE str_duplicate(VALUE klass, VALUE str) { VALUE dup = str_alloc(klass); - rb_str_replace(dup, str); + str_replace(dup, str); return dup; } @@ -831,7 +857,7 @@ rb_str_dup(VALUE str) VALUE rb_str_resurrect(VALUE str) { - return rb_str_replace(str_alloc(rb_cString), str); + return str_replace(str_alloc(rb_cString), str); } /* @@ -2179,8 +2205,8 @@ int rb_str_hash(VALUE str) { int e = ENCODING_GET(str); - if (e) { - if (rb_enc_str_asciionly_p(str)) e = 0; + if (e && rb_enc_str_coderange(str) == ENC_CODERANGE_7BIT) { + e = 0; } return (int)rb_memhash((const void *)RSTRING_PTR(str), RSTRING_LEN(str)) ^ e; } @@ -3840,30 +3866,11 @@ rb_str_gsub(int argc, VALUE *argv, VALUE str) VALUE rb_str_replace(VALUE str, VALUE str2) { - long len; if (str == str2) return str; StringValue(str2); - len = RSTRING_LEN(str2); - if (STR_ASSOC_P(str2)) { - str2 = rb_str_new4(str2); - } str_discard(str); - if (STR_SHARED_P(str2)) { - STR_SET_NOEMBED(str); - RSTRING(str)->as.heap.len = len; - RSTRING(str)->as.heap.ptr = RSTRING_PTR(str2); - FL_SET(str, ELTS_SHARED); - FL_UNSET(str, STR_ASSOC); - RSTRING(str)->as.heap.aux.shared = RSTRING(str2)->as.heap.aux.shared; - } - else { - str_replace_shared(str, rb_str_new4(str2)); - } - - OBJ_INFECT(str, str2); - rb_enc_cr_str_exact_copy(str, str2); - return str; + return str_replace(str, str2); } /*