fold: fix handling of multibyte characters

This commit is contained in:
Richard Ipsum 2020-10-09 15:52:10 +01:00 committed by Michael Forney
parent db1dc984c3
commit cd9f55f5e1

22
fold.c
View File

@ -7,19 +7,22 @@
#include "text.h" #include "text.h"
#include "util.h" #include "util.h"
#include "utf.h"
static int bflag = 0; static int bflag = 0;
static int sflag = 0; static int sflag = 0;
static size_t width = 80; static size_t width = 80;
static void static void
foldline(struct line *l) { foldline(struct line *l, const char *fname) {
size_t i, col, last, spacesect, len; size_t i, col, last, spacesect, len;
Rune r;
int runelen;
for (i = 0, last = 0, col = 0, spacesect = 0; i < l->len; i++) { for (i = 0, last = 0, col = 0, spacesect = 0; i < l->len; i += runelen) {
if (!UTF8_POINT(l->data[i]) && !bflag)
continue;
if (col >= width && ((l->data[i] != '\r' && l->data[i] != '\b') || bflag)) { if (col >= width && ((l->data[i] != '\r' && l->data[i] != '\b') || bflag)) {
if (bflag && col > width)
i -= runelen; /* never split a character */
len = ((sflag && spacesect) ? spacesect : i) - last; len = ((sflag && spacesect) ? spacesect : i) - last;
if (fwrite(l->data + last, 1, len, stdout) != len) if (fwrite(l->data + last, 1, len, stdout) != len)
eprintf("fwrite <stdout>:"); eprintf("fwrite <stdout>:");
@ -29,8 +32,11 @@ foldline(struct line *l) {
col = 0; col = 0;
spacesect = 0; spacesect = 0;
} }
if (sflag && isspace(l->data[i])) runelen = charntorune(&r, l->data + i, l->len - i);
spacesect = i + 1; if (!runelen || r == Runeerror)
eprintf("charntorune: %s: invalid utf\n", fname);
if (sflag && isspacerune(r))
spacesect = i + runelen;
if (!bflag && iscntrl(l->data[i])) { if (!bflag && iscntrl(l->data[i])) {
switch(l->data[i]) { switch(l->data[i]) {
case '\b': case '\b':
@ -46,7 +52,7 @@ foldline(struct line *l) {
break; break;
} }
} else { } else {
col++; col += bflag ? runelen : 1;
} }
} }
if (l->len - last) if (l->len - last)
@ -62,7 +68,7 @@ fold(FILE *fp, const char *fname)
while ((len = getline(&line.data, &size, fp)) > 0) { while ((len = getline(&line.data, &size, fp)) > 0) {
line.len = len; line.len = len;
foldline(&line); foldline(&line, fname);
} }
if (ferror(fp)) if (ferror(fp))
eprintf("getline %s:", fname); eprintf("getline %s:", fname);