summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--string.c16
1 files changed, 9 insertions, 7 deletions
diff --git a/string.c b/string.c
index b9c567cbe2..bdab6fa143 100644
--- a/string.c
+++ b/string.c
@@ -2802,18 +2802,20 @@ str_subseq(VALUE str, long beg, long len)
{
VALUE str2;
- if (!STR_EMBEDDABLE_P(len, TERM_LEN(str)) &&
- SHARABLE_SUBSTRING_P(beg, len, RSTRING_LEN(str))) {
- str2 = rb_str_new_shared(str);
+ const long rstring_embed_capa_max = ((sizeof(struct RString) - offsetof(struct RString, as.embed.ary)) / sizeof(char)) - 1;
+
+ 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;
if (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;
}