Project

General

Profile

Feature #4893 ยป String_call_initialize_v4.diff

Without any manual access to the flag - lazaridis.com (Lazaridis Ilias), 07/13/2011 08:18 AM

View differences:

"b/P:\\ri\\sandbox\\rb193\\string.c"
406 406
    return str;
407 407
}
408 408

  
409
/* use class-variable (bit-flag) */
410
#define FL_CALL_INIT FL_USER18
411

  
412
VALUE
413
rb_str_method_added(VALUE self, VALUE method_symbol)
414
{
415
/*  callback method, called whenever a String class method is added (defined) */
416

  
417
    ID mid = SYM2ID(method_symbol);
418
    ID idInitialize = rb_intern("initialize");
419

  
420
    if (mid == idInitialize) {
421
	FL_SET(rb_cString, FL_CALL_INIT);
422
    }
423
    return Qtrue; /* unused */
424
}
425

  
426
VALUE
427
rb_str_method_undefined(VALUE self, VALUE method_symbol)
428
{
429
/*  callback method, called whenever a String class method is undefined */
430

  
431
    if ( SYM2ID(method_symbol) == rb_intern("initialize") ) {
432
	FL_UNSET(rb_cString, FL_CALL_INIT);
433
    }
434
    return Qtrue; /* unused */
435
}
436

  
437
VALUE
438
rb_str_method_removed(VALUE self, VALUE method_symbol)
439
{
440
/*  callback method, called whenever a String class method is removed */
441

  
442
    if ( SYM2ID(method_symbol) == rb_intern("initialize") ) {
443
	FL_UNSET(rb_cString, FL_CALL_INIT);
444
    }
445
    return Qtrue; /* unused */
446
}
447

  
409 448
VALUE
410 449
rb_str_new(const char *ptr, long len)
411 450
{
412
    return str_new(rb_cString, ptr, len);
451
    /* str is already initialized by original initialize within str_new() */
452
    VALUE str = str_new(rb_cString, ptr, len);
453

  
454
    /* speed: call initialize only if it was redefined. See #4893 */
455
    if (FL_TEST(rb_cString, FL_CALL_INIT)) {
456
	/* passing str itself as parameter, avoiding need for a temp str */
457
	rb_obj_call_init((VALUE)str, 1, &str);
458
    }
459

  
460
    return str;
413 461
}
414 462

  
415 463
VALUE
......
7724 7772
    rb_cString  = rb_define_class("String", rb_cObject);
7725 7773
    rb_include_module(rb_cString, rb_mComparable);
7726 7774
    rb_define_alloc_func(rb_cString, str_alloc);
7775

  
7776
    rb_define_singleton_method(rb_cString, "method_added", rb_str_method_added, 1);
7777
    rb_define_singleton_method(rb_cString, "method_undefined", rb_str_method_undefined, 1);
7778
    rb_define_singleton_method(rb_cString, "method_removed", rb_str_method_removed, 1);
7779

  
7727 7780
    rb_define_singleton_method(rb_cString, "try_convert", rb_str_s_try_convert, 1);
7728 7781
    rb_define_method(rb_cString, "initialize", rb_str_init, -1);
7729 7782
    rb_define_method(rb_cString, "initialize_copy", rb_str_replace, 1);