summaryrefslogtreecommitdiff
path: root/string.c
diff options
context:
space:
mode:
Diffstat (limited to 'string.c')
-rw-r--r--string.c16
1 files changed, 12 insertions, 4 deletions
diff --git a/string.c b/string.c
index 61b0a96ef6..d2737045e1 100644
--- a/string.c
+++ b/string.c
@@ -1328,12 +1328,20 @@ rb_str_modify_expand(VALUE str, long expand)
if (expand < 0) {
rb_raise(rb_eArgError, "negative expanding string size");
}
- if (!str_independent(str) ||
- (expand > 0 &&
- (!STR_EMBED_P(str) ||
- RSTRING_LEN(str) + expand > RSTRING_EMBED_LEN_MAX))) {
+ if (!str_independent(str)) {
str_make_independent_expand(str, expand);
}
+ else if (expand > 0) {
+ long len = RSTRING_LEN(str);
+ long capa = len + expand;
+ if (!STR_EMBED_P(str)) {
+ REALLOC_N(RSTRING(str)->as.heap.ptr, char, capa+1);
+ RSTRING(str)->as.heap.aux.capa = capa;
+ }
+ else if (capa > RSTRING_EMBED_LEN_MAX) {
+ str_make_independent_expand(str, expand);
+ }
+ }
ENC_CODERANGE_CLEAR(str);
}