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

ruby-changes:68257

From: =E5=8D=9C=E9=83=A8=E6=98=8C=E5=B9=B3 <ko1@a...>
Date: Tue, 5 Oct 2021 14:18:43 +0900 (JST)
Subject: [ruby-changes:68257] 5112a54846 (master): include/ruby/encoding.h: convert macros into inline functions

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

From 5112a548467e04ebdb386f0cc7bacb29f38d3fd2 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=E5=8D=9C=E9=83=A8=E6=98=8C=E5=B9=B3?=
 <shyouhei@r...>
Date: Wed, 29 Sep 2021 12:58:26 +0900
Subject: include/ruby/encoding.h: convert macros into inline functions

Less macros == huge win.
---
 encoding.c                                 |  14 --
 include/ruby/internal/encoding/coderange.h |  88 ++++++----
 include/ruby/internal/encoding/ctype.h     |  83 +++++++--
 include/ruby/internal/encoding/encoding.h  | 267 +++++++++++++++++++++++------
 include/ruby/internal/encoding/string.h    |  33 ++--
 string.c                                   |   2 +-
 template/Doxyfile.tmpl                     |   1 +
 7 files changed, 368 insertions(+), 120 deletions(-)

diff --git a/encoding.c b/encoding.c
index a162821e64..2fb1c42047 100644
--- a/encoding.c
+++ b/encoding.c
@@ -1277,13 +1277,6 @@ rb_enc_codepoint_len(const char *p, const char *e, int *len_p, rb_encoding *enc) https://github.com/ruby/ruby/blob/trunk/encoding.c#L1277
     return rb_enc_mbc_to_codepoint(p, e, enc);
 }
 
-#undef rb_enc_codepoint
-unsigned int
-rb_enc_codepoint(const char *p, const char *e, rb_encoding *enc)
-{
-    return rb_enc_codepoint_len(p, e, 0, enc);
-}
-
 int
 rb_enc_codelen(int c, rb_encoding *enc)
 {
@@ -1294,13 +1287,6 @@ rb_enc_codelen(int c, rb_encoding *enc) https://github.com/ruby/ruby/blob/trunk/encoding.c#L1287
     return n;
 }
 
-#undef rb_enc_code_to_mbclen
-int
-rb_enc_code_to_mbclen(int code, rb_encoding *enc)
-{
-    return ONIGENC_CODE_TO_MBCLEN(enc, code);
-}
-
 int
 rb_enc_toupper(int c, rb_encoding *enc)
 {
diff --git a/include/ruby/internal/encoding/coderange.h b/include/ruby/internal/encoding/coderange.h
index 84daddeeb3..7a81208c9e 100644
--- a/include/ruby/internal/encoding/coderange.h
+++ b/include/ruby/internal/encoding/coderange.h
@@ -22,7 +22,9 @@ https://github.com/ruby/ruby/blob/trunk/include/ruby/internal/encoding/coderange.h#L22
  */
 
 #include "ruby/internal/attr/const.h"
+#include "ruby/internal/attr/pure.h"
 #include "ruby/internal/dllexport.h"
+#include "ruby/internal/fl_type.h"
 #include "ruby/internal/value.h"
 
 RBIMPL_SYMBOL_EXPORT_BEGIN()
@@ -65,6 +67,7 @@ rb_enc_coderange_clean_p(int cr) https://github.com/ruby/ruby/blob/trunk/include/ruby/internal/encoding/coderange.h#L67
     return (cr ^ (cr >> 1)) & RUBY_ENC_CODERANGE_7BIT;
 }
 
+RBIMPL_ATTR_CONST()
 /**
  * Queries if  a code range  is "clean".  "Clean" in  this context means  it is
  * known and valid.
@@ -73,8 +76,13 @@ rb_enc_coderange_clean_p(int cr) https://github.com/ruby/ruby/blob/trunk/include/ruby/internal/encoding/coderange.h#L76
  * @retval     1   It is.
  * @retval     0   It isn't.
  */
-#define RB_ENC_CODERANGE_CLEAN_P(cr) rb_enc_coderange_clean_p(cr)
+static inline bool
+RB_ENC_CODERANGE_CLEAN_P(enum ruby_coderange_type cr)
+{
+    return rb_enc_coderange_clean_p(cr);
+}
 
+RBIMPL_ATTR_PURE_UNLESS_DEBUG()
 /**
  * Queries the  (inline) code range of  the passed object.  The  object must be
  * capable  of   having  inline   encoding.   Using   this  macro   needs  deep
@@ -83,8 +91,15 @@ rb_enc_coderange_clean_p(int cr) https://github.com/ruby/ruby/blob/trunk/include/ruby/internal/encoding/coderange.h#L91
  * @param[in]  obj  Target object.
  * @return     An enum ::ruby_coderange_type.
  */
