From c4014b730e8ccd65c688fa61597353149ddbcf56 Mon Sep 17 00:00:00 2001 From: Brandon Mulcahy Date: Fri, 5 Dec 2014 16:32:08 -0500 Subject: [PATCH] Use utflen in cols(1) --- cols.1 | 3 +-- cols.c | 14 +++++++++----- 2 files changed, 10 insertions(+), 7 deletions(-) diff --git a/cols.1 b/cols.1 index 8f15cb3..5383e47 100644 --- a/cols.1 +++ b/cols.1 @@ -27,8 +27,7 @@ This implementation of .B cols assumes that every byte is a character which takes up one column on the screen. -It does not handle non-ASCII UTF-8 runes -or TAB characters correctly. +It does not handle TAB characters correctly. .B cols currently mangles files which contain embedded NULs. .B cols diff --git a/cols.c b/cols.c index 9262ceb..8b99f0e 100644 --- a/cols.c +++ b/cols.c @@ -7,6 +7,7 @@ #include #include "text.h" +#include "utf.h" #include "util.h" static long chars = 65; @@ -26,7 +27,7 @@ int main(int argc, char *argv[]) { long i, l, col; - size_t len; + size_t len, bytes; int maxlen = 0; struct winsize w; FILE *fp; @@ -59,9 +60,12 @@ main(int argc, char *argv[]) } for (l = 0; l < b.nlines; ++l) { - len = strlen(b.lines[l]); - if (len > 0 && b.lines[l][len-1] == '\n') - b.lines[l][--len] = '\0'; + len = utflen(b.lines[l]); + bytes = strlen(b.lines[l]); + if (len > 0 && b.lines[l][bytes-1] == '\n') { + b.lines[l][bytes-1] = '\0'; + --len; + } if (len > maxlen) maxlen = len; if (maxlen > (chars - 1) / 2) @@ -79,7 +83,7 @@ main(int argc, char *argv[]) n_rows = (b.nlines + (n_columns - 1)) / n_columns; for (i = 0; i < n_rows; ++i) { for (l = i, col = 1; l < b.nlines; l += n_rows, ++col) { - len = strlen(b.lines[l]); + len = utflen(b.lines[l]); fputs(b.lines[l], stdout); if (col < n_columns) printf("%*s", maxlen + 1 - (int)len, "");