Avoid doubling \ in common case of "c-maybe" quoting style.
authorEric Blake <ebb9@byu.net>
Sat, 16 Feb 2008 14:40:47 +0000 (07:40 -0700)
committerEric Blake <ebb9@byu.net>
Sat, 16 Feb 2008 14:41:39 +0000 (07:41 -0700)
* lib/quotearg.c (quotearg_buffer_restyled): Don't escape \ when
eliding outer quotes.
* lib/quotearg.h: Document this.
* tests/test-quotearg.c (result_strings, inputs, results_g)
(flag_results, locale_results): Test it by adding a new string to
each test group.
(compare_strings): Test new string.

Signed-off-by: Eric Blake <ebb9@byu.net>
ChangeLog
lib/quotearg.c
lib/quotearg.h
tests/test-quotearg.c

index e2f0b0d..468a04f 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,14 @@
+2008-02-16  Eric Blake  <ebb9@byu.net>
+
+       Avoid doubling \ in common case of "c-maybe" quoting style.
+       * lib/quotearg.c (quotearg_buffer_restyled): Don't escape \ when
+       eliding outer quotes.
+       * lib/quotearg.h: Document this.
+       * tests/test-quotearg.c (result_strings, inputs, results_g)
+       (flag_results, locale_results): Test it by adding a new string to
+       each test group.
+       (compare_strings): Test new string.
+
 2008-02-13  Eric Blake  <ebb9@byu.net>
 
        Avoid trigraph quoting in default output.
