summaryrefslogtreecommitdiff
path: root/sprintf.c
diff options
context:
space:
mode:
authorJean Boussier <jean.boussier@gmail.com>2022-07-25 10:21:38 +0200
committerJean Boussier <jean.boussier@gmail.com>2022-08-18 10:09:08 +0200
commitb0b9f7201acab05c2a3ad92c3043a1f01df3e17f (patch)
treefe3445ca10f4254baab7e5c182eaccb1943e3b3b /sprintf.c
parentfe61cad7490da8a879597f851f4a89856d44838e (diff)
rb_str_resize: Only clear coderange on truncation
If we are expanding the string or only stripping extra capacity then coderange won't change, so clearing it is wasteful.
Notes
Notes: Merged: https://github.com/ruby/ruby/pull/6178
Diffstat (limited to 'sprintf.c')
-rw-r--r--sprintf.c5
1 files changed, 4 insertions, 1 deletions
diff --git a/sprintf.c b/sprintf.c
index 2b2b34b5b4..1ee293b6d9 100644
--- a/sprintf.c
+++ b/sprintf.c
@@ -937,6 +937,8 @@ rb_str_format(int argc, const VALUE *argv, VALUE fmt)
if (RTEST(ruby_verbose)) rb_warn("%s", mesg);
}
rb_str_resize(result, blen);
+ // rb_str_format mutates the string without updating coderange
+ ENC_CODERANGE_CLEAR(result);
return result;
}
@@ -1163,6 +1165,8 @@ ruby_vsprintf0(VALUE result, char *p, const char *fmt, va_list ap)
buffer.value = 0;
BSD_vfprintf(&f, fmt, ap);
RBASIC_SET_CLASS_RAW(result, klass);
+ // vfprintf mutates the string without updating coderange
+ ENC_CODERANGE_CLEAR(result);
rb_str_resize(result, (char *)f._p - RSTRING_PTR(result));
#undef f
}
@@ -1183,7 +1187,6 @@ rb_enc_vsprintf(rb_encoding *enc, const char *fmt, va_list ap)
rb_enc_associate(result, enc);
}
ruby_vsprintf0(result, RSTRING_PTR(result), fmt, ap);
-
return result;
}