From 68f7fb971322ffb03c8b5deba60ee25e31c77bfd Mon Sep 17 00:00:00 2001 From: Thomas Voss Date: Fri, 23 Sep 2022 20:55:00 +0200 Subject: Fix text centering of multibyte characters --- center.c | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/center.c b/center.c index 37a643e..be6d9ee 100644 --- a/center.c +++ b/center.c @@ -113,8 +113,10 @@ center(FILE *fp) tabs++; } - len = lenfunc(line) + tabs * 8 - tabs + 1; - printf("%*s", ((int) width - len) / 2 + len, line); + len = lenfunc(line) + tabs * 8 - tabs; + for (int i = (width - len) / 2; i; i--) + putchar(' '); + fputs(line, stdout); } if (ferror(fp)) { warn("getline"); @@ -145,6 +147,9 @@ utf8len(const char *s) while (*s) l += (*s++ & 0xC0) != 0x80; + if (l > 0 && s[-1] == '\n') + l--; + return l; } -- cgit v1.2.3