Replace only use of `snprintf` in parser
authorNobuyoshi Nakada <[email protected]>
Fri, 25 Aug 2023 14:27:52 +0000 (25 23:27 +0900)
committerNobuyoshi Nakada <[email protected]>
Fri, 25 Aug 2023 14:34:02 +0000 (25 23:34 +0900)
parse.y
ruby_parser.c
rubyparser.h

diff --git a/parse.y b/parse.y
index cf8ae37..2075784 100644 (file)
--- a/parse.y
+++ b/parse.y
@@ -7710,6 +7710,13 @@ parser_mixed_escape(struct parser_params *p, const char *beg, rb_encoding *enc1,
     p->lex.pcur = pos;
 }
 
+static inline char
+nibble_char_upper(unsigned int c)
+{
+    c &= 0xf;
+    return c + (c < 10 ? '0' : 'A' - 10);
+}
+
 static int
 tokadd_string(struct parser_params *p,
               int func, int term, int paren, long *nest,
@@ -7799,12 +7806,11 @@ tokadd_string(struct parser_params *p,
                         pushback(p, c);
                         c = read_escape(p, 0, enc);
 
-                        int i;
-                        char escbuf[5];
-                        snprintf(escbuf, sizeof(escbuf), "\\x%02X", c);
-                        for (i = 0; i < 4; i++) {
-                            tokadd(p, escbuf[i]);
-                        }
+                        char *t = tokspace(p, rb_strlen_lit("\\x00"));
+                        *t++ = '\\';
+                        *t++ = 'x';
+                        *t++ = nibble_char_upper(c >> 4);
+                        *t++ = nibble_char_upper(c);
                         continue;
                       }
                     }
index 77328d3..6aaa867 100644 (file)
@@ -774,7 +774,6 @@ rb_parser_config_initialize(rb_parser_config_t *config)
     config->long2int = rb_long2int;
     config->special_const_p = special_const_p;
     config->builtin_type = builtin_type;
-    config->snprintf = snprintf;
 
     config->node_case_when_optimizable_literal = rb_node_case_when_optimizable_literal;
 
index 388f6c0..e95b71f 100644 (file)
@@ -601,11 +601,6 @@ typedef struct rb_parser_config_struct {
     int (*long2int)(long);
     int (*special_const_p)(VALUE);
     int (*builtin_type)(VALUE);
-#undef snprintf
-    int (*snprintf)(char *str, size_t n, char const *fmt, ...);
-#ifdef RUBY_SUBST_H
-# define snprintf(...) ruby_snprintf(__VA_ARGS__)
-#endif
 
     VALUE (*node_case_when_optimizable_literal)(const NODE *const node);