index c599acd..75fbc72 100644 (file)
@@ -373,7 +373,11 @@ quotearg_buffer_restyled (char *buffer, size_t buffersize,
        case '\r': esc = 'r'; goto c_and_shell_escape;
        case '\t': esc = 't'; goto c_and_shell_escape;
        case '\v': esc = 'v'; goto c_escape;
-       case '\\': esc = c; goto c_and_shell_escape;
+       case '\\': esc = c;
+         /* No need to escape the escape if we are trying to elide
+            outer quotes and nothing else is problematic.  */
+         if (backslash_escapes && elide_outer_quotes && quote_string_len)
+           goto store_c;
 
        c_and_shell_escape:
          if (quoting_style == shell_always_quoting_style
index 078d9b6..7700107 100644 (file)
@@ -169,7 +169,8 @@ enum quoting_flags
     QA_ELIDE_NULL_BYTES = 0x01,
 
     /* Omit the surrounding quote characters if no escaped characters
-       are encountered.  */
+       are encountered.  Note that if no other character needs
+       escaping, then neither does the escape character.  */
     QA_ELIDE_OUTER_QUOTES = 0x02,
 
     /* In the c_quoting_style and c_maybe_quoting_style, split ANSI
index 51f5ce7..a343169 100644 (file)
@@ -60,6 +60,7 @@ struct result_strings {
   char const *str3; /* Translation of "simple".  */
   char const *str4; /* Translation of " \t\n'\"\033?""?/\\".  */
   char const *str5; /* Translation of "a:b".  */
+  char const *str6; /* Translation of "a\\b".  */
 };
 
 struct result_groups {
@@ -69,103 +70,112 @@ struct result_groups {
 };
 
 static struct result_strings inputs = {
-  "", "\0001\0", 3, "simple", " \t\n'\"\033?""?/\\", "a:b"
+  "", "\0001\0", 3, "simple", " \t\n'\"\033?""?/\\", "a:b", "a\\b"
 };
 
 static struct result_groups results_g[] = {
   /* literal_quoting_style */
-  { { "", "\0""1\0", 3, "simple", " \t\n'\"\033?""?/\\", "a:b" },
-    { "", "1", 1, "simple", " \t\n'\"\033?""?/\\", "a:b" },
-    { "", "1", 1, "simple", " \t\n'\"\033?""?/\\", "a:b" } },
+  { { "", "\0""1\0", 3, "simple", " \t\n'\"\033?""?/\\", "a:b", "a\\b" },
+    { "", "1", 1, "simple", " \t\n'\"\033?""?/\\", "a:b", "a\\b" },
+    { "", "1", 1, "simple", " \t\n'\"\033?""?/\\", "a:b", "a\\b" } },
 
   /* shell_quoting_style */
-  { { "''", "\0""1\0", 3, "simple", "' \t\n'\\''\"\033?""?/\\'", "a:b" },
-    { "''", "1", 1, "simple", "' \t\n'\\''\"\033?""?/\\'", "a:b" },
-    { "''", "1", 1, "simple", "' \t\n'\\''\"\033?""?/\\'", "'a:b'" } },
+  { { "''", "\0""1\0", 3, "simple", "' \t\n'\\''\"\033?""?/\\'", "a:b",
+      "'a\\b'" },
+    { "''", "1", 1, "simple", "' \t\n'\\''\"\033?""?/\\'", "a:b",
+      "'a\\b'" },
+    { "''", "1", 1, "simple", "' \t\n'\\''\"\033?""?/\\'", "'a:b'",
+      "'a\\b'" } },
 
   /* shell_always_quoting_style */
-  { { "''", "'\0""1\0'", 5, "'simple'", "' \t\n'\\''\"\033?""?/\\'", "'a:b'" },
-    { "''", "'1'", 3, "'simple'", "' \t\n'\\''\"\033?""?/\\'", "'a:b'" },
-    { "''", "'1'", 3, "'simple'", "' \t\n'\\''\"\033?""?/\\'", "'a:b'" } },
+  { { "''", "'\0""1\0'", 5, "'simple'", "' \t\n'\\''\"\033?""?/\\'", "'a:b'",
+      "'a\\b'" },
+    { "''", "'1'", 3, "'simple'", "' \t\n'\\''\"\033?""?/\\'", "'a:b'",
+      "'a\\b'" },
+    { "''", "'1'", 3, "'simple'", "' \t\n'\\''\"\033?""?/\\'", "'a:b'",
+      "'a\\b'" } },
 
   /* c_quoting_style */
   { { "\"\"", "\"\\0001\\0\"", 9, "\"simple\"",
-      "\" \\t\\n'\\\"\\033?""?/\\\\\"", "\"a:b\"" },
+      "\" \\t\\n'\\\"\\033?""?/\\\\\"", "\"a:b\"", "\"a\\\\b\"" },
     { "\"\"", "\"\\0001\\0\"", 9, "\"simple\"",
-      "\" \\t\\n'\\\"\\033?""?/\\\\\"", "\"a:b\"" },
+      "\" \\t\\n'\\\"\\033?""?/\\\\\"", "\"a:b\"", "\"a\\\\b\"" },
     { "\"\"", "\"\\0001\\0\"", 9, "\"simple\"",
-      "\" \\t\\n'\\\"\\033?""?/\\\\\"", "\"a\\:b\"" } },
+      "\" \\t\\n'\\\"\\033?""?/\\\\\"", "\"a\\:b\"", "\"a\\\\b\"" } },
 
   /* c_maybe_quoting_style */
   { { "", "\"\\0001\\0\"", 9, "simple", "\" \\t\\n'\\\"\\033?""?/\\\\\"",
-      "a:b" },
+      "a:b", "a\\b" },
     { "", "\"\\0001\\0\"", 9, "simple", "\" \\t\\n'\\\"\\033?""?/\\\\\"",
-      "a:b" },
+      "a:b", "a\\b" },
     { "", "\"\\0001\\0\"", 9, "simple", "\" \\t\\n'\\\"\\033?""?/\\\\\"",
-      "\"a:b\"" } },
+      "\"a:b\"", "a\\b" } },
 
   /* escape_quoting_style */
