When the length is already known and likely to be short, avoiding the
function call to strlen can be a slight optimization.  Two obvious
places: when ntoa() builds a number and already knows where the \0 is,
and when dumping arguments when the separator is always a single byte.

* src/builtin:c (dump_args): Change type of sep.
(m4_shift, m4_errprint, m4_m4wrap, expand_user_macro): Adjust all
callers.
(ntoa): Add optional end parameter.
(shipout_int, m4_eval, m4_maketemp): Adjust all callers.
* src/debug.c (trace_format): Likewise.
* src/output.c (shipout_text): Likewise.
* src/m4.h (ntoa): Adjust prototype.
---
 src/builtin.c | 34 +++++++++++++++++++---------------
 src/debug.c   |  2 +-
 src/m4.h      |  2 +-
 src/output.c  |  2 +-
 4 files changed, 22 insertions(+), 18 deletions(-)

diff --git a/src/builtin.c b/src/builtin.c
index efd51d9e..3447dc33 100644
--- a/src/builtin.c
+++ b/src/builtin.c
@@ -447,7 +447,7 @@ numeric_arg (token_data *macro, const char *arg, int 
*valuep)
 static char const digits[] = "0123456789abcdefghijklmnopqrstuvwxyz";

 const char *
-ntoa (int32_t value, int radix)
+ntoa (int32_t value, int radix, const char **end)
 {
   bool negative;
   uint32_t uvalue;
@@ -455,6 +455,8 @@ ntoa (int32_t value, int radix)
   char *s = &str[sizeof str];

   *--s = '\0';
+  if (end)
+    *end = s;

   if (value < 0)
     {
@@ -488,9 +490,10 @@ static void
 shipout_int (struct obstack *obs, int val)
 {
   const char *s;
+  const char *e;

-  s = ntoa ((int32_t) val, 10);
-  obstack_grow (obs, s, strlen (s));
+  s = ntoa ((int32_t) val, 10, &e);
+  obstack_grow (obs, s, e - s);
 }

 /*-------------------------------------------------------------------.
@@ -500,15 +503,14 @@ shipout_int (struct obstack *obs, int val)

 static void
 dump_args (struct obstack *obs, int argc, token_data **argv,
-           const char *sep, bool quoted)
+           char sep, bool quoted)
 {
   int i;
-  size_t len = strlen (sep);

   for (i = 1; i < argc; i++)
     {
       if (i > 1)
-        obstack_grow (obs, sep, len);
+        obstack_1grow (obs, sep);
       if (quoted)
         obstack_grow (obs, lquote.string, lquote.length);
       obstack_grow (obs, TOKEN_DATA_TEXT (argv[i]),
@@ -1089,6 +1091,7 @@ m4_eval (struct obstack *obs, int argc, token_data **argv)
   int radix = 10;
   int min = 1;
   const char *s;
+  const char *e;

   if (bad_argc (argv[0], argc, 2, 4))
     return;
@@ -1135,17 +1138,17 @@ m4_eval (struct obstack *obs, int argc, token_data 
**argv)
       return;
     }

-  s = ntoa (value, radix);
+  s = ntoa (value, radix, &e);

   if (*s == '-')
     {
       obstack_1grow (obs, '-');
       s++;
     }
-  for (min -= strlen (s); --min >= 0;)
+  for (min -= e - s; --min >= 0;)
     obstack_1grow (obs, '0');

-  obstack_grow (obs, s, strlen (s));
+  obstack_grow (obs, s, e - s);
 }

 static void
@@ -1290,7 +1293,7 @@ m4_shift (struct obstack *obs, int argc, token_data 
**argv)
 {
   if (bad_argc (argv[0], argc, 2, -1))
     return;
-  dump_args (obs, argc - 1, argv + 1, ",", true);
+  dump_args (obs, argc - 1, argv + 1, ',', true);
 }

 /*--------------------------------------------------------------------------.
@@ -1464,14 +1467,15 @@ m4_maketemp (struct obstack *obs, int argc, token_data 
**argv)
       int len = strlen (str);
       int i;
       int len2;
+      const char *e;

       M4ERROR ((warning_status, 0, _("recommend using mkstemp instead")));
       for (i = len; i > 1; i--)
         if (str[i - 1] != 'X')
           break;
       obstack_grow (obs, str, i);
-      str = ntoa ((int32_t) getpid (), 10);
-      len2 = strlen (str);
+      str = ntoa ((int32_t) getpid (), 10, &e);
+      len2 = e - str;
       if (len2 > len - i)
         obstack_grow0 (obs, str + len2 - (len - i), len - i);
       else
@@ -1502,7 +1506,7 @@ m4_errprint (struct obstack *obs, int argc, token_data 
**argv)
 {
   if (bad_argc (argv[0], argc, 2, -1))
     return;
-  dump_args (obs, argc, argv, " ", false);
+  dump_args (obs, argc, argv, ' ', false);
   obstack_1grow (obs, '\0');
   debug_flush_files ();
   xfprintf (stderr, "%s", (char *) obstack_finish (obs));
@@ -1587,7 +1591,7 @@ m4_m4wrap (struct obstack *obs, int argc, token_data 
**argv)
   if (no_gnu_extensions)
     obstack_grow (obs, ARG (1), strlen (ARG (1)));
   else
-    dump_args (obs, argc, argv, " ", false);
+    dump_args (obs, argc, argv, ' ', false);
   obstack_1grow (obs, '\0');
   push_wrapup ((char *) obstack_finish (obs));
 }
@@ -2262,7 +2266,7 @@ expand_user_macro (struct obstack *obs, symbol *sym,

         case '*': /* all arguments */
         case '@': /* ... same, but quoted */
-          dump_args (obs, argc, argv, ",", *text == '@');
+          dump_args (obs, argc, argv, ',', *text == '@');
           text++;
           break;

diff --git a/src/debug.c b/src/debug.c
index a4017660..876b11f5 100644
--- a/src/debug.c
+++ b/src/debug.c
@@ -290,7 +290,7 @@ trace_format (const char *fmt, ...)

         case 'd':
           d = va_arg (args, int);
-          s = ntoa (d, 10);
+          s = ntoa (d, 10, NULL);
           break;

         default:
diff --git a/src/m4.h b/src/m4.h
index c1fb806b..858cb0de 100644
--- a/src/m4.h
+++ b/src/m4.h
@@ -440,7 +440,7 @@ extern void m4_placeholder (struct obstack *, int, 
token_data **)
   ATTRIBUTE_COLD;
 extern void init_pattern_buffer (struct re_pattern_buffer *,
                                  struct re_registers *);
-extern const char *ntoa (int32_t, int);
+extern const char *ntoa (int32_t, int, const char **);

 extern const builtin *find_builtin_by_addr (builtin_func *);
 extern const builtin *find_builtin_by_name (const char *);
diff --git a/src/output.c b/src/output.c
index 84f1513e..f40253c1 100644
--- a/src/output.c
+++ b/src/output.c
@@ -670,7 +670,7 @@ shipout_text (struct obstack *obs, const char *text, int 
length, int line)
               OUTPUT_CHARACTER ('n');
               OUTPUT_CHARACTER ('e');
               OUTPUT_CHARACTER (' ');
-              for (cursor = ntoa (line, 10); *cursor; cursor++)
+              for (cursor = ntoa (line, 10, NULL); *cursor; cursor++)
                 OUTPUT_CHARACTER (*cursor);
               if (output_current_line < 1 && current_file[0] != '\0')
                 {
-- 
2.48.1


_______________________________________________
M4-patches mailing list
M4-patches@gnu.org
https://lists.gnu.org/mailman/listinfo/m4-patches

Reply via email to