summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authormatz <matz@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2009-05-25 00:43:23 +0000
committermatz <matz@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2009-05-25 00:43:23 +0000
commit970cfee510c3d536ae5348ecbb4d3d8232d72c20 (patch)
tree5d38c96159a20b86a8007af522a0a3e3df181109
parentd629684bb91e7ea64508cd24629a44d41ff328dd (diff)
* 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
-rw-r--r--ChangeLog10
-rw-r--r--string.c55
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 <matz@ruby-lang.org>
+
+ * 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 <nobu@ruby-lang.org>
* 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
@@ -815,10 +815,36 @@ rb_obj_as_string(VALUE obj)
}
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);
}
/*