diff options
author | Nobuyoshi Nakada <nobu@ruby-lang.org> | 2023-02-04 01:31:56 +0900 |
---|---|---|
committer | Nobuyoshi Nakada <nobu@ruby-lang.org> | 2023-02-14 15:18:21 +0900 |
commit | 2490b2e1216f02aa896abdf121a28e50cac107c3 (patch) | |
tree | 8d76b8bae254eb4f2d9083fe8f52c661230d6cab | |
parent | 45f0e3a673964069a4c9c57ce8665cbc21ac267f (diff) | |
download | ruby-2490b2e1216f02aa896abdf121a28e50cac107c3.tar.gz |
Add utility macros `DECIMAL_SIZE_OF` and `DECIMAL_SIZE_OF_BYTES`
-rw-r--r-- | ast.c | 2 | ||||
-rw-r--r-- | compile.c | 2 | ||||
-rw-r--r-- | include/ruby/util.h | 9 |
3 files changed, 11 insertions, 2 deletions
@@ -377,7 +377,7 @@ rest_arg(rb_ast_t *ast, const NODE *rest_arg) static VALUE node_children(rb_ast_t *ast, const NODE *node) { - char name[DECIMAL_SIZE_OF_BITS(sizeof(long) * CHAR_BIT) + 2]; /* including '$' */ + char name[sizeof("$") + DECIMAL_SIZE_OF(long)]; enum node_type type = nd_type(node); switch (type) { @@ -8275,7 +8275,7 @@ compile_builtin_function_call(rb_iseq_t *iseq, LINK_ANCHOR *const ret, const NOD } else { # define BUILTIN_INLINE_PREFIX "_bi" - char inline_func[DECIMAL_SIZE_OF_BITS(sizeof(int) * CHAR_BIT) + sizeof(BUILTIN_INLINE_PREFIX)]; + char inline_func[sizeof(BUILTIN_INLINE_PREFIX) + DECIMAL_SIZE_OF(int)]; bool cconst = false; retry:; const struct rb_builtin_function *bf = iseq_builtin_function_lookup(iseq, builtin_func); diff --git a/include/ruby/util.h b/include/ruby/util.h index e8727a3200..ee11bc940a 100644 --- a/include/ruby/util.h +++ b/include/ruby/util.h @@ -36,6 +36,15 @@ RBIMPL_SYMBOL_EXPORT_BEGIN() /** an approximation of ceil(n * log10(2)), up to 65536 at least */ #define DECIMAL_SIZE_OF_BITS(n) (((n) * 3010 + 9998) / 9999) +/** an approximation of decimal representation size for n-bytes */ +#define DECIMAL_SIZE_OF_BYTES(n) DECIMAL_SIZE_OF_BITS((n) * CHAR_BIT) + +/** + * An approximation of decimal representation size. `expr` may be a + * type name + */ +#define DECIMAL_SIZE_OF(expr) DECIMAL_SIZE_OF_BYTES(sizeof(expr)) + /** * Character to number mapping like `'a'` -> `10`, `'b'` -> `11` etc. For * punctuation etc., the value is -1. "36" terminology comes from the fact |