bug-diffutils
[Top][All Lists]
Advanced

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

Re: [bug-diffutils] [PATCH] diff: Encode file names with special charact


From: Jim Meyering
Subject: Re: [bug-diffutils] [PATCH] diff: Encode file names with special characters
Date: Mon, 10 Sep 2012 12:27:59 +0200

Andreas Gruenbacher wrote:
> Here is an updated version with a NEWS entry and a test case.
>
> Note that the test case might not survive sending over email as it
> contains a special character.  Maybe the test case can be turned into
> plain text somehow and stay reasonably portable.
...
> * src/util.c (c_escape_char): New function.
> (c_escape): New function.
> (begin_output): Escape file names when needed.
> * src/context.c (print_context_header): New names parameter.
> (print_context_label): New name parameter.
> * src/diff.h (print_context_header): Change prototype.
> * tests/filename-quoting: New file.
> * NEWS: Document this change.
...
> +char *
> +c_escape (char const *str)
> +{
> +  char const *s;
> +  unsigned int plus = 0;
> +  bool must_quote = false;
> +
> +  for (s = str; *s; s++)
> +    {
> +      char c = *s;
> +
> +      if (c == ' ')
> +     {
> +       must_quote = true;
> +       continue;
> +     }
> +      switch (c_escape_char (*s))
> +     {
> +       case 1:
> +         plus += 3;
> +         /* fall through */
> +       case 0:
> +         break;
> +       default:
> +         plus++;
> +         break;
> +     }
> +    }

Thanks for doing this.  I've begun reviewing it.
Here's one change I would like to squash in,
to make this function static, to adjust a type,
to avoid an strlen call and to adjust code layout.
Ok with you?

diff --git a/src/util.c b/src/util.c
index c488a7d..c02d156 100644
--- a/src/util.c
+++ b/src/util.c
@@ -184,11 +184,11 @@ static char c_escape_char (char c)
   }
 }

-char *
+static char *
 c_escape (char const *str)
 {
   char const *s;
-  unsigned int plus = 0;
+  size_t plus = 0;
   bool must_quote = false;

   for (s = str; *s; s++)
@@ -215,9 +215,10 @@ c_escape (char const *str)

   if (must_quote || plus)
     {
-      char *buffer, *b;
+      size_t s_len = s - str;
+      char *buffer = xmalloc (s_len + plus + 3);
+      char *b = buffer;

-      b = buffer = xmalloc (strlen (str) + plus + 3);
       *b++ = '"';
       for (s = str; *s; s++)
        {
@@ -245,8 +246,8 @@ c_escape (char const *str)
       *b = 0;
       return buffer;
     }
-  else
-    return (char *) str;
+
+  return (char *) str;
 }

 void



reply via email to

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