From: Nick Bowler Date: Wed, 13 Dec 2023 03:01:31 +0000 (-0500) Subject: cdecl99: Optimize print_version a bit. X-Git-Tag: v1.3~60 X-Git-Url: https://git.draconx.ca/gitweb/cdecl99.git/commitdiff_plain/09d517aa84f99c3686803d156cde10fc0c02bdc2 cdecl99: Optimize print_version a bit. Instead of a whole bunch of separate print calls, convert print_version to a single call to a printing function, with a single string argument. Compilers do not seem to coalesce multiple consecutive prints together, so doing it explicitly gives a modest reduction in code size. Wrap that all up into a new set of macros so that the same trick can also be shared with the test applications. --- diff --git a/Makefile.am b/Makefile.am index 6888d83..90818f5 100644 --- a/Makefile.am +++ b/Makefile.am @@ -32,7 +32,7 @@ dist_man_MANS = doc/cdecl99.1 doc/libcdecl.3 noinst_HEADERS = conf_pre.h conf_post.h common/src/help.h common/src/tap.h \ common/src/xtra.h src/cdecl.h src/scan.h src/parse.h \ - t/declgen.h t/test.h + src/version.h t/declgen.h t/test.h noinst_DATA = $(MOFILES) diff --git a/src/cdecl99.c b/src/cdecl99.c index 6eb00df..5f60037 100644 --- a/src/cdecl99.c +++ b/src/cdecl99.c @@ -29,15 +29,14 @@ #include #include -#include #include #include "cdecl99.h" #include "cdecl.h" #include "help.h" #include "xtra.h" -#include "copysym.h" #include "options.h" +#include "version.h" #if HAVE_READLINE_READLINE_H # include @@ -77,13 +76,7 @@ void print_error(const char *fmt, ...) static void print_version(void) { - const char *copysign = copyright_symbol(locale_charset()); - - puts(PACKAGE_STRING); - printf("Copyright %s 2023 Nick Bowler.\n", copysign); - puts("License GPLv3+: GNU GPL version 3 or any later version."); - puts("This is free software: you are free to change and redistribute it."); - puts("There is NO WARRANTY, to the extent permitted by law."); + do_print_version(PACKAGE_STRING); } static void print_usage(FILE *f) diff --git a/src/version.h b/src/version.h new file mode 100644 index 0000000..9d41d11 --- /dev/null +++ b/src/version.h @@ -0,0 +1,42 @@ +/* + * Copyright © 2023 Nick Bowler + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#ifndef CDECL99_VERSION_H_ +#define CDECL99_VERSION_H_ + +#define version_blurb(s, copyright) \ + s "\n" \ + copyright "2023 Nick Bowler\n" \ + "License GPLv3+: GNU GPL version 3 or any later version.\n" \ + "This is free software: you are free to change and redistribute it.\n" \ + "There is NO WARRANTY, to the extent permitted by law." \ + +#if ENABLE_NLS && !VERSION_NO_COPYRIGHT_SYMBOL +#include +#include "copysym.h" + +#define do_print_version(s) do { \ + printf(version_blurb(s, "Copyright %s ")"\n", \ + copyright_symbol(locale_charset())); \ +} while (0) +#else +#define do_print_version(s) do { \ + puts(version_blurb(s, "Copyright (C) ")); \ +} while (0) +#endif + +#endif diff --git a/t/rendertest.c b/t/rendertest.c index 14faafb..e5299c2 100644 --- a/t/rendertest.c +++ b/t/rendertest.c @@ -26,7 +26,7 @@ #include #include "test.h" -#define PROGNAME "outbuf" +#define PROGNAME "rendertest" static const char *progname = PROGNAME; static const char sopts[] = "n:ECVH"; static const struct option lopts[] = { diff --git a/t/test.h b/t/test.h index 091ad88..2d77abc 100644 --- a/t/test.h +++ b/t/test.h @@ -43,7 +43,6 @@ void test_explain_decl(struct cdecl *decl); bool strict_strtoul(unsigned long *val, const char *str, int base); -void test_print_version(const char *program); void test_print_options(const struct option *lopts); /* @@ -71,4 +70,10 @@ static inline int test_rng_50_50(struct test_rng *rng) return test_rng_uniform_int(rng, 2) == 0; } +#define VERSION_NO_COPYRIGHT_SYMBOL 1 +#include "version.h" + +#define test_print_version(s) \ + do_print_version(s " (" PACKAGE_NAME ") " PACKAGE_VERSION) + #endif diff --git a/t/testlib.c b/t/testlib.c index 8edb423..c6b1975 100644 --- a/t/testlib.c +++ b/t/testlib.c @@ -89,15 +89,6 @@ bool strict_strtoul(unsigned long *val, const char *str, int base) return true; } -void test_print_version(const char *program) -{ - printf("%s (%s) %s\n", program, PACKAGE_NAME, PACKAGE_VERSION); - puts("Copyright (C) 2023 Nick Bowler."); - puts("License GPLv3+: GNU GPL version 3 or later ."); - puts("This is free software: you are free to change and redistribute it."); - puts("There is NO WARRANTY, to the extent permitted by law."); -} - void test_print_options(const struct option *lopts) { const struct option *opt;