[前][次][番号順一覧][スレッド一覧]

ruby-changes:72970

From: Nobuyoshi <ko1@a...>
Date: Fri, 19 Aug 2022 03:06:11 +0900 (JST)
Subject: [ruby-changes:72970] f34280ec6b (master): Scan the code range of the formatted portion

https://git.ruby-lang.org/ruby.git/commit/?id=f34280ec6b

From f34280ec6b684eeb21ef3336c7002e1a83dcfd2c Mon Sep 17 00:00:00 2001
From: Nobuyoshi Nakada <nobu@r...>
Date: Wed, 17 Aug 2022 17:41:48 +0900
Subject: Scan the code range of the formatted portion

---
 sprintf.c | 14 +++++++++++---
 1 file 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) https://github.com/ruby/ruby/blob/trunk/sprintf.c#L1153
     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) https://github.com/ruby/ruby/blob/trunk/sprintf.c#L1169
     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
 }
 
-- 
cgit v1.2.1


--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]