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

ruby-changes:72949

From: Nobuyoshi <ko1@a...>
Date: Wed, 17 Aug 2022 18:45:31 +0900 (JST)
Subject: [ruby-changes:72949] ddb81b9307 (master): Extract common code from `rb_enc_vsprintf` and `rb_str_vcatf`

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

From ddb81b9307f7114fcb37f6fcc37b7692b1a40cb4 Mon Sep 17 00:00:00 2001
From: Nobuyoshi Nakada <nobu@r...>
Date: Wed, 17 Aug 2022 17:39:40 +0900
Subject: Extract common code from `rb_enc_vsprintf` and `rb_str_vcatf`

---
 sprintf.c | 58 +++++++++++++++++++++++++---------------------------------
 1 file changed, 25 insertions(+), 33 deletions(-)

diff --git a/sprintf.c b/sprintf.c
index 09b9bf15d2..2b2b34b5b4 100644
--- a/sprintf.c
+++ b/sprintf.c
@@ -1145,35 +1145,44 @@ ruby__sfvextra(rb_printf_buffer *fp, size_t valsize, void *valp, long *sz, int s https://github.com/ruby/ruby/blob/trunk/sprintf.c#L1145
     return cp;
 }
 
-VALUE
-rb_enc_vsprintf(rb_encoding *enc, const char *fmt, va_list ap)
+static void
+ruby_vsprintf0(VALUE result, char *p, const char *fmt, va_list ap)
 {
     rb_printf_buffer_extra buffer;
 #define f buffer.base
-    VALUE result;
+    VALUE klass = RBASIC(result)->klass;
 
     f._flags = __SWR | __SSTR;
     f._bf._size = 0;
-    f._w = 120;
-    result = rb_str_buf_new(f._w);
-    if (enc) {
-        if (rb_enc_mbminlen(enc) > 1) {
-            /* the implementation deeply depends on plain char */
-            rb_raise(rb_eArgError, "cannot construct wchar_t based encoding string: %s",
-                     rb_enc_name(enc));
-        }
-        rb_enc_associate(result, enc);
-    }
+    f._w = rb_str_capacity(result);
     f._bf._base = (unsigned char *)result;
-    f._p = (unsigned char *)RSTRING_PTR(result);
+    f._p = (unsigned char *)p;
     RBASIC_CLEAR_CLASS(result);
     f.vwrite = ruby__sfvwrite;
     f.vextra = ruby__sfvextra;
     buffer.value = 0;
     BSD_vfprintf(&f, fmt, ap);
-    RBASIC_SET_CLASS_RAW(result, rb_cString);
+    RBASIC_SET_CLASS_RAW(result, klass);
     rb_str_resize(result, (char *)f._p - RSTRING_PTR(result));
 #undef f
+}
+
+VALUE
+rb_enc_vsprintf(rb_encoding *enc, const char *fmt, va_list ap)
+{
+    const int initial_len = 120;
+    VALUE result;
+
+    result = rb_str_buf_new(initial_len);
+    if (enc) {
+        if (rb_enc_mbminlen(enc) > 1) {
+            /* the implementation deeply depends on plain char */
+            rb_raise(rb_eArgError, "cannot construct wchar_t based encoding string: %s",
+                     rb_enc_name(enc));
+        }
+        rb_enc_associate(result, enc);
+    }
+    ruby_vsprintf0(result, RSTRING_PTR(result), fmt, ap);
 
     return result;
 }
@@ -1213,26 +1222,9 @@ rb_sprintf(const char *format, ...) https://github.com/ruby/ruby/blob/trunk/sprintf.c#L1222
 VALUE
 rb_str_vcatf(VALUE str, const char *fmt, va_list ap)
 {
-    rb_printf_buffer_extra buffer;
-#define f buffer.base
-    VALUE klass;
-
     StringValue(str);
     rb_str_modify(str);
-    f._flags = __SWR | __SSTR;
-    f._bf._size = 0;
-    f._w = rb_str_capacity(str);
-    f._bf._base = (unsigned char *)str;
-    f._p = (unsigned char *)RSTRING_END(str);
-    klass = RBASIC(str)->klass;
-    RBASIC_CLEAR_CLASS(str);
-    f.vwrite = ruby__sfvwrite;
-    f.vextra = ruby__sfvextra;
-    buffer.value = 0;
-    BSD_vfprintf(&f, fmt, ap);
-    RBASIC_SET_CLASS_RAW(str, klass);
-    rb_str_resize(str, (char *)f._p - RSTRING_PTR(str));
-#undef f
+    ruby_vsprintf0(str, RSTRING_END(str), fmt, ap);
 
     return str;
 }
-- 
cgit v1.2.1


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

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