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

ruby-changes:73341

From: Nobuyoshi <ko1@a...>
Date: Wed, 31 Aug 2022 17:28:23 +0900 (JST)
Subject: [ruby-changes:73341] 9dc60653db (master): Extract `update_coderange` macro

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

From 9dc60653db186b1ae9400ed75b413a07728ce6ff Mon Sep 17 00:00:00 2001
From: Nobuyoshi Nakada <nobu@r...>
Date: Tue, 30 Aug 2022 14:16:02 +0900
Subject: Extract `update_coderange` macro

Which restarts scanning the code range in unscanned part.
---
 sprintf.c | 30 ++++++++++++++----------------
 1 file changed, 14 insertions(+), 16 deletions(-)

diff --git a/sprintf.c b/sprintf.c
index 32a72439af..b2bdd4a072 100644
--- a/sprintf.c
+++ b/sprintf.c
@@ -221,7 +221,7 @@ rb_str_format(int argc, const VALUE *argv, VALUE fmt) https://github.com/ruby/ruby/blob/trunk/sprintf.c#L221
     VALUE result;
 
     long scanned = 0;
-    int coderange = ENC_CODERANGE_7BIT;
+    enum ruby_coderange_type coderange = ENC_CODERANGE_7BIT;
     int width, prec, flags = FNONE;
     int nextarg = 1;
     int posarg = 0;
@@ -246,6 +246,16 @@ rb_str_format(int argc, const VALUE *argv, VALUE fmt) https://github.com/ruby/ruby/blob/trunk/sprintf.c#L246
         rb_raise(rb_eArgError, "flag after precision"); \
     }
 
+#define update_coderange(partial) do { \
+        if (coderange != ENC_CODERANGE_BROKEN && scanned < blen \
+            && rb_enc_to_index(enc) /* != ENCINDEX_ASCII_8BIT */) { \
+            int cr = coderange; \
+            scanned += rb_str_coderange_scan_restartable(buf+scanned, buf+blen, enc, &cr); \
+            ENC_CODERANGE_SET(result, \
+                              (partial && cr == ENC_CODERANGE_UNKNOWN ? \
+                               ENC_CODERANGE_BROKEN : (coderange = cr))); \
+        } \
+    } while (0)
     ++argc;
     --argv;
     StringValue(fmt);
@@ -273,10 +283,7 @@ rb_str_format(int argc, const VALUE *argv, VALUE fmt) https://github.com/ruby/ruby/blob/trunk/sprintf.c#L283
             rb_raise(rb_eArgError, "incomplete format specifier; use %%%% (double %%) instead");
         }
         PUSH(p, t - p);
-        if (coderange != ENC_CODERANGE_BROKEN && scanned < blen) {
-            scanned += rb_str_coderange_scan_restartable(buf+scanned, buf+blen, enc, &coderange);
-            ENC_CODERANGE_SET(result, coderange);
-        }
+        update_coderange(FALSE);
         if (t >= end) {
             /* end of fmt string */
             goto sprint_exit;
@@ -492,13 +499,7 @@ rb_str_format(int argc, const VALUE *argv, VALUE fmt) https://github.com/ruby/ruby/blob/trunk/sprintf.c#L499
               format_s1:
                 len = RSTRING_LEN(str);
                 rb_str_set_len(result, blen);
-                if (coderange != ENC_CODERANGE_BROKEN && scanned < blen) {
-                    int cr = coderange;
-                    scanned += rb_str_coderange_scan_restartable(buf+scanned, buf+blen, enc, &cr);
-                    ENC_CODERANGE_SET(result,
-                                      (cr == ENC_CODERANGE_UNKNOWN ?
-                                       ENC_CODERANGE_BROKEN : (coderange = cr)));
-                }
+                update_coderange(TRUE);
                 enc = rb_enc_check(result, str);
                 if (flags&(FPREC|FWIDTH)) {
                     slen = rb_enc_strlen(RSTRING_PTR(str),RSTRING_END(str),enc);
@@ -930,10 +931,7 @@ rb_str_format(int argc, const VALUE *argv, VALUE fmt) https://github.com/ruby/ruby/blob/trunk/sprintf.c#L931
         flags = FNONE;
     }
 
-    if (coderange != ENC_CODERANGE_BROKEN && scanned < blen) {
-        scanned += rb_str_coderange_scan_restartable(buf+scanned, buf+blen, enc, &coderange);
-        ENC_CODERANGE_SET(result, coderange);
-    }
+    update_coderange(FALSE);
   sprint_exit:
     rb_str_tmp_frozen_release(orig, fmt);
     /* XXX - We cannot validate the number of arguments if (digit)$ style used.
-- 
cgit v1.2.1


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

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