summaryrefslogtreecommitdiff
path: root/string.c
diff options
context:
space:
mode:
authorusa <usa@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2014-09-05 04:23:37 +0000
committerusa <usa@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2014-09-05 04:23:37 +0000
commitb9e630607ed20bd35adf75faee325d88654c42e4 (patch)
tree8415d744ce43c8bfd2c73a2e0a32231a5a24d896 /string.c
parentd7f49a9b066f511cdea83762b81d25fffd2f54c7 (diff)
merge revision(s) 46408,46410,46413,46414,46424,46436,46437: [Backport #9934]
string.c: shrink too big buffer * string.c (rb_str_resize): shrink the buffer even if new length is same but it is enough smaller than the capacity. * file.c (expand_path): shrink expanded path which no longer needs rooms to append. [ruby-core:63114] [Bug #9934] * string.c (rb_str_resize): should consider the capacity instead of the old length, as pointed out by nagachika. * string.c (rb_str_resize): update capa only when buffer get reallocated. http://d.hatena.ne.jp/nagachika/20140613/ruby_trunk_changes_46413_46420#r46413 git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/branches/ruby_2_0_0@47399 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'string.c')
-rw-r--r--string.c15
1 files changed, 9 insertions, 6 deletions
diff --git a/string.c b/string.c
index a45d7ad471..d6a5232411 100644
--- a/string.c
+++ b/string.c
@@ -838,7 +838,7 @@ RUBY_FUNC_EXPORTED size_t
rb_str_memsize(VALUE str)
{
if (!STR_EMBED_P(str) && !STR_SHARED_P(str)) {
- return RSTRING(str)->as.heap.aux.capa;
+ return RSTRING(str)->as.heap.aux.capa + 1; /* termlen */
}
else {
return 0;
@@ -1863,9 +1863,11 @@ rb_str_resize(VALUE str, long len)
independent = str_independent(str);
ENC_CODERANGE_CLEAR(str);
slen = RSTRING_LEN(str);
- if (len != slen) {
+ {
+ long capa;
if (STR_EMBED_P(str)) {
- if (len <= RSTRING_EMBED_LEN_MAX) {
+ if (len == slen) return str;
+ if (len + 1 <= RSTRING_EMBED_LEN_MAX + 1) {
STR_SET_EMBED_LEN(str, len);
RSTRING(str)->as.ary[len] = '\0';
return str;
@@ -1884,14 +1886,15 @@ rb_str_resize(VALUE str, long len)
return str;
}
else if (!independent) {
+ if (len == slen) return str;
str_make_independent_expand(str, len - slen);
}
- else if (slen < len || slen - len > 1024) {
+ else if ((capa = RSTRING(str)->as.heap.aux.capa) < len ||
+ (capa - len) > (len < 1024 ? len : 1024)) {
REALLOC_N(RSTRING(str)->as.heap.ptr, char, len+1);
- }
- if (!STR_NOCAPA_P(str)) {
RSTRING(str)->as.heap.aux.capa = len;
}
+ else if (len == slen) return str;
RSTRING(str)->as.heap.len = len;
RSTRING(str)->as.heap.ptr[len] = '\0'; /* sentinel */
}