-#define RB_ENC_CODERANGE(obj) ((int)RBASIC(obj)->flags & RUBY_ENC_CODERANGE_MASK)
+static inline enum ruby_coderange_type
+RB_ENC_CODERANGE(VALUE obj)
+{
+    VALUE ret = RB_FL_TEST_RAW(obj, RUBY_ENC_CODERANGE_MASK);
+
+    return RBIMPL_CAST((enum ruby_coderange_type)ret);
+}
 
+RBIMPL_ATTR_PURE_UNLESS_DEBUG()
 /**
  * Queries   the    (inline)   code   range    of   the   passed    object   is
  * ::RUBY_ENC_CODERANGE_7BIT.   The object  must  be capable  of having  inline
@@ -95,7 +110,11 @@ rb_enc_coderange_clean_p(int cr) https://github.com/ruby/ruby/blob/trunk/include/ruby/internal/encoding/coderange.h#L110
  * @retval     1    It is ascii only.
  * @retval     0    Otherwise (including cases when the range is not known).
  */
-#define RB_ENC_CODERANGE_ASCIIONLY(obj) (RB_ENC_CODERANGE(obj) == RUBY_ENC_CODERANGE_7BIT)
+static inline bool
+RB_ENC_CODERANGE_ASCIIONLY(VALUE obj)
+{
+    return RB_ENC_CODERANGE(obj) == RUBY_ENC_CODERANGE_7BIT;
+}
 
 /**
  * Destructively modifies the passed object so  that its (inline) code range is
@@ -106,9 +125,12 @@ rb_enc_coderange_clean_p(int cr) https://github.com/ruby/ruby/blob/trunk/include/ruby/internal/encoding/coderange.h#L125
  * @param[out]  cr   An enum ::ruby_coderange_type.
  * @post        `obj`'s code range is `cr`.
  */
-#define RB_ENC_CODERANGE_SET(obj,cr) (\
-        RBASIC(obj)->flags = \
-        (RBASIC(obj)->flags & ~RUBY_ENC_CODERANGE_MASK) | (cr))
+static inline void
+RB_ENC_CODERANGE_SET(VALUE obj, enum ruby_coderange_type cr)
+{
+    RB_FL_UNSET_RAW(obj, RUBY_ENC_CODERANGE_MASK);
+    RB_FL_SET_RAW(obj, cr);
+}
 
 /**
  * Destructively clears  the passed object's  (inline) code range.   The object
@@ -118,8 +140,13 @@ rb_enc_coderange_clean_p(int cr) https://github.com/ruby/ruby/blob/trunk/include/ruby/internal/encoding/coderange.h#L140
  * @param[out]  obj  Target object.
  * @post        `obj`'s code range is ::RUBY_ENC_CODERANGE_UNKNOWN.
  */
-#define RB_ENC_CODERANGE_CLEAR(obj) RB_ENC_CODERANGE_SET((obj),0)
+static inline void
+RB_ENC_CODERANGE_CLEAR(VALUE obj)
+{
+    RB_FL_UNSET_RAW(obj, RUBY_ENC_CODERANGE_MASK);
+}
 
+RBIMPL_ATTR_CONST()
 /* assumed ASCII compatibility */
 /**
  * "Mix"  two code  ranges  into one.   This  is handy  for  instance when  you
@@ -131,28 +158,22 @@ rb_enc_coderange_clean_p(int cr) https://github.com/ruby/ruby/blob/trunk/include/ruby/internal/encoding/coderange.h#L158
  * @param[in]  b  Another enum ::ruby_coderange_type.
  * @return     The `a` "and" `b`.
  */
-#define RB_ENC_CODERANGE_AND(a, b) \
-    ((a) == RUBY_ENC_CODERANGE_7BIT ? (b) : \
-     (a) != RUBY_ENC_CODERANGE_VALID ? RUBY_ENC_CODERANGE_UNKNOWN : \
-     (b) == RUBY_ENC_CODERANGE_7BIT ? RUBY_ENC_CODERANGE_VALID : (b))
-
-/**
- * This is #RB_ENCODING_SET  + RB_ENC_CODERANGE_SET combo.  The  object must be
- * capable  of   having  inline   encoding.   Using   this  macro   needs  deep
- * understanding of bit level object binary layout.
- *
- * @param[out]  obj       Target object.
- * @param[in]   encindex  Encoding in encindex format.
- * @param[in]   cr        An enum ::ruby_coderange_type.
- * @post        `obj`'s encoding is `encindex`.
- * @post        `obj`'s code range is `cr`.
- */
-#define RB_ENCODING_CODERANGE_SET(obj, encindex, cr) \
-    do { \
-        VALUE rb_encoding_coderange_obj = (obj); \
-        RB_ENCODING_SET(rb_encoding_coderange_obj, (encindex)); \
-        RB_ENC_CODERANGE_SET(rb_encoding_coderange_obj, (cr)); \
-    } while (0)
+static inline enum ruby_coderange_type
+RB_ENC_CODERANGE_AND(enum ruby_coderange_type a, enum ruby_coderange_type b)
+{
+    if (a == RUBY_ENC_CODERANGE_7BIT) {
+        return b;
+    }
+    else if (a != RUBY_ENC_CODERANGE_VALID) {
+        return RUBY_ENC_CODERANGE_UNKNOWN;
+    }
+    else if (b == RUBY_ENC_CODERANGE_7BIT) {
+        return RUBY_ENC_CODERANGE_VALID;
+    }
+    else {
+        return b;
+    }
+}
 
 #define ENC_CODERANGE_MASK                        RUBY_ENC_CODERANGE_MASK                      /**< @old{RUBY_ENC_CODERANGE_MASK} */
 #define ENC_CODERANGE_UNKNOWN                     RUBY_ENC_CODERANGE_UNKNOWN                   /**< @old{RUBY_ENC_CODERANGE_UNKNOWN} */
