[dev] [PATCH] [sbase] Use utflen in cols(1)

From: Brandon Mulcahy <brandon_AT_jangler.info>
Date: Fri, 5 Dec 2014 16:32:08 -0500

---
 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
_AT_@ -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
_AT_@ -7,6 +7,7 @@
 #include <unistd.h>
 
 #include "text.h"
+#include "utf.h"
 #include "util.h"
 
 static long chars = 65;
_AT_@ -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;
_AT_@ -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)
_AT_@ -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, "");
-- 
2.1.3
Received on Fri Dec 05 2014 - 22:32:08 CET

This archive was generated by hypermail 2.3.0 : Fri Dec 05 2014 - 22:36:07 CET