Avoid allocating a temporary empty string in String#slice!
This commit is contained in:
parent
ca2888fb3d
commit
52dc0632fa
Notes:
git
2020-01-31 09:22:43 +09:00
11
string.c
11
string.c
@ -215,6 +215,8 @@ str_make_independent(VALUE str)
|
|||||||
/* symbols for [up|down|swap]case/capitalize options */
|
/* symbols for [up|down|swap]case/capitalize options */
|
||||||
static VALUE sym_ascii, sym_turkic, sym_lithuanian, sym_fold;
|
static VALUE sym_ascii, sym_turkic, sym_lithuanian, sym_fold;
|
||||||
|
|
||||||
|
static VALUE empty_string;
|
||||||
|
|
||||||
static rb_encoding *
|
static rb_encoding *
|
||||||
get_actual_encoding(const int encidx, VALUE str)
|
get_actual_encoding(const int encidx, VALUE str)
|
||||||
{
|
{
|
||||||
@ -4887,13 +4889,13 @@ rb_str_slice_bang(int argc, VALUE *argv, VALUE str)
|
|||||||
|
|
||||||
rb_check_arity(argc, 1, 2);
|
rb_check_arity(argc, 1, 2);
|
||||||
for (i=0; i<argc; i++) {
|
for (i=0; i<argc; i++) {
|
||||||
buf[i] = argv[i];
|
buf[i] = argv[i];
|
||||||
}
|
}
|
||||||
str_modify_keep_cr(str);
|
str_modify_keep_cr(str);
|
||||||
result = rb_str_aref_m(argc, buf, str);
|
result = rb_str_aref_m(argc, buf, str);
|
||||||
if (!NIL_P(result)) {
|
if (!NIL_P(result)) {
|
||||||
buf[i] = rb_str_new(0,0);
|
buf[i] = empty_string;
|
||||||
rb_str_aset_m(argc+1, buf, str);
|
rb_str_aset_m(argc+1, buf, str);
|
||||||
}
|
}
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
@ -11380,6 +11382,9 @@ Init_String(void)
|
|||||||
rb_define_method(rb_cString, "unicode_normalize!", rb_str_unicode_normalize_bang, -1);
|
rb_define_method(rb_cString, "unicode_normalize!", rb_str_unicode_normalize_bang, -1);
|
||||||
rb_define_method(rb_cString, "unicode_normalized?", rb_str_unicode_normalized_p, -1);
|
rb_define_method(rb_cString, "unicode_normalized?", rb_str_unicode_normalized_p, -1);
|
||||||
|
|
||||||
|
empty_string = rb_fstring_enc_lit("", rb_usascii_encoding());
|
||||||
|
rb_gc_register_mark_object(empty_string);
|
||||||
|
|
||||||
rb_fs = Qnil;
|
rb_fs = Qnil;
|
||||||
rb_define_hooked_variable("$;", &rb_fs, 0, rb_fs_setter);
|
rb_define_hooked_variable("$;", &rb_fs, 0, rb_fs_setter);
|
||||||
rb_define_hooked_variable("$-F", &rb_fs, 0, rb_fs_setter);
|
rb_define_hooked_variable("$-F", &rb_fs, 0, rb_fs_setter);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user