cut: fix -F n-m to match toybox

function                                             old     new   delta
cut_main                                            1339    1391     +52

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
This commit is contained in:
Denys Vlasenko 2024-12-14 13:58:49 +01:00
parent d48400d8fb
commit b25ea3f156
2 changed files with 28 additions and 3 deletions

View file

@ -243,6 +243,16 @@ static void cut_file(FILE *file, const char *delim, const char *odelim,
start = next;
continue;
}
/* -F N-M preserves intermediate delimiters: */
//printf "1 2 3 4 5 6 7\n" | toybox cut -O: -F2,4-6,7
//2:4 5 6:7
if (opt_REGEX && dcount <= cut_list[cl_pos].endpos)
continue;
// NB: toybox does the above for -f too, but it's a compatibility bug:
//printf "1 2 3 4 5 6 7 8\n" | toybox cut -d' ' -O: -f2,4-6,7
//2:4 5 6:7 // WRONG!
//printf "1 2 3 4 5 6 7 8\n" | cut -d' ' --output-delimiter=: -f2,4-6,7
//2:4:5:6:7 // GNU coreutils 9.1
}
#if ENABLE_FEATURE_CUT_REGEX
if (end != start || !opt_REGEX)

View file

@ -104,6 +104,19 @@ Weather forecast for tonight : dark.
Apple: you can buy better, but you can't pay more.
Subcalifragilisticexpialidocious.
Auntie Em: Hate you, hate Kansas. Took the dog. Dorothy."
optional FEATURE_CUT_REGEX LONG_OPTS
testing "cut -F preserves intermediate delimiters" \
"cut --output-delimiter=: -F2,4-6,7" \
"2:4 5 6:7\n" \
"" "1 2 3 4\t\t5 6 7 8\n"
SKIP=
optional LONG_OPTS
testing "cut -f does not preserve intermediate delimiters" \
"cut --output-delimiter=: -d' ' -f2,4-6,7" \
"2:4:5:6:7\n" \
"" "1 2 3 4 5 6 7 8\n"
SKIP=
testing "cut empty field" "cut -d ':' -f 1-3" \
@ -124,23 +137,25 @@ testing "cut -dNEWLINE" \
"2\n4\n6\n7\n" \
"" "1\n2\n3\n4\n5\n6\n7"
optional LONG_OPTS
testing "cut -dNEWLINE --output-delimiter" \
"cut -d'
' -O@@ -f4,2,6-8" \
' --output-delimiter=@@ -f4,2,6-8" \
"2@@4@@6@@7\n" \
"" "1\n2\n3\n4\n5\n6\n7"
testing "cut -dNEWLINE --output-delimiter 2" \
"cut -d'
' -O@@ -f4,2,6-8" \
' --output-delimiter=@@ -f4,2,6-8" \
"2@@4@@6@@7\n" \
"" "1\n2\n3\n4\n5\n6\n7\n"
testing "cut -dNEWLINE --output-delimiter EMPTY_INPUT" \
"cut -d'
' -O@@ -f4,2,6-8" \
' --output-delimiter=@@ -f4,2,6-8" \
"" \
"" ""
SKIP=
# This seems to work as if delimiter is never found.
# We test here that -d '' does *not* operate as if there was no -d