-  { { "", "\\0001\\0", 7, "simple", " \\t\\n'\"\\033?""?/\\\\", "a:b" },
-    { "", "\\0001\\0", 7, "simple", " \\t\\n'\"\\033?""?/\\\\", "a:b" },
-    { "", "\\0001\\0", 7, "simple", " \\t\\n'\"\\033?""?/\\\\", "a\\:b" } },
+  { { "", "\\0001\\0", 7, "simple", " \\t\\n'\"\\033?""?/\\\\", "a:b",
+      "a\\\\b" },
+    { "", "\\0001\\0", 7, "simple", " \\t\\n'\"\\033?""?/\\\\", "a:b",
+      "a\\\\b" },
+    { "", "\\0001\\0", 7, "simple", " \\t\\n'\"\\033?""?/\\\\", "a\\:b",
+      "a\\\\b" } },
 
   /* locale_quoting_style */
   { { "`'", "`\\0001\\0'", 9, "`simple'", "` \\t\\n\\'\"\\033?""?/\\\\'",
-      "`a:b'" },
+      "`a:b'", "`a\\\\b'" },
     { "`'", "`\\0001\\0'", 9, "`simple'", "` \\t\\n\\'\"\\033?""?/\\\\'",
-      "`a:b'" },
+      "`a:b'", "`a\\\\b'" },
     { "`'", "`\\0001\\0'", 9, "`simple'", "` \\t\\n\\'\"\\033?""?/\\\\'",
-      "`a\\:b'" } },
+      "`a\\:b'", "`a\\\\b'" } },
 
   /* clocale_quoting_style */
   { { "\"\"", "\"\\0001\\0\"", 9, "\"simple\"",
-      "\" \\t\\n'\\\"\\033?""?/\\\\\"", "\"a:b\"" },
+      "\" \\t\\n'\\\"\\033?""?/\\\\\"", "\"a:b\"", "\"a\\\\b\"" },
     { "\"\"", "\"\\0001\\0\"", 9, "\"simple\"",
-      "\" \\t\\n'\\\"\\033?""?/\\\\\"", "\"a:b\"" },
+      "\" \\t\\n'\\\"\\033?""?/\\\\\"", "\"a:b\"", "\"a\\\\b\"" },
     { "\"\"", "\"\\0001\\0\"", 9, "\"simple\"",
-      "\" \\t\\n'\\\"\\033?""?/\\\\\"", "\"a\\:b\"" } }
+      "\" \\t\\n'\\\"\\033?""?/\\\\\"", "\"a\\:b\"", "\"a\\\\b\"" } }
 };
 
 static struct result_groups flag_results[] = {
   /* literal_quoting_style and QA_ELIDE_NULL_BYTES */
-  { { "", "1", 1, "simple", " \t\n'\"\033?""?/\\", "a:b" },
-    { "", "1", 1, "simple", " \t\n'\"\033?""?/\\", "a:b" },
-    { "", "1", 1, "simple", " \t\n'\"\033?""?/\\", "a:b" } },
+  { { "", "1", 1, "simple", " \t\n'\"\033?""?/\\", "a:b", "a\\b" },
+    { "", "1", 1, "simple", " \t\n'\"\033?""?/\\", "a:b", "a\\b" },
+    { "", "1", 1, "simple", " \t\n'\"\033?""?/\\", "a:b", "a\\b" } },
 
   /* c_quoting_style and QA_ELIDE_OUTER_QUOTES */
   { { "", "\"\\0001\\0\"", 9, "simple", "\" \\t\\n'\\\"\\033?""?/\\\\\"",
-      "a:b" },
+      "a:b", "a\\b" },
     { "", "\"\\0001\\0\"", 9, "simple", "\" \\t\\n'\\\"\\033?""?/\\\\\"",
-      "a:b" },
+      "a:b", "a\\b" },
     { "", "\"\\0001\\0\"", 9, "simple", "\" \\t\\n'\\\"\\033?""?/\\\\\"",
-      "\"a:b\"" } },
+      "\"a:b\"", "a\\b" } },
 
   /* c_quoting_style and QA_SPLIT_TRIGRAPHS */
   { { "\"\"", "\"\\0001\\0\"", 9, "\"simple\"",
-      "\" \\t\\n'\\\"\\033?\"\"?/\\\\\"", "\"a:b\"" },
+      "\" \\t\\n'\\\"\\033?\"\"?/\\\\\"", "\"a:b\"", "\"a\\\\b\"" },
     { "\"\"", "\"\\0001\\0\"", 9, "\"simple\"",
-      "\" \\t\\n'\\\"\\033?\"\"?/\\\\\"", "\"a:b\"" },
+      "\" \\t\\n'\\\"\\033?\"\"?/\\\\\"", "\"a:b\"", "\"a\\\\b\"" },
     { "\"\"", "\"\\0001\\0\"", 9, "\"simple\"",
-      "\" \\t\\n'\\\"\\033?\"\"?/\\\\\"", "\"a\\:b\"" } }
+      "\" \\t\\n'\\\"\\033?\"\"?/\\\\\"", "\"a\\:b\"", "\"a\\\\b\"" } }
 };
 
 #if ENABLE_NLS
 static struct result_groups locale_results[] = {
   /* locale_quoting_style */
   { { LQ RQ, LQ "\\0001\\0" RQ, 11, LQ "simple" RQ,
-      LQ " \\t\\n'\"\\033?""?/\\\\" RQ, LQ "a:b" RQ },
+      LQ " \\t\\n'\"\\033?""?/\\\\" RQ, LQ "a:b" RQ, LQ "a\\\\b" RQ },
     { LQ RQ, LQ "\\0001\\0" RQ, 11, LQ "simple" RQ,
-      LQ " \\t\\n'\"\\033?""?/\\\\" RQ, LQ "a:b" RQ },
+      LQ " \\t\\n'\"\\033?""?/\\\\" RQ, LQ "a:b" RQ, LQ "a\\\\b" RQ },
     { LQ RQ, LQ "\\0001\\0" RQ, 11, LQ "simple" RQ,
-      LQ " \\t\\n'\"\\033?""?/\\\\" RQ, LQ "a\\:b" RQ } },
+      LQ " \\t\\n'\"\\033?""?/\\\\" RQ, LQ "a\\:b" RQ, LQ "a\\\\b" RQ } },
 
   /* clocale_quoting_style */
   { { LQ RQ, LQ "\\0001\\0" RQ, 11, LQ "simple" RQ,
-      LQ " \\t\\n'\"\\033?""?/\\\\" RQ, LQ "a:b" RQ },
+      LQ " \\t\\n'\"\\033?""?/\\\\" RQ, LQ "a:b" RQ, LQ "a\\\\b" RQ },
     { LQ RQ, LQ "\\0001\\0" RQ, 11, LQ "simple" RQ,
-      LQ " \\t\\n'\"\\033?""?/\\\\" RQ, LQ "a:b" RQ },
+      LQ " \\t\\n'\"\\033?""?/\\\\" RQ, LQ "a:b" RQ, LQ "a\\\\b" RQ },
     { LQ RQ, LQ "\\0001\\0" RQ, 11, LQ "simple" RQ,
-      LQ " \\t\\n'\"\\033?""?/\\\\" RQ, LQ "a\\:b" RQ } }
+      LQ " \\t\\n'\"\\033?""?/\\\\" RQ, LQ "a\\:b" RQ, LQ "a\\\\b" RQ } }
 };
 #endif /* ENABLE_NLS */
 
@@ -203,6 +213,10 @@ compare_strings (char *(func) (char const *, size_t *),
   len = SIZE_MAX;
   p = func (inputs.str5, &len);
   compare (results->str5, strlen (results->str5), p, len);
+
+  len = strlen (inputs.str6);
+  p = func (inputs.str6, &len);
+  compare (results->str6, strlen (results->str6), p, len);
 }
 
 static char *