From 619f7eab8fa93116e66d839d057a276e54a59085 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?John=20Ankarstr=C3=B6m?= Date: Wed, 21 Jul 2021 14:44:47 +0200 Subject: pf: Try not to repeat puncuation after % --- fref.lex | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/fref.lex b/fref.lex index 5a35bd4..2b0861d 100644 --- a/fref.lex +++ b/fref.lex @@ -164,7 +164,7 @@ entry() /* Print date. */ if(!pf(" (%).\n", e.da) && e.a){ - if(e.au[i][strlen(e.au[i])-1] != '.') + if(!strpbrk(e.au[i]+strlen(e.au[i])-1, ".,?!")) pf("."); pf("\n"); } @@ -273,13 +273,14 @@ gettrans(char *lg) * This function interpolates any number of strings into a formatted * string, which is printed only if all interpolated strings are non-empty. * Interpolation points are marked by % or *. If a string is interpolated - * using %, the function empties it after printing it. + * using %, the function empties it after printing it. Additionally, the + * function tries not to repeat punctuation after %. */ int pf(char *fmt, ...) { char *buf, *p, **fs; - int i, n, sz; + int i, n, punct, sz; va_list ap; /* Count interpolated strings. */ @@ -303,19 +304,33 @@ pf(char *fmt, ...) } va_end(ap); + /* Keep track of prior punctuation. */ + punct = 0; + /* Print formatted string. */ for(i = 0; *fmt; fmt++) switch(*fmt){ case '%': + p = fs[i]+strlen(fs[i])-1; + punct = strpbrk(p, ".,?!") ? 1 : 0; printf("%s", fs[i]); fs[i][0] = 0; i++; break; case '*': + punct = 0; printf("%s", fs[i]); i++; break; + case '.': + case ',': + case '?': + case '!': + if(punct && punct--) + break; + /* FALLTHROUGH */ default: + punct = 0; putchar(*fmt); } return 1; -- cgit v1.2.3