@@ -167,6 +188,15 @@ rb_enc_coderange_clean_p(int cr) https://github.com/ruby/ruby/blob/trunk/include/ruby/internal/encoding/coderange.h#L188
 #define ENC_CODERANGE_AND(a, b)                   RB_ENC_CODERANGE_AND(a, b)                   /**< @old{RB_ENC_CODERANGE_AND} */
 #define ENCODING_CODERANGE_SET(obj, encindex, cr) RB_ENCODING_CODERANGE_SET(obj, encindex, cr) /**< @old{RB_ENCODING_CODERANGE_SET} */
 
+/** @cond INTERNAL_MACRO */
+#define RB_ENC_CODERANGE           RB_ENC_CODERANGE
+#define RB_ENC_CODERANGE_AND       RB_ENC_CODERANGE_AND
+#define RB_ENC_CODERANGE_ASCIIONLY RB_ENC_CODERANGE_ASCIIONLY
+#define RB_ENC_CODERANGE_CLEAN_P   RB_ENC_CODERANGE_CLEAN_P
+#define RB_ENC_CODERANGE_CLEAR     RB_ENC_CODERANGE_CLEAR
+#define RB_ENC_CODERANGE_SET       RB_ENC_CODERANGE_SET
+/** @endcond */
+
 RBIMPL_SYMBOL_EXPORT_END()
 
 #endif /* RUBY_INTERNAL_ENCODING_CODERANGE_H */
diff --git a/include/ruby/internal/encoding/ctype.h b/include/ruby/internal/encoding/ctype.h
index e0b95f93b2..422e2b9c2d 100644
--- a/include/ruby/internal/encoding/ctype.h
+++ b/include/ruby/internal/encoding/ctype.h
@@ -39,7 +39,14 @@ RBIMPL_SYMBOL_EXPORT_BEGIN() https://github.com/ruby/ruby/blob/trunk/include/ruby/internal/encoding/ctype.h#L39
  * @retval     0          It isn't.
  * @retval     otherwise  It is.
  */
-#define rb_enc_is_newline(p,end,enc)  ONIGENC_IS_MBC_NEWLINE((enc),(UChar*)(p),(UChar*)(end))
+static inline bool
+rb_enc_is_newline(const char *p,  const char *e, const rb_encoding *enc)
+{
+    OnigUChar *up = RBIMPL_CAST((OnigUChar *)p);
+    OnigUChar *ue = RBIMPL_CAST((OnigUChar *)e);
+
+    return ONIGENC_IS_MBC_NEWLINE(enc, up, ue);
+}
 
 /**
  * Queries if the passed  code point is of passed character  type in the passed
@@ -52,7 +59,11 @@ RBIMPL_SYMBOL_EXPORT_BEGIN() https://github.com/ruby/ruby/blob/trunk/include/ruby/internal/encoding/ctype.h#L59
  * @retval     1    `c` is of `t` in `enc`.
  * @retval     0    Otherwise.
  */
-#define rb_enc_isctype(c,t,enc) ONIGENC_IS_CODE_CTYPE((enc),(c),(t))
+static inline bool
+rb_enc_isctype(OnigCodePoint c, OnigCtype t, const rb_encoding *enc)
+{
+    return ONIGENC_IS_CODE_CTYPE(enc, c, t);
+}
 
 /**
  * Identical to rb_isascii(), except it additionally takes an encoding.
@@ -67,7 +78,11 @@ RBIMPL_SYMBOL_EXPORT_BEGIN() https://github.com/ruby/ruby/blob/trunk/include/ruby/internal/encoding/ctype.h#L78
  * `enc` is  ignored.  This  is at least  an intentional  implementation detail
  * (not a bug).  But th (... truncated)

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

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