@@ -313,6 +313,7 @@ rb_obj_as_string(obj)
313
313
return str ;
314
314
}
315
315
316
+ static VALUE rb_str_s_alloc _ ((VALUE ));
316
317
static VALUE rb_str_replace _ ((VALUE , VALUE ));
317
318
318
319
VALUE
@@ -539,7 +540,20 @@ rb_str_associated(str)
539
540
}
540
541
541
542
static const char null_str [] = "" ;
542
- #define null_str ((char *)null_str)
543
+ #define make_null_str (s ) do { \
544
+ FL_SET(s, ELTS_SHARED); \
545
+ RSTRING(s)->ptr = (char *)null_str; \
546
+ RSTRING(s)->aux.shared = 0; \
547
+ } while (0)
548
+
549
+ static VALUE
550
+ rb_str_s_alloc (klass )
551
+ VALUE klass ;
552
+ {
553
+ VALUE str = str_alloc (klass );
554
+ make_null_str (str );
555
+ return str ;
556
+ }
543
557
544
558
VALUE
545
559
rb_string_value (ptr )
@@ -551,8 +565,7 @@ rb_string_value(ptr)
551
565
* ptr = s ;
552
566
}
553
567
if (!RSTRING (s )-> ptr ) {
554
- FL_SET (s , ELTS_SHARED );
555
- RSTRING (s )-> ptr = null_str ;
568
+ make_null_str (s );
556
569
}
557
570
return s ;
558
571
}
@@ -583,8 +596,7 @@ rb_check_string_type(str)
583
596
{
584
597
str = rb_check_convert_type (str , T_STRING , "String" , "to_str" );
585
598
if (!NIL_P (str ) && !RSTRING (str )-> ptr ) {
586
- FL_SET (str , ELTS_SHARED );
587
- RSTRING (str )-> ptr = null_str ;
599
+ make_null_str (str );
588
600
}
589
601
return str ;
590
602
}
@@ -2308,9 +2320,18 @@ rb_str_replace(str, str2)
2308
2320
RSTRING (str )-> aux .shared = RSTRING (str2 )-> aux .shared ;
2309
2321
}
2310
2322
else {
2311
- rb_str_modify (str );
2312
- rb_str_resize (str , RSTRING (str2 )-> len );
2313
- memcpy (RSTRING (str )-> ptr , RSTRING (str2 )-> ptr , RSTRING (str2 )-> len );
2323
+ if (str_independent (str )) {
2324
+ rb_str_resize (str , RSTRING (str2 )-> len );
2325
+ memcpy (RSTRING (str )-> ptr , RSTRING (str2 )-> ptr , RSTRING (str2 )-> len );
2326
+ if (!RSTRING (str )-> ptr ) {
2327
+ make_null_str (str );
2328
+ }
2329
+ }
2330
+ else {
2331
+ RSTRING (str )-> ptr = RSTRING (str2 )-> ptr ;
2332
+ RSTRING (str )-> len = RSTRING (str2 )-> len ;
2333
+ str_make_independent (str );
2334
+ }
2314
2335
if (FL_TEST (str2 , STR_ASSOC )) {
2315
2336
FL_SET (str , STR_ASSOC );
2316
2337
RSTRING (str )-> aux .shared = RSTRING (str2 )-> aux .shared ;
@@ -4908,7 +4929,7 @@ Init_String()
4908
4929
rb_cString = rb_define_class ("String" , rb_cObject );
4909
4930
rb_include_module (rb_cString , rb_mComparable );
4910
4931
rb_include_module (rb_cString , rb_mEnumerable );
4911
- rb_define_alloc_func (rb_cString , str_alloc );
4932
+ rb_define_alloc_func (rb_cString , rb_str_s_alloc );
4912
4933
rb_define_method (rb_cString , "initialize" , rb_str_init , -1 );
4913
4934
rb_define_method (rb_cString , "initialize_copy" , rb_str_replace , 1 );
4914
4935
rb_define_method (rb_cString , "<=>" , rb_str_cmp_m , 1 );
0 commit comments