summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNobuyoshi Nakada <nobu@ruby-lang.org>2022-08-17 17:41:48 +0900
committerNobuyoshi Nakada <nobu@ruby-lang.org>2022-08-19 03:05:53 +0900
commitf34280ec6b684eeb21ef3336c7002e1a83dcfd2c (patch)
tree768c63475f80f3eb53381a84c4617ef85975f3aa
parent59da26789f11dfa56a029a72210998cdd2f84174 (diff)
Scan the code range of the formatted portion
Notes
Notes: Merged: https://github.com/ruby/ruby/pull/6253
-rw-r--r--sprintf.c14
1 files changed, 11 insertions, 3 deletions
diff --git a/sprintf.c b/sprintf.c
index 1ee293b6d9..04cead7731 100644
--- a/sprintf.c
+++ b/sprintf.c
@@ -1153,6 +1153,10 @@ ruby_vsprintf0(VALUE result, char *p, const char *fmt, va_list ap)
rb_printf_buffer_extra buffer;
#define f buffer.base
VALUE klass = RBASIC(result)->klass;
+ int coderange = ENC_CODERANGE(result);
+ long scanned = 0;
+
+ if (coderange != ENC_CODERANGE_UNKNOWN) scanned = p - RSTRING_PTR(result);
f._flags = __SWR | __SSTR;
f._bf._size = 0;
@@ -1165,9 +1169,13 @@ 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));
+ p = RSTRING_PTR(result);
+ long blen = (char *)f._p - p;
+ if (scanned < blen) {
+ rb_str_coderange_scan_restartable(p + scanned, p + blen, rb_enc_get(result), &coderange);
+ ENC_CODERANGE_SET(result, coderange);
+ }
+ rb_str_resize(result, blen);
#undef f
}