wrap: fix rune counting and don't wrap inside a unicode byte sequence - randomcrap - random crap programs of varying quality
(HTM) git clone git://git.codemadness.org/randomcrap
(DIR) Log
(DIR) Files
(DIR) Refs
(DIR) README
(DIR) LICENSE
---
(DIR) commit c6cac85875ff45f9b748100fa9277f817794b502
(DIR) parent 24d8a0fa0f87e055ea68b7004b2609287cc5d8e5
(HTM) Author: Hiltjo Posthuma <hiltjo@codemadness.org>
Date: Mon, 20 Mar 2023 20:44:01 +0100
wrap: fix rune counting and don't wrap inside a unicode byte sequence
Diffstat:
M wrap.c | 12 +++++++-----
1 file changed, 7 insertions(+), 5 deletions(-)
---
(DIR) diff --git a/wrap.c b/wrap.c
@@ -7,17 +7,19 @@ main(void)
int c, i = 0, sw = 72, hw = 79;
while ((c = getchar()) != EOF) {
- putchar(c);
if (c == '\n') {
i = 0;
- } else if (i > hw || (i > sw && (c == ' ' || c == '\t'))) {
- putchar('\n');
- i = 0;
} else {
/* start of rune, wrongly assume 1 rune is 1 column for now */
- if (!(c & 0x80))
+ if ((c & 0xc0) != 0x80) { /* start of character */
+ if (i > hw || (i > sw && (c == ' ' || c == '\t'))) {
+ putchar('\n');
+ i = 0;
+ }
i++;
+ }
}
+ putchar(c);
}
return 0;