diff: don’t backspace before first column

* 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.
This commit is contained in:
Paul Eggert 2023-07-05 11:01:01 -07:00
parent 43b7a667e5
commit 6e091776f8
3 changed files with 25 additions and 2 deletions

View File

@ -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;

View File

@ -10,6 +10,7 @@ TESTS = \
colliding-file-names \
diff3 \
excess-slash \
expand-tabs \
help-version \
ifdef \
invalid-re \

19
tests/expand-tabs Executable file
View File

@ -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