@@ -306,6 +306,7 @@ rb_obj_as_string(obj)
306
306
return str ;
307
307
}
308
308
309
+ static VALUE rb_str_s_alloc _ ((VALUE ));
309
310
static VALUE rb_str_replace _ ((VALUE , VALUE ));
310
311
311
312
VALUE
@@ -531,7 +532,21 @@ rb_str_associated(str)
531
532
return Qfalse ;
532
533
}
533
534
534
- static char * null_str = "" ;
535
+ static const char null_str [] = "" ;
536
+ #define make_null_str (s ) do { \
537
+ FL_SET(s, ELTS_SHARED); \
538
+ RSTRING(s)->ptr = (char *)null_str; \
539
+ RSTRING(s)->aux.shared = 0; \
540
+ } while (0)
541
+
542
+ static VALUE
543
+ rb_str_s_alloc (klass )
544
+ VALUE klass ;
545
+ {
546
+ VALUE str = str_alloc (klass );
547
+ make_null_str (str );
548
+ return str ;
549
+ }
535
550
536
551
VALUE
537
552
rb_string_value (ptr )
@@ -543,8 +558,7 @@ rb_string_value(ptr)
543
558
* ptr = s ;
544
559
}
545
560
if (!RSTRING (s )-> ptr ) {
546
- FL_SET (s , ELTS_SHARED );
547
- RSTRING (s )-> ptr = null_str ;
561
+ make_null_str (s );
548
562
}
549
563
return s ;
550
564
}
@@ -575,8 +589,7 @@ rb_check_string_type(str)
575
589
{
576
590
str = rb_check_convert_type (str , T_STRING , "String" , "to_str" );
577
591
if (!NIL_P (str ) && !RSTRING (str )-> ptr ) {
578
- FL_SET (str , ELTS_SHARED );
579
- RSTRING (str )-> ptr = null_str ;
592
+ make_null_str (str );
580
593
}
581
594
return str ;
582
595
}
@@ -2256,9 +2269,18 @@ rb_str_replace(str, str2)
2256
2269
RSTRING (str )-> aux .shared = RSTRING (str2 )-> aux .shared ;
2257
2270
}
2258
2271
else {
2259
- rb_str_modify (str );
2260
- rb_str_resize (str , RSTRING (str2 )-> len );
2261
- memcpy (RSTRING (str )-> ptr , RSTRING (str2 )-> ptr , RSTRING (str2 )-> len );
2272
+ if (str_independent (str )) {
2273
+ rb_str_resize (str , RSTRING (str2 )-> len );
2274
+ memcpy (RSTRING (str )-> ptr , RSTRING (str2 )-> ptr , RSTRING (str2 )-> len );
2275
+ if (!RSTRING (str )-> ptr ) {
2276
+ make_null_str (str );
2277
+ }
2278
+ }
2279
+ else {
2280
+ RSTRING (str )-> ptr = RSTRING (str2 )-> ptr ;
2281
+ RSTRING (str )-> len = RSTRING (str2 )-> len ;
2282
+ str_make_independent (str );
2283
+ }
2262
2284
if (FL_TEST (str2 , STR_ASSOC )) {
2263
2285
FL_SET (str , STR_ASSOC );
2264
2286
RSTRING (str )-> aux .shared = RSTRING (str2 )-> aux .shared ;
@@ -4642,7 +4664,7 @@ Init_String()
4642
4664
rb_cString = rb_define_class ("String" , rb_cObject );
4643
4665
rb_include_module (rb_cString , rb_mComparable );
4644
4666
rb_include_module (rb_cString , rb_mEnumerable );
4645
- rb_define_alloc_func (rb_cString , str_alloc );
4667
+ rb_define_alloc_func (rb_cString , rb_str_s_alloc );
4646
4668
rb_define_method (rb_cString , "initialize" , rb_str_init , -1 );
4647
4669
rb_define_method (rb_cString , "initialize_copy" , rb_str_replace , 1 );
4648
4670
rb_define_method (rb_cString , "<=>" , rb_str_cmp_m , 1 );
0 commit comments