poke-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[PATCH] Use strchrnul in pkl_trans1_ps_print_stmt


From: Tim Rühsen
Subject: [PATCH] Use strchrnul in pkl_trans1_ps_print_stmt
Date: Sun, 5 Apr 2020 17:03:11 +0200

2020-04-04  Tim Rühsen  <address@hidden>

        * bootstrap.conf: Add gnulib module strchrnul.
        * src/pkl-trans.c: Include string.h and xstrndup.h.
        (pkl_trans1_ps_print_stmt): Code cleanup using strchrnul.
---
 ChangeLog       |  6 ++++++
 bootstrap.conf  |  1 +
 src/pkl-trans.c | 31 +++++++------------------------
 3 files changed, 14 insertions(+), 24 deletions(-)

diff --git a/bootstrap.conf b/bootstrap.conf
index 664a9ab9..381a6f5d 100644
--- a/bootstrap.conf
+++ b/bootstrap.conf
@@ -44,6 +44,7 @@ gnulib_modules="
   secure_getenv
   socket
   stdbool
+  strchrnul
   streq
   strtoull
   tempname
diff --git a/src/pkl-trans.c b/src/pkl-trans.c
index ea2b73ee..e7b50546 100644
--- a/src/pkl-trans.c
+++ b/src/pkl-trans.c
@@ -21,7 +21,9 @@
 #include <gettext.h>
 #define _(str) gettext (str)
 #include <stdio.h>
+#include <string.h>
 #include <xalloc.h>
+#include "xstrndup.h"

 #include "pk-utils.h"

@@ -520,17 +522,8 @@ PKL_PHASE_BEGIN_HANDLER (pkl_trans1_ps_print_stmt)
   /* Process the prefix string, if any.  */
   if (*p != '%')
     {
-      char *prefix = xmalloc (strlen (fmt) + 1);
-      size_t j = 0;
-
-      while (*p != '%' && *p != '\0')
-            {
-              prefix[j] = *p;
-              p++;
-              j++;
-            }
-      prefix[j] = '\0';
-      PKL_AST_PRINT_STMT_PREFIX (print_stmt) = prefix;
+      p = strchrnul (fmt, '%');
+      PKL_AST_PRINT_STMT_PREFIX (print_stmt) = xstrndup (fmt, p - fmt);
     }

   /* Process the format string.  */
@@ -796,19 +789,9 @@ PKL_PHASE_BEGIN_HANDLER (pkl_trans1_ps_print_stmt)
       /* Add the optional suffix to the argument.  */
       if (*p != '\0' && *p != '%')
         {
-          /* This argument has a prefix.  */
-          size_t j;
-          char *suffix = xmalloc (strlen (fmt) + 1);
-
-          j = 0;
-          while (*p != '%' && *p != '\0')
-            {
-              suffix[j] = *p;
-              p++;
-              j++;
-            }
-          suffix[j] = '\0';
-          PKL_AST_PRINT_STMT_ARG_SUFFIX (arg) = suffix;
+          char *end = strchrnul (p, '%');
+          PKL_AST_PRINT_STMT_ARG_SUFFIX (arg) = xstrndup (p, end - p);
+          p = end;
         }
     }

--
2.26.0




reply via email to

[Prev in Thread] Current Thread [Next in Thread]