From 6e091776f8ef0f5248aa3ee72c75b95a198d97e8 Mon Sep 17 00:00:00 2001 From: Paul Eggert Date: Wed, 5 Jul 2023 11:01:01 -0700 Subject: [PATCH] =?UTF-8?q?diff:=20don=E2=80=99t=20backspace=20before=20fi?= =?UTF-8?q?rst=20column?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * src/util.c (output_1_line): When expanding tabs, treat backspace before column 1 as no-op, since that’s what most devices do. * tests/expand-tabs: New test. * tests/Makefile.am (TESTS): Add it. --- src/util.c | 7 +++++-- tests/Makefile.am | 1 + tests/expand-tabs | 19 +++++++++++++++++++ 3 files changed, 25 insertions(+), 2 deletions(-) create mode 100755 tests/expand-tabs diff --git a/src/util.c b/src/util.c index 053184e..c1c8660 100644 --- a/src/util.c +++ b/src/util.c @@ -1397,12 +1397,15 @@ output_1_line (char const *base, char const *limit, char const *flag_format, break; case '\b': - column--; - if (column < 0) + if (0 < column) + column--; + else if (0 < tab) { tab--; column = tab_size - 1; } + else + continue; putc (c, out); break; diff --git a/tests/Makefile.am b/tests/Makefile.am index db757bd..5eb725a 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -10,6 +10,7 @@ TESTS = \ colliding-file-names \ diff3 \ excess-slash \ + expand-tabs \ help-version \ ifdef \ invalid-re \ diff --git a/tests/expand-tabs b/tests/expand-tabs new file mode 100755 index 0000000..ae4c0fa --- /dev/null +++ b/tests/expand-tabs @@ -0,0 +1,19 @@ +#!/bin/sh +# Test tab expansion. + +. "${srcdir=.}/init.sh"; path_prepend_ ../src + +fail=0 + +cat >exp <<'EOF' || framework_failure_ +0a1 +> x +EOF + +for p in '\b\tx\n' '\b x\n' '\b \tx\n'; do + printf "$p" | returns_ 1 diff -t /dev/null - >out || fail=1 +done + +compare exp out || fail=1 + +Exit $fail