From ac7d28f7efee5ebd9576ddd577a53240c4ad6765 Mon Sep 17 00:00:00 2001 From: Rob Landley Date: Fri, 29 Sep 2023 15:37:00 -0500 Subject: [PATCH] Fold tests and corresponding fixes. --- tests/fold.test | 24 +++++++++++++++--------- toys/pending/fold.c | 13 ++++++++----- 2 files changed, 23 insertions(+), 14 deletions(-) diff --git a/tests/fold.test b/tests/fold.test index ab3353dd..e30597e5 100644 --- a/tests/fold.test +++ b/tests/fold.test @@ -4,12 +4,18 @@ #testing "name" "command" "result" "infile" "stdin" -#copied from gnu, but according to what i understand the output should be. - -testing "fold_test_1" "fold -w2 -s" "a\t\n" "" "a\t" -testing "fold_test_2" "fold -w4 -s" "abcd\nef \nd\n" "" "abcdef d\n" -testing "fold_test_3" "fold -w4 -s" "a \ncd \nfgh\n" "" "a cd fgh\n" -testing "fold_test_4" "fold -w4 -s" "abc \nef\n" "" "abc ef\n" - -#the question i posted -testing "fold_test_5" "fold -w1" "a\nb\nc\nd\n" "" "abcd" +testcmd 'default wrap' '-w10' 'one two th\nree\nfour five\n' \ + '' 'one two three\nfour five\n' +testing "abuse" "fold -w1" "a\nb\nc\nd" "" "abcd" +testcmd 'first char goes over' '-w2' '\t\n' '' '\t\n' +testcmd 'tab goes over' '-sw2' 'a\n\t\n' '' 'a\t\n' +testcmd 'tab goes over unterminated' '-sw2' 'a\n\t' '' 'a\t' +testcmd '-s' '-sw4' 'abcd\nef g\n' '' 'abcdef g\n' +testcmd '-s 2' '-sw4' 'abcd\nef \ngh\n' '' 'abcdef gh\n' +testcmd '-s 3' '-sw4' 'a \ncd \nfgh\n' '' 'a cd fgh\n' +testcmd '-s 4' '-sw4' 'abcd\n efg\n' '' 'abcd efg\n' +testcmd '-s 5' '-sw4' 'abc \nef\n' '' 'abc ef\n' +testcmd '-b tab' '-bw8' 'abc\tdefg\nhi' '' 'abc\tdefghi' +testcmd '-bs' '-bsw8' 'abc\t\ndefghi' '' 'abc\tdefghi' +testcmd 'backspace' '-w3' 'abc\bd\nef\n' '' 'abc\bdef\n' +testcmd '-b backspace' '-bw3' 'abc\n\bde\nf\n' '' 'abc\bdef\n' diff --git a/toys/pending/fold.c b/toys/pending/fold.c index 035dca1b..7f118921 100644 --- a/toys/pending/fold.c +++ b/toys/pending/fold.c @@ -45,10 +45,12 @@ void do_fold(int fd, char *name) // Parse next character's byte length and column width bb = ww = 1; - if (ss[ii]<32) ww = 0; + if (ss[ii]<32) ww = FLAG(b); if (FLAG(b)) cc = ss[ii]; - else if ((bb = utf8towc(&cc, ss+ii, 4))>0 && (ww = wcwidth(cc))<0) ww = 0; - if (cc=='\t') ww = 8-(width&7); + else { + if ((bb = utf8towc(&cc, ss+ii, 4))>0 && (ww = wcwidth(cc))<0) ww = 0; + if (cc=='\t') ww = 8-(width&7); + } // Did line end? if (!cc || cc=='\r' || cc=='\n') { @@ -64,13 +66,14 @@ void do_fold(int fd, char *name) } // backspace? - } else if (cc=='\b') { + } else if (!FLAG(b) && cc=='\b') { // Find last set bit, and clear it. This handles wide chars and tabs. while (width) { --width; if (TT.bs[width/8]&(1<<(width&7))) break; } TT.bs[width/8] &= ~(1<<(width&7)); + ii++; // Is it time to wrap? @@ -92,7 +95,7 @@ void do_fold(int fd, char *name) TT.bs[width/8] |= (1<<(width&7)); ii += bb; width += ww; - if (iswspace(cc)) space = ii; + if (FLAG(s) && iswspace(cc)) space = ii; } } if (fp != stdin) fclose(fp); -- 2.39.2