1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

Make string slices views rather than copies

Just like commit 1c16645 for arrays, this commit changes string slices
to be a view rather than a copy even if it can be allocated through VWA.
This commit is contained in:
Peter Zhu 2022-09-27 14:50:40 -04:00
parent 247d598477
commit 6f8d17e43c
Notes: git 2022-09-28 22:05:55 +09:00

View file

@ -2802,18 +2802,20 @@ str_subseq(VALUE str, long beg, long len)
{ {
VALUE str2; VALUE str2;
if (!STR_EMBEDDABLE_P(len, TERM_LEN(str)) && const long rstring_embed_capa_max = ((sizeof(struct RString) - offsetof(struct RString, as.embed.ary)) / sizeof(char)) - 1;
SHARABLE_SUBSTRING_P(beg, len, RSTRING_LEN(str))) {
str2 = rb_str_new_shared(str); if (!SHARABLE_SUBSTRING_P(beg, len, RSTRING_LEN(str)) ||
len <= rstring_embed_capa_max) {
str2 = rb_str_new(RSTRING_PTR(str) + beg, len);
RB_GC_GUARD(str);
}
else {
str2 = str_new_shared(rb_cString, str);
RSTRING(str2)->as.heap.ptr += beg; RSTRING(str2)->as.heap.ptr += beg;
if (RSTRING(str2)->as.heap.len > len) { if (RSTRING(str2)->as.heap.len > len) {
RSTRING(str2)->as.heap.len = len; RSTRING(str2)->as.heap.len = len;
} }
} }
else {
str2 = rb_str_new(RSTRING_PTR(str) + beg, len);
RB_GC_GUARD(str);
}
return str2; return str2;
} }