From cc589a8b1539e9515985081d9498dda2b6b6caf7 Mon Sep 17 00:00:00 2001 From: Nick Bowler Date: Mon, 4 Dec 2023 21:37:43 -0500 Subject: [PATCH] help: Perform some suffix merging. Change the format strings to change the two-space indent to just one space. This looks perfectly fine, and for the normal case enables half the strings to be dropped as they are now suffixes of other strings. We also move the strings in help_print_desc into the table, as one of these is a suffix of the other, but out of the box GCC does not fully perform this optimization. --- src/help-glo.str | 9 ++++++--- src/help-std.str | 15 +++++++++------ src/help.c | 42 ++++++++++++++++++++++++------------------ tests/functions.at | 44 ++++++++++++++++++++++---------------------- 4 files changed, 61 insertions(+), 49 deletions(-) diff --git a/src/help-glo.str b/src/help-glo.str index 90600e9..d0fd3cb 100644 --- a/src/help-glo.str +++ b/src/help-glo.str @@ -5,6 +5,9 @@ # incorporate the output directly into help.c @macro -&fmt_long_optional_arg \ -%s [%s] -&fmt_long_mandatory_arg \ -%s %s -&fmt_long_without_arg \ -%s +&fmt_long_optional_arg \ -%s [%s] +&fmt_long_mandatory_arg \ -%s %s +&fmt_long_without_arg \ -%s + +&&fmt_desc %*s%.*s\n +&&fmt_nl \n diff --git a/src/help-std.str b/src/help-std.str index 09717df..0a6ab3b 100644 --- a/src/help-std.str +++ b/src/help-std.str @@ -5,9 +5,12 @@ # incorporate the output directly into help.c @macro -&fmt_short_optional_arg \ -%c, --%s[=%s] -&fmt_long_optional_arg \ --%s[=%s] -&fmt_short_mandatory_arg \ -%c, --%s=%s -&fmt_long_mandatory_arg \ --%s=%s -&fmt_short_without_arg \ -%c, --%s -&fmt_long_without_arg \ --%s +&fmt_short_optional_arg \ -%c, --%s[=%s] +&fmt_long_optional_arg \ --%s[=%s] +&fmt_short_mandatory_arg \ -%c, --%s=%s +&fmt_long_mandatory_arg \ --%s=%s +&fmt_short_without_arg \ -%c, --%s +&fmt_long_without_arg \ --%s + +&&fmt_desc %*s%.*s\n +&&fmt_nl \n diff --git a/src/help.c b/src/help.c index c537eef..3886c27 100644 --- a/src/help.c +++ b/src/help.c @@ -78,32 +78,38 @@ enum { #if HELP_GETOPT_LONG_ONLY /* String table generated by gen-strtab.awk from help-glo.str */ #define STRTAB_INITIALIZER \ - N_(" -%s [%s]") \ - "\0" N_(" -%s %s") \ - "\0" N_(" -%s") \ + N_(" -%s [%s]") \ + "\0" "%*s%.*s\n" \ + "\0" N_(" -%s %s") \ + "\0" N_(" -%s") \ "" enum { fmt_long_optional_arg = 0, - fmt_long_mandatory_arg = 11, - fmt_long_without_arg = 20 + fmt_long_mandatory_arg = 19, + fmt_long_without_arg = 27, + fmt_desc = 10, + fmt_nl = 17 }; #else /* String table generated by gen-strtab.awk from help-std.str */ #define STRTAB_INITIALIZER \ - N_(" -%c, --%s[=%s]") \ - "\0" N_(" -%c, --%s=%s") \ - "\0" N_(" --%s[=%s]") \ - "\0" N_(" -%c, --%s") \ - "\0" N_(" --%s=%s") \ - "\0" N_(" --%s") \ + STR_L10N_(N_(" --%s[=%s]")) \ + STR_L10N_(N_(" --%s=%s")) \ + STR_L10N_(N_(" --%s")) \ + N_(" -%c, --%s[=%s]") \ + "\0" N_(" -%c, --%s=%s") \ + "\0" N_(" -%c, --%s") \ + "\0" "%*s%.*s\n" \ "" enum { fmt_short_optional_arg = 0, - fmt_long_optional_arg = 32, - fmt_short_mandatory_arg = 17, - fmt_long_mandatory_arg = 56, - fmt_short_without_arg = 44, - fmt_long_without_arg = 66 + fmt_long_optional_arg = 5, + fmt_short_mandatory_arg = 16, + fmt_long_mandatory_arg = 21, + fmt_short_without_arg = 30, + fmt_long_without_arg = 35, + fmt_desc = 41, + fmt_nl = 48 }; #endif @@ -191,9 +197,9 @@ void help_print_desc(const struct option *opt, const char *s, int i, int w) s = pgettext_expr(opt->name, s); do { - size_t n = strcspn(s, "\n"); + size_t n = strcspn(s, &optstr.tab[fmt_nl]); - printf("%*s%.*s\n", n ? i-w : 0, "", (int)n, s); + printf(&optstr.tab[fmt_desc], n ? i-w : 0, "", (int)n, s); s += n + (s[n] != '\0'); w = 0; } while (*s); diff --git a/tests/functions.at b/tests/functions.at index 71a282d..6b092a0 100644 --- a/tests/functions.at +++ b/tests/functions.at @@ -70,17 +70,17 @@ AT_CHECK([m4_join([ ], [--hello-this-is-a-very-long-option 50], [--not-long 12], [--flagged -f \&])], [0], -[[ --foo 7 - -b, --bar 11 - --baz=ARG 11 - -B, --baz=ARG 15 - --quux[=ARG] 14 - -q, --quux[=ARG] 18 - --hello-this-is-a-very-long-option +[[ --foo 6 + -b, --bar 10 + --baz=ARG 10 + -B, --baz=ARG 14 + --quux[=ARG] 13 + -q, --quux[=ARG] 17 + --hello-this-is-a-very-long-option 0 - --hello-this-is-a-very-long-option 36 - --not-long 12 - --flagged 11 + --hello-this-is-a-very-long-option 35 + --not-long 11 + --flagged 10 ]]) AT_CLEANUP @@ -102,17 +102,17 @@ AT_CHECK([m4_join([ ], [--hello-this-is-a-very-long-option 50], [--not-long 12], [--flagged -f \&])], [0], -[[ -foo 6 - -bar 6 - -baz ARG 10 - -baz ARG 10 - -quux [ARG] 13 - -quux [ARG] 13 - -hello-this-is-a-very-long-option +[[ -foo 5 + -bar 5 + -baz ARG 9 + -baz ARG 9 + -quux [ARG] 12 + -quux [ARG] 12 + -hello-this-is-a-very-long-option 0 - -hello-this-is-a-very-long-option 35 - -not-long 11 - -flagged 10 + -hello-this-is-a-very-long-option 34 + -not-long 10 + -flagged 9 ]]) AT_CLEANUP @@ -132,8 +132,8 @@ BEGIN { print zenkaku, hankaku >"args" - print " " zenkaku "\t" "8" - print " " hankaku "\t" "8" + print " " zenkaku "\t" "7" + print " " hankaku "\t" "7" } EOF -- 2.43.2