initialize_copy(p1)
public
Replaces the contents and taintedness of str with the
corresponding values in other_str.
s = "hello"
s.replace "world"
Show source
/*
* call-seq:
* str.replace(other_str) => str
*
* Replaces the contents and taintedness of <i>str</i> with the corresponding
* values in <i>other_str</i>.
*
* s = "hello" #=> "hello"
* s.replace "world" #=> "world"
*/
static VALUE
rb_str_replace(str, str2)
VALUE str, str2;
{
if (str == str2) return str;
StringValue(str2);
if (FL_TEST(str2, ELTS_SHARED)) {
if (str_independent(str)) {
free(RSTRING(str)->ptr);
}
RSTRING(str)->len = RSTRING(str2)->len;
RSTRING(str)->ptr = RSTRING(str2)->ptr;
FL_SET(str, ELTS_SHARED);
FL_UNSET(str, STR_ASSOC);
RSTRING(str)->aux.shared = RSTRING(str2)->aux.shared;
}
else {
if (str_independent(str)) {
rb_str_resize(str, RSTRING(str2)->len);
memcpy(RSTRING(str)->ptr, RSTRING(str2)->ptr, RSTRING(str2)->len);
if (!RSTRING(str)->ptr) {
make_null_str(str);
}
}
else {
RSTRING(str)->ptr = RSTRING(str2)->ptr;
RSTRING(str)->len = RSTRING(str2)->len;
str_make_independent(str);
}
if (FL_TEST(str2, STR_ASSOC)) {
FL_SET(str, STR_ASSOC);
RSTRING(str)->aux.shared = RSTRING(str2)->aux.shared;
}
}
OBJ_INFECT(str, str2);
return str;
}