1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

* 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
This commit is contained in:
matz 2009-05-25 00:43:23 +00:00
parent d629684bb9
commit 970cfee510
2 changed files with 41 additions and 24 deletions

View file

@ -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

View file

@ -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);
}
/*