From 3d986f521cac261e29c043be50bb626f7641d841 Mon Sep 17 00:00:00 2001 From: Eric Blake Date: Sat, 16 Feb 2008 07:40:47 -0700 Subject: [PATCH] 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. Signed-off-by: Eric Blake --- ChangeLog | 11 ++++++ lib/quotearg.c | 6 +++- lib/quotearg.h | 3 +- tests/test-quotearg.c | 94 +++++++++++++++++++++++++++++---------------------- 4 files changed, 72 insertions(+), 42 deletions(-) diff --git a/ChangeLog b/ChangeLog index e2f0b0d23..468a04f15 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,14 @@ +2008-02-16 Eric Blake + + 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 Avoid trigraph quoting in default output. diff --git a/lib/quotearg.c b/lib/quotearg.c index c599acdbb..75fbc72f3 100644 --- a/lib/quotearg.c +++ b/lib/quotearg.c @@ -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 diff --git a/lib/quotearg.h b/lib/quotearg.h index 078d9b6b5..770010700 100644 --- a/lib/quotearg.h +++ b/lib/quotearg.h @@ -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 diff --git a/tests/test-quotearg.c b/tests/test-quotearg.c index 51f5ce718..a34316961 100644 --- a/tests/test-quotearg.c +++ b/tests/test-quotearg.c @@ -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 * -- 2.11.0