From e9bab734900d62af2bf2fc873dd480e7503335b9 Mon Sep 17 00:00:00 2001 From: Ralph Corderoy Date: Sun, 14 May 2017 11:23:33 +0100 Subject: [PATCH] Use new PLURALS(n) macro instead of variety of tests. Whether to output a plural noun in a message was decided by ternary operators that tested n==1, n!=1, n>1, etc. Make them all consistent by using PLURALS(n). --- h/mh.h | 9 +++++++++ sbr/utils.c | 5 +++++ uip/burst.c | 2 +- uip/folder.c | 6 +++--- uip/forw.c | 6 +++--- uip/mhlsbr.c | 4 ++-- uip/msgchk.c | 2 +- uip/pick.c | 2 +- uip/post.c | 8 +++----- 9 files changed, 28 insertions(+), 16 deletions(-) diff --git a/h/mh.h b/h/mh.h index 77671f8d..22e3a0b5 100644 --- a/h/mh.h +++ b/h/mh.h @@ -51,6 +51,15 @@ typedef unsigned char boolean; /* not int so we can pack in a structure */ /* FENDNULL fends off NULL by giving an empty string instead. */ #define FENDNULL(s) ((s) ? (s) : "") +/* PLURALS gives a pointer to the string "s" when n isn't 1, and to the + * empty string "" when it is. Suitable for obtaining the plural `s' + * used for English nouns. It treats -1 as plural, as does GNU gettext. + * Having output vary for plurals is annoying for those writing parsers; + * better to phrase the output such that no test is needed, e.g. + * "messages found: 42". */ +extern const char plurals[]; +#define PLURALS(n) (plurals + ((n) == 1)) + /* * char array that keeps track of size in both bytes and characters * Usage note: diff --git a/sbr/utils.c b/sbr/utils.c index 4c87b38a..ef7df4aa 100644 --- a/sbr/utils.c +++ b/sbr/utils.c @@ -17,6 +17,11 @@ extern void remove_registered_files_atexit(); extern char *mhdocdir; +/* plurals gives the letter ess to indicate a plural noun, or an empty + * string as plurals+1 for the singular noun. Used by the PLURALS + * macro. */ +const char plurals[] = "s"; + /* * We allocate space for messages (msgs array) * this number of elements at a time. diff --git a/uip/burst.c b/uip/burst.c index 0f62db74..0b15ebc9 100644 --- a/uip/burst.c +++ b/uip/burst.c @@ -172,7 +172,7 @@ main (int argc, char **argv) if ((numburst = find_delim (msgnum, smsgs, &mimesw)) >= 1) { if (verbosw) printf ("%d message%s exploded from digest %d\n", - numburst, numburst > 1 ? "s" : "", msgnum); + numburst, PLURALS(numburst), msgnum); burst (&mp, msgnum, smsgs, numburst, inplace, verbosw, maildir, mimesw); } else { diff --git a/uip/folder.c b/uip/folder.c index 5fd411cd..077fa053 100644 --- a/uip/folder.c +++ b/uip/folder.c @@ -570,7 +570,7 @@ print_folders (void) } else { printf ("has %*d message%1s (%*d-%*d)", nummsgdigits, fi[i].nummsg, - (fi[i].nummsg == 1) ? "" : "s", + PLURALS(fi[i].nummsg), lowmsgdigits, fi[i].lowmsg, hghmsgdigits, fi[i].hghmsg); if (fi[i].curmsg >= fi[i].lowmsg && fi[i].curmsg <= fi[i].hghmsg) { @@ -592,8 +592,8 @@ print_folders (void) if (all) putchar('\n'); printf ("TOTAL = %d message%s in %d folder%s.\n", - total_msgs, total_msgs != 1 ? "s" : "", - total_folders, total_folders != 1 ? "s" : ""); + total_msgs, PLURALS(total_msgs), + total_folders, PLURALS(total_folders)); } fflush (stdout); diff --git a/uip/forw.c b/uip/forw.c index 8746d45d..ef536a56 100644 --- a/uip/forw.c +++ b/uip/forw.c @@ -588,7 +588,7 @@ copy_draft (int out, char *digest, char *file, int volume, int issue, int dashst if (msgnum == mp->lowsel) { snprintf (bp, buflen, " Forwarded Message%s", - mp->numsel > 1 ? "s" : ""); + PLURALS(mp->numsel)); } else { snprintf (bp, buflen, " Message %d", msgcnt); } @@ -625,7 +625,7 @@ copy_draft (int out, char *digest, char *file, int volume, int issue, int dashst strncpy (buffer, delim4, sizeof(buffer)); } else { snprintf (buffer, sizeof(buffer), "\n------- End of Forwarded Message%s\n", - mp->numsel > 1 ? "s" : ""); + PLURALS(mp->numsel)); } if (write (out, buffer, strlen (buffer)) < 0) { advise (drft, "write"); @@ -657,7 +657,7 @@ copy_mime_draft (int out) char buffer[BUFSIZ]; snprintf (buffer, sizeof(buffer), "#forw [forwarded message%s] +%s", - mp->numsel == 1 ? "" : "s", mp->foldpath); + PLURALS(mp->numsel), mp->foldpath); if (write (out, buffer, strlen (buffer)) < 0) { advise (drft, "write"); } diff --git a/uip/mhlsbr.c b/uip/mhlsbr.c index ea0c7d32..e2d97607 100644 --- a/uip/mhlsbr.c +++ b/uip/mhlsbr.c @@ -521,7 +521,7 @@ mhl (int argc, char **argv) } else printf ("\n------- End of Forwarded Message%s\n", - vecp > 1 ? "s" : ""); + PLURALS(vecp)); } fflush(stdout); @@ -946,7 +946,7 @@ mhlfile (FILE *fp, char *mname, int ofilen, int ofilec) else { printf ("\n-------"); if (ofilen == 1) - printf (" Forwarded Message%s", ofilec > 1 ? "s" : ""); + printf (" Forwarded Message%s", PLURALS(ofilec)); else printf (" Message %d", ofilen); printf ("\n\n"); diff --git a/uip/msgchk.c b/uip/msgchk.c index 2fede57e..44d93528 100644 --- a/uip/msgchk.c +++ b/uip/msgchk.c @@ -399,7 +399,7 @@ remotemail (char *host, char *port, char *user, char *proxy, int notifysw, printf ("%s has ", user); printf ("%d message%s (%d bytes)", - nmsgs, nmsgs != 1 ? "s" : "", nbytes); + nmsgs, PLURALS(nmsgs), nbytes); } else notifysw = 0; diff --git a/uip/pick.c b/uip/pick.c index c5025888..eb7e3573 100644 --- a/uip/pick.c +++ b/uip/pick.c @@ -304,7 +304,7 @@ main (int argc, char **argv) * Print total matched if not printing each matched message number. */ if (!listsw) { - printf ("%d hit%s\n", mp2->numsel, mp2->numsel == 1 ? "" : "s"); + printf ("%d hit%s\n", mp2->numsel, PLURALS(mp2->numsel)); } svector_free (seqs); diff --git a/uip/post.c b/uip/post.c index 3c9a61e8..021aba81 100644 --- a/uip/post.c +++ b/uip/post.c @@ -1566,18 +1566,16 @@ find_prefix (void) } -#define plural(x) (x == 1 ? "" : "s") - static void chkadr (void) { if (badadr && unkadr) die (NULL, "%d address%s unparsable, %d addressee%s undeliverable", - badadr, plural (badadr), unkadr, plural (badadr)); + badadr, PLURALS(badadr), unkadr, PLURALS(badadr)); if (badadr) - die (NULL, "%d address%s unparsable", badadr, plural (badadr)); + die (NULL, "%d address%s unparsable", badadr, PLURALS(badadr)); if (unkadr) - die (NULL, "%d addressee%s undeliverable", unkadr, plural (unkadr)); + die (NULL, "%d addressee%s undeliverable", unkadr, PLURALS(unkadr)); } -- 2.56.0